ProphetRouter出现异常——The One 模拟器

ProphetRouter会遇到如下问题:

 

Exception in thread "main" java.lang.IllegalArgumentException: Comparison method
violates its general contract!
        at java.util.TimSort.mergeHi(Unknown Source)
        at java.util.TimSort.mergeAt(Unknown Source)
        at java.util.TimSort.mergeForceCollapse(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.TimSort.sort(Unknown Source)
        at java.util.Arrays.sort(Unknown Source)
        at java.util.Collections.sort(Unknown Source)
        at routing.ProphetRouter.tryOtherMessages(ProphetRouter.java:241)
        at routing.ProphetRouter.update(ProphetRouter.java:201)
        at core.DTNHost.update(DTNHost.java:330)
        at core.World.updateHosts(World.java:217)
        at core.World.update(World.java:186)
        at gui.DTNSimGUI.runSim(DTNSimGUI.java:115)
        at ui.DTNSimUI.start(DTNSimUI.java:77)
        at core.DTNSim.main(DTNSim.java:92)

 

是由于:ProphetRouter.java中的TupleComparator 引起的:

private class TupleComparator implements Comparator
        <Tuple<Message, Connection>> {

        public int compare(Tuple<Message, Connection> tuple1,
                Tuple<Message, Connection> tuple2) {
            // delivery probability of tuple1's message with tuple1's connection
            double p1 = ((ProphetRouter)tuple1.getValue().
                    getOtherNode(getHost()).getRouter()).getPredFor(
                    tuple1.getKey().getTo());
            // -"- tuple2...
            double p2 = ((ProphetRouter)tuple2.getValue().
                    getOtherNode(getHost()).getRouter()).getPredFor(
                    tuple2.getKey().getTo());

            // bigger probability should come first
            if (p2-p1 == 0) {
                /* equal probabilities -> let queue mode decide */
               return compareByQueueMode(tuple1.getKey(), tuple2.getKey());
            }
            else if (p2-p1 < 0) {
                return -1;
            }
            else {
                return 1;
            }
        }
    }

 

因为:java版本问题,java1.7使用TimSort,而TimSort发现comparable违反Comparable contract会抛出

IllegalArgumentException。

与这个问题类似:http://stackoverflow.com/questions/6626437/why-does-my-compare-method-throw-exception-comparison-method-violates-its-gen

From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source

Area: API: Utilities

Synopsis: Updated sort behavior for Arrays and Collections may throw anIllegalArgumentException

Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) byjava.util.Collections.sort has been replaced. The new sort implementation may throw anIllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently ignored such a situation. If the previous behavior is desired, you can use the new system property, java.util.Arrays.useLegacyMergeSort, to restore previous mergesort behavior.

Nature of Incompatibility: behavioral

RFE: 6804124

因此,作如下修改:

在程序主函数加入一下一行代码:

System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");

你可能感兴趣的:(ProphetRouter出现异常——The One 模拟器)