The Artificial Intelligence Forum                             data mining forum
This forum is to discuss artificial intelligence. You can ask anything about artificial intelligence : science-fiction, robots, consciousness, movies and so on! Also, you can discuss anything related to implementing artificial intelligence in machines such as programming, algorithms, etc. No registration is required!  
(Java) Sample or Small Simple Program to reproduce this error
Posted by: Dev
Date: November 26, 2019 02:25AM

Hi,

Looking for a simple or small standalone java program to reproduce the error

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!

Please share any example code snippet.

Thanks

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Date: November 26, 2019 03:47AM

Here it is:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;

public class MainTest {
	public static void main(String[] arg) {
		sort(3, 2, 2, 2, 2, 3, 2, 3, 2, 2, 3, 2, 3, 3, 2, 2, 2, 2, 2, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1, 1);

	}

	static void sort(Integer... ints) {
		List<Integer> list = Arrays.asList(ints);
		Collections.sort(list, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				if (o1 < o2) {
					return -1;
				} else {
					return 1;
				}
			}
		});
		System.out.println(list);
	}
}

The result:

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.base/java.util.TimSort.mergeLo(TimSort.java:781)
at java.base/java.util.TimSort.mergeAt(TimSort.java:518)
at java.base/java.util.TimSort.mergeCollapse(TimSort.java:448)
at java.base/java.util.TimSort.sort(TimSort.java:245)
at java.base/java.util.Arrays.sort(Arrays.java:1442)
at java.base/java.util.Arrays$ArrayList.sort(Arrays.java:4426)
at java.base/java.util.Collections.sort(Collections.java:177)
at test.MainTest.sort(MainTest.java:17)
at test.MainTest.main(MainTest.java:10)

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Posted by: Java
Date: November 26, 2019 05:51AM

Why this program throws error...Excellent..you can reproduced the problem....

What was the root cause?

Where the problem is?

Is the code has issue?

Please explain

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Date: November 26, 2019 07:04AM

Hi,

The compare() method in Java is defined in the Compator Interface.

Usually, we redefine the compare() method to be able to compare some objects. For examle, in my example, I defined a compare() method to compare integers stored in some list.

Now, when we define a compare() method, there are some rules to be followed. This is what is called the contract. If you dont follow that contract, then you may get the error that you had.

The main rules are not complicated.
If you compare two elements "a" and "b":
- if a < b then compare() should return a negative integer
- if a == b then compare() should return 0
- if a > b then compare() should return a positive integer

Beside, it is expected that compare() is transitive, that is if a > b and b > c, then a > c.

Beside, it is expected that if you check a < b and b > a they will give you the opposite result.

The problem in my example is that the compare() method assumes that if a < b is not true then it is a > b. But this is not necessarily true... In fact, it is possible that a = b. So because of this, the compare method() of my example will always say that a > b when in fact it is a ==b or a > b. This violates the contract and cause the error. The correct method should be like this:

if (o1 < o2) {
					return -1;
				} else if (o1 > o2){
					return 1;
				}else {
					return 0;
				}

In other words, the problem in my example is that the compare() method was not defined correctly. You can make a compare() method to compare anything like numbers, apples, oranges, etc. But in any case, you need to follow the contract of the compare() method, which I described above.



Edited 2 time(s). Last edit at 11/26/2019 07:06AM by webmasterphilfv.

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Posted by: Dev
Date: November 26, 2019 08:15PM

But the below code is not throwing exception as you said...

package com.company;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;

public class MainTest2 {
public static void main(String[] arg) {
sort(1,2, 1, 3, 1);

}

static void sort(Integer... ints) {
List<Integer> list = Arrays.asList(ints);
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 < o2) {
return -1;
} else {
return 1;
}
}
});
System.out.println(list);
}
}

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Date: November 26, 2019 08:36PM

Yes, if you apply the compare method for sorting different kind of data, it is possible that you dont get errors but it does not mean that the compare() method is correct.


For example, it is assumed that if compare(a,b) < 0 then compare(b,a) >0. If your compare() method dont respect that, the compare method violates the contract. However, if you apply the compare method to a list of only one element like: [1] it will not throw any error.... The error will just show up in some specific case like if your data contains at least two elements.... And here it depends also on the sorting algorithm and how it accesses your data. The sorting algorithm will find out that on some data your compare() method is violating the contract or it will not find out depending on the data. But still the compare() method is wrong.



Edited 1 time(s). Last edit at 11/26/2019 08:37PM by webmasterphilfv.

Options: ReplyQuote
Re: (Java) Sample or Small Simple Program to reproduce this error
Posted by: Dev
Date: November 26, 2019 08:48PM

So, it means that this would occur in rare or edge case....

When mutiple values are being sorted only this problem may arise...

Is that true ? Or we don't know when the issue will araise.

Options: ReplyQuote


This forum is powered by Phorum and provided by P. Fournier-Viger (© 2012).
Terms of use.