java - What could possibly be wrong with this Comparator? -


i'm getting "exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: comparison method violates general contract!" errors comparator. used in priorityqueue.

public class nodefpqcomp implements comparator<node> {      @override     public int compare(node arg0, node arg1) {         int result=0;          if (arg0.getf() - arg1.getf() > 0) result = 1;         if (arg0.getf() - arg1.getf() < 0) result = -1;          return result;       }  } 

getf() float value never gets close limits no overflow issues here. returns sum of 2 other floats:

public float getg() {         return this.g;     }      public float geth() {         return this.h;     }      public float getf() {         return getg() + geth();     } 

here's queue uses it:

openlist = new priorityqueue<node>((int) gridsizex()*gridsizey()/10, new nodefpqcomp()); 

what want have queue (a-star path finding) accept multiple objects of same value (same f-value), different identity (the different nodes/squares on map).

the weird thing path finding still works, why getting exception? how comparator not working? missing? changing values getf() relies on after object has been added queue (this happens lot in a-star) mess somehow?

and here's full stacktrace:

exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: comparison method violates general contract!     @ java.util.timsort.mergelo(unknown source)     @ java.util.timsort.mergeat(unknown source)     @ java.util.timsort.mergecollapse(unknown source)     @ java.util.timsort.sort(unknown source)     @ java.util.timsort.sort(unknown source)     @ java.util.arrays.sort(unknown source)     @ java.util.collections.sort(unknown source)     @ javax.swing.sortingfocustraversalpolicy.enumerateandsortcycle(unknown source)     @ javax.swing.sortingfocustraversalpolicy.getfocustraversalcycle(unknown source)     @ javax.swing.sortingfocustraversalpolicy.getfirstcomponent(unknown source)     @ javax.swing.layoutfocustraversalpolicy.getfirstcomponent(unknown source)     @ javax.swing.sortingfocustraversalpolicy.getdefaultcomponent(unknown source)     @ java.awt.focustraversalpolicy.getinitialcomponent(unknown source)     @ java.awt.defaultkeyboardfocusmanager.dispatchevent(unknown source)     @ java.awt.component.dispatcheventimpl(unknown source)     @ java.awt.container.dispatcheventimpl(unknown source)     @ java.awt.window.dispatcheventimpl(unknown source)     @ java.awt.component.dispatchevent(unknown source)     @ java.awt.eventqueue.dispatcheventimpl(unknown source)     @ java.awt.eventqueue.access$200(unknown source)     @ java.awt.eventqueue$3.run(unknown source)     @ java.awt.eventqueue$3.run(unknown source)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.awt.eventqueue$4.run(unknown source)     @ java.awt.eventqueue$4.run(unknown source)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.awt.eventqueue.dispatchevent(unknown source)     @ java.awt.sequencedevent.dispatch(unknown source)     @ java.awt.eventqueue.dispatcheventimpl(unknown source)     @ java.awt.eventqueue.access$200(unknown source)     @ java.awt.eventqueue$3.run(unknown source)     @ java.awt.eventqueue$3.run(unknown source)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.awt.eventqueue$4.run(unknown source)     @ java.awt.eventqueue$4.run(unknown source)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(unknown source)     @ java.awt.eventqueue.dispatchevent(unknown source)     @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source)     @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source)     @ java.awt.eventdispatchthread.pumpevents(unknown source)     @ java.awt.eventdispatchthread.pumpevents(unknown source)     @ java.awt.eventdispatchthread.run(unknown source) 

the "contract" means compare method should total ordering. (edit) should consistent equals(object obj) method (even though may not strictly imposed) make sure data structures (such treeset, treemap) behave way expect, may based on both conparison and equals method.

make sure have in check.

total ordering means

  • a.compareto(a) >= 0, every a
  • a.compareto(b) >= 0 , b.compareto(a) >= 0 implies a "equal" b, every a,b; means if a.compareto(b) == 0, b.compareto(a) == 0
  • a.compareto(b) >= 0 , b.compareto(c) >= 0 implies a.compareto(c) >= 0, every a,b,c

it's easy give counterexamples compare method returns {0,1,-1} , not consistent total ordering, check in particular.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -