Example usage for java.lang Double compare

List of usage examples for java.lang Double compare

Introduction

In this page you can find the example usage for java.lang Double compare.

Prototype

public static int compare(double d1, double d2) 

Source Link

Document

Compares the two specified double values.

Usage

From source file:imitationNLG.SFX.java

public Double evaluateGeneration(HashMap<String, JAROW> classifierAttrs,
        HashMap<String, HashMap<String, JAROW>> classifierWords, ArrayList<DatasetInstance> trainingData,
        ArrayList<DatasetInstance> testingData, HashMap<String, HashSet<String>> availableAttributeActions,
        HashMap<String, HashMap<String, HashSet<Action>>> availableWordActions,
        HashMap<Integer, HashSet<String>> nGrams, boolean printResults, int epoch) {
    System.out.println("Evaluate argument generation ");

    int totalArgDistance = 0;
    ArrayList<ScoredFeaturizedTranslation<IString, String>> generations = new ArrayList<>();
    ArrayList<ArrayList<Action>> generationActions = new ArrayList<>();
    HashMap<ArrayList<Action>, DatasetInstance> generationActionsMap = new HashMap<>();
    ArrayList<ArrayList<Sequence<IString>>> finalReferences = new ArrayList<>();
    ArrayList<String> predictedStrings = new ArrayList<>();
    ArrayList<String> predictedStringMRs = new ArrayList<>();
    ArrayList<Double> attrCoverage = new ArrayList<>();
    ArrayList<ArrayList<String>> predictedAttrLists = new ArrayList<>();
    HashSet<HashMap<String, HashSet<String>>> mentionedAttrs = new HashSet<HashMap<String, HashSet<String>>>();
    for (DatasetInstance di : testingData) {
        String predicate = di.getMeaningRepresentation().getPredicate();
        ArrayList<Action> predictedActionList = new ArrayList<>();
        ArrayList<Action> predictedWordList = new ArrayList<>();

        //PHRASE GENERATION EVALUATION
        String predictedAttr = "";
        ArrayList<String> predictedAttrValues = new ArrayList<>();
        ArrayList<String> predictedAttributes = new ArrayList<>();

        HashSet<String> attrValuesToBeMentioned = new HashSet<>();
        HashSet<String> attrValuesAlreadyMentioned = new HashSet<>();
        HashMap<String, ArrayList<String>> valuesToBeMentioned = new HashMap<>();
        for (String attribute : di.getMeaningRepresentation().getAttributes().keySet()) {
            for (String value : di.getMeaningRepresentation().getAttributes().get(attribute)) {
                attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase());
            }//www.  j a  v  a2s . c  o m
            valuesToBeMentioned.put(attribute,
                    new ArrayList<>(di.getMeaningRepresentation().getAttributes().get(attribute)));
        }
        if (attrValuesToBeMentioned.isEmpty()) {
            attrValuesToBeMentioned.add("empty=empty");
        }
        HashSet<String> attrValuesToBeMentionedCopy = new HashSet<>(attrValuesToBeMentioned);
        while (!predictedAttr.equals(SFX.TOKEN_END) && predictedAttrValues.size() < maxAttrRealizationSize) {
            if (!predictedAttr.isEmpty()) {
                attrValuesToBeMentioned.remove(predictedAttr);
            }
            Instance attrTrainingVector = SFX.this.createAttrInstance(predicate, "@TOK@", predictedAttrValues,
                    predictedActionList, attrValuesAlreadyMentioned, attrValuesToBeMentioned,
                    di.getMeaningRepresentation(), availableAttributeActions);

            if (attrTrainingVector != null) {
                Prediction predictAttr = classifierAttrs.get(predicate).predict(attrTrainingVector);
                if (predictAttr.getLabel() != null) {
                    predictedAttr = predictAttr.getLabel().trim();
                    String predictedValue = "";
                    if (!predictedAttr.equals(SFX.TOKEN_END)) {
                        predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned, trainingData);

                        HashSet<String> rejectedAttrs = new HashSet<String>();
                        while (predictedValue.isEmpty() && !predictedAttr.equals(SFX.TOKEN_END)) {
                            rejectedAttrs.add(predictedAttr);

                            predictedAttr = SFX.TOKEN_END;
                            double maxScore = -Double.MAX_VALUE;
                            for (String attr : predictAttr.getLabel2Score().keySet()) {
                                if (!rejectedAttrs.contains(attr) && (Double
                                        .compare(predictAttr.getLabel2Score().get(attr), maxScore) > 0)) {
                                    maxScore = predictAttr.getLabel2Score().get(attr);
                                    predictedAttr = attr;
                                }
                            }
                            if (!predictedAttr.equals(SFX.TOKEN_END)) {
                                predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned,
                                        trainingData);
                            }
                        }
                    }
                    if (!predictedAttr.equals(SFX.TOKEN_END)) {
                        predictedAttr += "=" + predictedValue;
                    }
                    predictedAttrValues.add(predictedAttr);

                    String attribute = predictedAttrValues.get(predictedAttrValues.size() - 1).split("=")[0];
                    String attrValue = predictedAttrValues.get(predictedAttrValues.size() - 1);
                    predictedAttributes.add(attrValue);

                    //GENERATE PHRASES
                    if (!attribute.equals(SFX.TOKEN_END)) {
                        if (classifierWords.get(predicate).containsKey(attribute)) {
                            String predictedWord = "";

                            boolean isValueMentioned = false;
                            String valueTBM = "";
                            if (attrValue.contains("=")) {
                                valueTBM = attrValue.substring(attrValue.indexOf('=') + 1);
                            }
                            if (valueTBM.isEmpty()) {
                                isValueMentioned = true;
                            }
                            ArrayList<String> subPhrase = new ArrayList<>();
                            while (!predictedWord.equals(RoboCup.TOKEN_END)
                                    && predictedWordList.size() < maxWordRealizationSize) {
                                ArrayList<String> predictedAttributesForInstance = new ArrayList<>();
                                for (int i = 0; i < predictedAttributes.size() - 1; i++) {
                                    predictedAttributesForInstance.add(predictedAttributes.get(i));
                                }
                                if (!predictedAttributes.get(predictedAttributes.size() - 1)
                                        .equals(attrValue)) {
                                    predictedAttributesForInstance
                                            .add(predictedAttributes.get(predictedAttributes.size() - 1));
                                }
                                Instance wordTrainingVector = createWordInstance(predicate,
                                        new Action("@TOK@", attrValue), predictedAttributesForInstance,
                                        predictedActionList, isValueMentioned, attrValuesAlreadyMentioned,
                                        attrValuesToBeMentioned, di.getMeaningRepresentation(),
                                        availableWordActions.get(predicate), nGrams, false);

                                if (wordTrainingVector != null) {
                                    if (classifierWords.get(predicate) != null) {
                                        if (classifierWords.get(predicate).get(attribute) != null) {
                                            Prediction predictWord = classifierWords.get(predicate)
                                                    .get(attribute).predict(wordTrainingVector);
                                            if (predictWord.getLabel() != null) {
                                                predictedWord = predictWord.getLabel().trim();
                                                predictedActionList.add(new Action(predictedWord, attrValue));
                                                if (!predictedWord.equals(SFX.TOKEN_END)) {
                                                    subPhrase.add(predictedWord);
                                                    predictedWordList.add(new Action(predictedWord, attrValue));
                                                }
                                            } else {
                                                predictedWord = SFX.TOKEN_END;
                                                predictedActionList.add(new Action(predictedWord, attrValue));
                                            }
                                        } else {
                                            predictedWord = SFX.TOKEN_END;
                                            predictedActionList.add(new Action(predictedWord, attrValue));
                                        }
                                    }
                                }
                                if (!isValueMentioned) {
                                    if (!predictedWord.equals(SFX.TOKEN_END)) {
                                        if (predictedWord.startsWith(SFX.TOKEN_X)
                                                && (valueTBM.matches("\"[xX][0-9]+\"")
                                                        || valueTBM.matches("[xX][0-9]+")
                                                        || valueTBM.startsWith(SFX.TOKEN_X))) {
                                            isValueMentioned = true;
                                        } else if (!predictedWord.startsWith(SFX.TOKEN_X)
                                                && !(valueTBM.matches("\"[xX][0-9]+\"")
                                                        || valueTBM.matches("[xX][0-9]+")
                                                        || valueTBM.startsWith(SFX.TOKEN_X))) {
                                            String valueToCheck = valueTBM;
                                            if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                                    || valueToCheck.equals("yes or no")
                                                    || valueToCheck.equals("none")
                                                    || valueToCheck.equals("dont_care")
                                                    || valueToCheck.equals("empty")) {
                                                if (attribute.contains("=")) {
                                                    valueToCheck = attribute.replace("=", ":");
                                                } else {
                                                    valueToCheck = attribute + ":" + valueTBM;
                                                }
                                            }
                                            if (!valueToCheck.equals("empty:empty")
                                                    && valueAlignments.containsKey(valueToCheck)) {
                                                for (ArrayList<String> alignedStr : valueAlignments
                                                        .get(valueToCheck).keySet()) {
                                                    if (endsWith(subPhrase, alignedStr)) {
                                                        isValueMentioned = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (isValueMentioned) {
                                        attrValuesAlreadyMentioned.add(attrValue);
                                        attrValuesToBeMentioned.remove(attrValue);
                                    }
                                }
                                String mentionedAttrValue = "";
                                if (!predictedWord.startsWith(SFX.TOKEN_X)) {
                                    for (String attrValueTBM : attrValuesToBeMentioned) {
                                        if (attrValueTBM.contains("=")) {
                                            String value = attrValueTBM
                                                    .substring(attrValueTBM.indexOf('=') + 1);
                                            if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+")
                                                    || value.startsWith(SFX.TOKEN_X))) {
                                                String valueToCheck = value;
                                                if (valueToCheck.equals("no") || valueToCheck.equals("yes")
                                                        || valueToCheck.equals("yes or no")
                                                        || valueToCheck.equals("none")
                                                        || valueToCheck.equals("dont_care")
                                                        || valueToCheck.equals("empty")) {
                                                    valueToCheck = attrValueTBM.replace("=", ":");
                                                }
                                                if (!valueToCheck.equals("empty:empty")
                                                        && valueAlignments.containsKey(valueToCheck)) {
                                                    for (ArrayList<String> alignedStr : valueAlignments
                                                            .get(valueToCheck).keySet()) {
                                                        if (endsWith(subPhrase, alignedStr)) {
                                                            mentionedAttrValue = attrValueTBM;
                                                            break;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (!mentionedAttrValue.isEmpty()) {
                                    attrValuesAlreadyMentioned.add(attrValue);
                                    attrValuesToBeMentioned.remove(mentionedAttrValue);
                                }
                            }
                            if (predictedWordList.size() >= maxWordRealizationSize && !predictedActionList
                                    .get(predictedActionList.size() - 1).getWord().equals(SFX.TOKEN_END)) {
                                predictedWord = SFX.TOKEN_END;
                                predictedActionList.add(new Action(predictedWord,
                                        predictedAttrValues.get(predictedAttrValues.size() - 1)));
                            }
                        } else {
                            String predictedWord = SFX.TOKEN_END;
                            predictedActionList.add(new Action(predictedWord, attrValue));
                        }
                    }
                } else {
                    predictedAttr = SFX.TOKEN_END;
                }
            }
        }
        ArrayList<String> predictedAttrs = new ArrayList<>();
        for (String attributeValuePair : predictedAttrValues) {
            predictedAttrs.add(attributeValuePair.split("=")[0]);
        }

        ArrayList<Action> cleanActionList = new ArrayList<Action>();
        for (Action action : predictedActionList) {
            if (!action.getWord().equals(SFX.TOKEN_END) && !action.getWord().equals(SFX.TOKEN_START)) {
                cleanActionList.add(action);
            }
        }
        for (int i = 0; i < cleanActionList.size(); i++) {
            for (ArrayList<Action> surrounds : punctPatterns.keySet()) {
                boolean matches = true;
                int m = 0;
                for (int s = 0; s < surrounds.size(); s++) {
                    if (surrounds.get(s) != null) {
                        if (i + s < cleanActionList.size()) {
                            if (!cleanActionList.get(i + s).getWord().equals(surrounds.get(s)
                                    .getWord()) /*|| !cleanActionList.get(i).getAttribute().equals(surrounds.get(s).getAttribute())*/) {
                                matches = false;
                                s = surrounds.size();
                            } else {
                                m++;
                            }
                        } else {
                            matches = false;
                            s = surrounds.size();
                        }
                    }
                }
                if (matches && m > 0) {
                    cleanActionList.add(i + 2, punctPatterns.get(surrounds));
                }
            }
        }

        String predictedString = "";
        ArrayList<String> predictedAttrList = new ArrayList<String>();
        HashSet<String> redundants = new HashSet<String>();
        for (Action action : cleanActionList) {
            if (action.getWord().startsWith(SFX.TOKEN_X)) {
                predictedString += di.getMeaningRepresentation().getDelexMap().get(action.getWord()) + " ";
                //predictedString += "x ";
                if (di.getMeaningRepresentation().getDelexMap().get(action.getWord()) == null
                        || di.getMeaningRepresentation().getDelexMap().get(action.getWord()).equals("null")) {
                    redundants.add(action.getWord());
                }
            } else {
                predictedString += action.getWord() + " ";
            }
            if (predictedAttrList.isEmpty()) {
                predictedAttrList.add(action.getAttribute());
            } else if (!predictedAttrList.get(predictedAttrList.size() - 1).equals(action.getAttribute())) {
                predictedAttrList.add(action.getAttribute());
            }
        }
        predictedAttrLists.add(predictedAttrList);
        if (attrValuesToBeMentionedCopy.size() != 0.0) {
            double redundAttrs = 0.0;
            double missingAttrs = 0.0;
            for (String attr : predictedAttrList) {
                if (!attrValuesToBeMentionedCopy.contains(attr)) {
                    redundAttrs += 1.0;
                }
            }
            for (String attr : attrValuesToBeMentionedCopy) {
                if (!predictedAttrList.contains(attr)) {
                    missingAttrs += 1.0;
                }
            }
            double attrSize = (double) attrValuesToBeMentionedCopy.size();
            attrCoverage.add((redundAttrs + missingAttrs) / attrSize);
        }

        if (predicate.startsWith("?")) {
            predictedString = predictedString.trim() + "?";
        } else {
            predictedString = predictedString.trim() + ".";
        }
        predictedString = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();

        if (!mentionedAttrs.contains(di.getMeaningRepresentation().getAttributes())) {
            predictedStrings.add(predictedString);
            predictedStringMRs.add(di.getMeaningRepresentation().getMRstr());
            mentionedAttrs.add(di.getMeaningRepresentation().getAttributes());
        }

        Sequence<IString> translation = IStrings
                .tokenize(NISTTokenizer.tokenize(predictedString.toLowerCase()));
        ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(translation, null,
                0);
        generations.add(tran);
        generationActions.add(predictedActionList);
        generationActionsMap.put(predictedActionList, di);

        ArrayList<Sequence<IString>> references = new ArrayList<>();
        for (ArrayList<Action> realization : di.getEvalRealizations()) {
            String cleanedWords = "";
            for (Action nlWord : realization) {
                if (!nlWord.equals(new Action(SFX.TOKEN_START, ""))
                        && !nlWord.equals(new Action(SFX.TOKEN_END, ""))) {
                    if (nlWord.getWord().startsWith(SFX.TOKEN_X)) {
                        cleanedWords += di.getMeaningRepresentation().getDelexMap().get(nlWord.getWord()) + " ";
                    } else {
                        cleanedWords += nlWord.getWord() + " ";
                    }
                }
            }
            cleanedWords = cleanedWords.trim();
            if (!cleanedWords.endsWith(".")) {
                cleanedWords += ".";
            }
            cleanedWords = cleanedWords.replaceAll("\\?", " \\? ").replaceAll(":", " : ")
                    .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ").trim();
            references.add(IStrings.tokenize(NISTTokenizer.tokenize(cleanedWords)));
        }
        finalReferences.add(references);

        //EVALUATE ATTRIBUTE SEQUENCE
        HashSet<ArrayList<String>> goldAttributeSequences = new HashSet<>();
        for (DatasetInstance di2 : testingData) {
            if (di2.getMeaningRepresentation().getAttributes()
                    .equals(di.getMeaningRepresentation().getAttributes())) {
                goldAttributeSequences.addAll(di2.getEvalMentionedAttributeSequences().values());
            }
        }

        int minTotArgDistance = Integer.MAX_VALUE;
        for (ArrayList<String> goldArgs : goldAttributeSequences) {
            int totArgDistance = 0;
            HashSet<Integer> matchedPositions = new HashSet<>();
            for (int i = 0; i < predictedAttrs.size(); i++) {
                if (!predictedAttrs.get(i).equals(SFX.TOKEN_START)
                        && !predictedAttrs.get(i).equals(SFX.TOKEN_END)) {
                    int minArgDistance = Integer.MAX_VALUE;
                    int minArgPos = -1;
                    for (int j = 0; j < goldArgs.size(); j++) {
                        if (!matchedPositions.contains(j)) {
                            if (goldArgs.get(j).equals(predictedAttrs.get(i))) {
                                int argDistance = Math.abs(j - i);

                                if (argDistance < minArgDistance) {
                                    minArgDistance = argDistance;
                                    minArgPos = j;
                                }
                            }
                        }
                    }

                    if (minArgPos == -1) {
                        totArgDistance += 100;
                    } else {
                        matchedPositions.add(minArgPos);
                        totArgDistance += minArgDistance;
                    }
                }
            }
            ArrayList<String> predictedCopy = (ArrayList<String>) predictedAttrs.clone();
            for (String goldArg : goldArgs) {
                if (!goldArg.equals(SFX.TOKEN_END)) {
                    boolean contained = predictedCopy.remove(goldArg);
                    if (!contained) {
                        totArgDistance += 1000;
                    }
                }
            }
            if (totArgDistance < minTotArgDistance) {
                minTotArgDistance = totArgDistance;
            }
        }
        totalArgDistance += minTotArgDistance;
    }

    previousResults = generationActions;

    crossAvgArgDistances.add(totalArgDistance / (double) testingData.size());

    NISTMetric NIST = new NISTMetric(finalReferences);
    BLEUMetric BLEU = new BLEUMetric(finalReferences, 4, false);
    BLEUMetric BLEUsmooth = new BLEUMetric(finalReferences, 4, true);
    Double nistScore = NIST.score(generations);
    Double bleuScore = BLEU.score(generations);
    Double bleuSmoothScore = BLEUsmooth.score(generations);

    double finalCoverage = 0.0;
    for (double c : attrCoverage) {
        finalCoverage += c;
    }
    finalCoverage /= (double) attrCoverage.size();
    crossNIST.add(nistScore);
    crossBLEU.add(bleuScore);
    crossBLEUSmooth.add(bleuSmoothScore);
    System.out.println("Avg arg distance: \t" + totalArgDistance / (double) testingData.size());
    System.out.println("NIST: \t" + nistScore);
    System.out.println("BLEU: \t" + bleuScore);
    System.out.println("COVERAGE: \t" + finalCoverage);
    System.out.println("g: " + generations);
    System.out.println("attr: " + predictedAttrLists);
    System.out.println("BLEU smooth: \t" + bleuSmoothScore);
    previousBLEU = bleuScore;

    if (printResults) {
        BufferedWriter bw = null;
        File f = null;
        try {
            f = new File("random_SFX" + dataset + "TextsAfter" + (epoch) + "_"
                    + JDAggerForSFX.earlyStopMaxFurtherSteps + "_" + JDAggerForSFX.p + "epochsTESTINGDATA.txt");
        } catch (NullPointerException e) {
            System.err.println("File not found." + e);
        }

        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
        } catch (FileNotFoundException e) {
            System.err.println("Error opening file for writing! " + e);
        }

        try {
            bw.write("BLEU:" + bleuScore);
            bw.write("\n");
        } catch (IOException e) {
            System.err.println("Write error!");
        }
        for (int i = 0; i < predictedStrings.size(); i++) {
            try {
                //Grafoume to String sto arxeio
                //SFX HOTEL TEXTS WITH LOLS -> 3
                //SFX RESTAURANT TEXTS WITH LOLS -> 5
                bw.write("MR;" + predictedStringMRs.get(i).replaceAll(";", ",") + ";");
                if (dataset.equals("hotel")) {
                    bw.write("LOLS_SFHOT;");
                } else {
                    bw.write("LOLS_SFRES;");
                }
                //bw.write("@@srcdoc@@" + (i + 1));
                /*String out = predictedStrings.get(i).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ");
                out = out.substring(0, 1).toUpperCase() + out.substring(1);
                bw.write(out + ";");
                if (dataset.equals("hotel")) {
                bw.write("WEN_SFHOT;");
                } else {
                bw.write("WEN_SFRES;");
                }
                if (!wenDaToGen.containsKey(predictedStringMRs.get(i).trim().toLowerCase())) {
                System.out.println(wenDaToGen.keySet());
                System.out.println(predictedStringMRs.get(i).trim().toLowerCase());
                System.exit(0);
                }
                out = wenDaToGen.get(predictedStringMRs.get(i).trim().toLowerCase()).replaceAll(" i ", " I ").replaceAll(" -ly ", "ly ").replaceAll(" s ", "s ").replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll("  ", " ");
                out = out.substring(0, 1).toUpperCase() + out.substring(1);
                bw.write(out + ";");*/
                //bw.write("@@judgeFluency@@-1");
                //bw.write("@@judgeInform@@-1");
                //bw.write("@@judgeQuality@@-1");

                bw.write("\n");
            } catch (IOException e) {
                System.err.println("Write error!");
            }
        }

        try {
            bw.close();
        } catch (IOException e) {
            System.err.println("Error closing file.");
        } catch (Exception e) {
        }
    }
    return bleuScore;
}

From source file:com.almende.eve.agent.MeetingAgent.java

/**
 * Calculate all feasible intervals with their preference weight, based on
 * the event status, stored infeasible intervals, and preferred intervals.
 * If there are no solutions, an empty array is returned.
 * // w  w w.  ja v  a  2 s .  co  m
 * @return solutions
 */
private List<Weight> calculateSolutions() {
    LOG.info("calculateSolutions started"); // TODO: cleanup

    final State state = getState();
    final List<Weight> solutions = new ArrayList<Weight>();

    // get the activity
    final Activity activity = state.get("activity", Activity.class);
    if (activity == null) {
        return solutions;
    }

    // get infeasible intervals
    ArrayList<Interval> infeasible = state.get("infeasible", new TypeUtil<ArrayList<Interval>>() {
    });
    if (infeasible == null) {
        infeasible = new ArrayList<Interval>();
    }

    // get preferred intervals
    List<Weight> preferred = state.get("preferred", new TypeUtil<ArrayList<Weight>>() {
    });
    if (preferred == null) {
        preferred = new ArrayList<Weight>();
    }

    // get the duration of the activity
    final Long durationLong = activity.withConstraints().withTime().getDuration();
    Duration duration = null;
    if (durationLong != null) {
        duration = new Duration(durationLong);
    } else {
        // TODO: give error when duration is not defined?
        duration = Duration.standardHours(1);
    }

    // check interval at next half hour
    final DateTime firstTimeslot = getNextHalfHour();
    Interval test = new Interval(firstTimeslot, firstTimeslot.plus(duration));
    testInterval(infeasible, preferred, test, solutions);

    // loop over all infeasible intervals
    for (final Interval i : infeasible) {
        // test timeslot left from the infeasible interval
        test = new Interval(i.getStart().minus(duration), i.getStart());
        testInterval(infeasible, preferred, test, solutions);

        // test timeslot right from the infeasible interval
        test = new Interval(i.getEnd(), i.getEnd().plus(duration));
        testInterval(infeasible, preferred, test, solutions);
    }

    // loop over all preferred intervals
    for (final Weight w : preferred) {
        // test timeslot left from the start of the preferred interval
        test = new Interval(w.getStart().minus(duration), w.getStart());
        testInterval(infeasible, preferred, test, solutions);

        // test timeslot right from the start of the preferred interval
        test = new Interval(w.getStart(), w.getStart().plus(duration));
        testInterval(infeasible, preferred, test, solutions);

        // test timeslot left from the end of the preferred interval
        test = new Interval(w.getEnd().minus(duration), w.getEnd());
        testInterval(infeasible, preferred, test, solutions);

        // test timeslot right from the end of the preferred interval
        test = new Interval(w.getEnd(), w.getEnd().plus(duration));
        testInterval(infeasible, preferred, test, solutions);
    }

    // order the calculated feasible timeslots by weight, from highest to
    // lowest. In case of equals weights, the timeslots are ordered by
    // start date
    class WeightComparator implements Comparator<Weight> {
        @Override
        public int compare(final Weight a, final Weight b) {
            if (a.getWeight() != null && b.getWeight() != null) {
                final int cmp = Double.compare(a.getWeight(), b.getWeight());
                if (cmp == 0) {
                    return a.getStart().compareTo(b.getStart());
                } else {
                    return -cmp;
                }
            }
            return 0;
        }
    }
    final WeightComparator comparator = new WeightComparator();
    Collections.sort(solutions, comparator);

    // remove duplicates
    int i = 1;
    while (i < solutions.size()) {
        if (solutions.get(i).equals(solutions.get(i - 1))) {
            solutions.remove(i);
        } else {
            i++;
        }
    }

    return solutions;
}

From source file:ml.shifu.shifu.core.dtrain.dt.DTMaster.java

/**
 * Split node into left and right for level-wised tree growth, doneNode should be populated by
 * {@link #populateGainInfoToNode(int, Node, GainInfo)}
 *///www .  j a va2s.co  m
private void splitNodeForLevelWisedTree(boolean isLeaf, int treeId, Node doneNode) {
    if (!isLeaf) {
        boolean leftChildIsLeaf = Node.indexToLevel(doneNode.getId()) + 1 == this.maxDepth
                || Double.compare(doneNode.getLeftImpurity(), 0d) == 0;
        // such node is just set into isLeaf to true, a new node is created with leaf flag but will be
        // changed to final leaf in later iteration
        int leftIndex = Node.leftIndex(doneNode.getId());
        Node left = new Node(leftIndex, doneNode.getLeftPredict(), doneNode.getLeftImpurity(), true);
        doneNode.setLeft(left);
        // update nodeNum
        this.trees.get(treeId).incrNodeNum();

        if (!leftChildIsLeaf) {
            this.toDoQueue.offer(new TreeNode(treeId, left));
        } else {
            LOG.debug("Left node {} in tree {} is set to leaf and not submitted to workers", leftIndex, treeId);
        }

        boolean rightChildIsLeaf = Node.indexToLevel(doneNode.getId()) + 1 == this.maxDepth
                || Double.compare(doneNode.getRightImpurity(), 0d) == 0;

        // such node is just set into isLeaf to true
        int rightIndex = Node.rightIndex(doneNode.getId());
        Node right = new Node(rightIndex, doneNode.getRightPredict(), doneNode.getRightImpurity(), true);

        doneNode.setRight(right);
        // update nodeNum
        this.trees.get(treeId).incrNodeNum();

        if (!rightChildIsLeaf) {
            this.toDoQueue.offer(new TreeNode(treeId, right));
        } else {
            LOG.debug("Right node {} in tree {} is set to leaf and not submitted to workers", rightIndex,
                    treeId);
        }
    } else {
        LOG.info("Done node {} in tree {} is final set to leaf", doneNode.getId(), treeId);
    }
}

From source file:org.gvsig.framework.web.service.impl.OGCInfoServiceImpl.java

/**
 * Get the four coordinates that represent the bounding box
 * which includes all the layers indicated for a WMS Server
 *
 * @param urlServer Url of the WMS server to connect and get the data
 * @param crs CRS of the bounding box/*from w  ww  .  ja v  a 2  s .  c  o m*/
 * @param layers List of layers to include in the bounding box calculated
 * @return A list of coordinates that represents the bounding box calculated
 *         (minX, minY, maxX, maxY). null if haven't valid bounding box
 */
private List<String> getWMSLayersBoundingBox(String urlServer, String crs, TreeSet<String> layers) {
    List<String> boundingBox = new ArrayList<String>();
    double xMax = 0;
    double xMin = 0;
    double yMin = 0;
    double yMax = 0;
    BoundaryBox bbox = null;
    try {
        WMSClient wms = new WMSClient(urlServer);
        wms.connect(null);
        if (layers != null) {
            Iterator<String> iterLayers = layers.iterator();
            // get the first element
            WMSLayer layer = wms.getLayer(iterLayers.next());
            if (layer != null) {
                bbox = layer.getBbox(crs);
                if (bbox != null) {
                    xMax = bbox.getXmax();
                    yMax = bbox.getYmax();
                    xMin = bbox.getXmin();
                    yMin = bbox.getYmin();
                }
            }
            while (iterLayers.hasNext()) {
                layer = wms.getLayer(iterLayers.next());
                bbox = layer.getBbox(crs);
                if (bbox != null) {
                    // if getXmax is greater than xMax
                    if (Double.compare(xMax, bbox.getXmax()) < 0) {
                        xMax = bbox.getXmax();
                    }
                    // if getYmax is greater than yMax
                    if (Double.compare(yMax, bbox.getYmax()) < 0) {
                        yMax = bbox.getYmax();
                    }
                    // if getXmin is less than xMin
                    if (Double.compare(xMin, bbox.getXmin()) > 0) {
                        xMin = bbox.getXmin();
                    }
                    // if getYmin is less than yMin
                    if (Double.compare(yMin, bbox.getYmin()) > 0) {
                        yMin = bbox.getYmin();
                    }
                }
            }
            if (bbox != null) {
                boundingBox.add(String.valueOf(xMin));
                boundingBox.add(String.valueOf(yMin));
                boundingBox.add(String.valueOf(xMax));
                boundingBox.add(String.valueOf(yMax));
            }
        }
    } catch (Exception exc) {
        // Show exception in log and create ServerGeoException which is
        // captured on controller and puts message to ajax response
        logger.error("Exception on getWMSLayersBoundingBox", exc);
        throw new ServerGeoException();
    }
    return boundingBox;
}

From source file:org.esa.nest.gpf.ASARCalibrator.java

/**
 * Get calibration factors from Metadata for each band in the product.
 * Here it is assumed that the calibration factor values do not change in case that there are
 * multiple records in the Main Processing Parameters data set.
 *///from   w  w  w.ja  v a 2  s.c  o m
private void getCalibrationFactorFromMetadata() {

    final MetadataElement origRoot = AbstractMetadata.getOriginalProductMetadata(sourceProduct);
    MetadataElement ads;
    if (numMPPRecords == 1) {
        ads = origRoot.getElement("MAIN_PROCESSING_PARAMS_ADS");
    } else {
        ads = origRoot.getElement("MAIN_PROCESSING_PARAMS_ADS").getElement("MAIN_PROCESSING_PARAMS_ADS.1");
        if (ads == null)
            ads = origRoot.getElement("MAIN_PROCESSING_PARAMS_ADS");
    }

    if (ads == null) {
        throw new OperatorException("MAIN_PROCESSING_PARAMS_ADS not found");
    }

    MetadataAttribute calibrationFactorsAttr = ads
            .getAttribute("ASAR_Main_ADSR.sd/calibration_factors.1.ext_cal_fact");

    if (calibrationFactorsAttr == null) {
        throw new OperatorException("calibration_factors.1.ext_cal_fact not found");
    }

    newCalibrationConstant[0] = (double) calibrationFactorsAttr.getData().getElemFloat();

    calibrationFactorsAttr = ads.getAttribute("ASAR_Main_ADSR.sd/calibration_factors.2.ext_cal_fact");

    if (calibrationFactorsAttr == null) {
        throw new OperatorException("calibration_factors.2.ext_cal_fact not found");
    }

    newCalibrationConstant[1] = (double) calibrationFactorsAttr.getData().getElemFloat();

    if (Double.compare(newCalibrationConstant[0], 0.0) == 0
            && Double.compare(newCalibrationConstant[1], 0.0) == 0) {
        throw new OperatorException("Calibration factors in metadata are zero");
    }
    //System.out.println("calibration factor for band 1 is " + calibrationFactor[0]);
    //System.out.println("calibration factor for band 2 is " + calibrationFactor[1]);
}

From source file:com.unboundid.scim2.common.utils.JsonUtils.java

/**
 * Compares two JsonNodes for order. Nodes containing datetime and numerical
 * values are ordered accordingly. Otherwise, the values' string
 * representation will be compared lexicographically.
 *
 * @param n1 the first node to be compared.
 * @param n2 the second node to be compared.
 * @param attributeDefinition The attribute definition of the attribute
 *                            whose values to compare or {@code null} to
 *                            compare string values using case insensitive
 *                            matching./*from w  w  w.  ja  v a 2  s . c  om*/
 * @return a negative integer, zero, or a positive integer as the
 *         first argument is less than, equal to, or greater than the second.
 */
public static int compareTo(final JsonNode n1, final JsonNode n2,
        final AttributeDefinition attributeDefinition) {
    if (n1.isTextual() && n2.isTextual()) {
        Date d1 = dateValue(n1);
        Date d2 = dateValue(n2);
        if (d1 != null && d2 != null) {
            return d1.compareTo(d2);
        } else {
            if (attributeDefinition != null && attributeDefinition.getType() == AttributeDefinition.Type.STRING
                    && attributeDefinition.isCaseExact()) {
                return n1.textValue().compareTo(n2.textValue());
            }
            return StaticUtils.toLowerCase(n1.textValue()).compareTo(StaticUtils.toLowerCase(n2.textValue()));
        }
    }

    if (n1.isNumber() && n2.isNumber()) {
        if (n1.isBigDecimal() || n2.isBigDecimal()) {
            return n1.decimalValue().compareTo(n2.decimalValue());
        }

        if (n1.isFloatingPointNumber() || n2.isFloatingPointNumber()) {
            return Double.compare(n1.doubleValue(), n2.doubleValue());
        }

        if (n1.isBigInteger() || n2.isBigInteger()) {
            return n1.bigIntegerValue().compareTo(n2.bigIntegerValue());
        }

        return Long.compare(n1.longValue(), n2.longValue());
    }

    // Compare everything else lexicographically
    return n1.asText().compareTo(n2.asText());
}

From source file:com.joptimizer.optimizers.LPStandardConverterTest.java

/**
 * Standardization (to the strictly standard form) of a problem on the form:
 * min(c) s.t./* w  w w  . ja va  2 s . com*/
 * A.x = b
 * lb <= x <= ub
 * 
 * This is the presolved (with JOptimizer) pilot4 netlib problem.
 * @TODO: the strict conversion is net yet ready.
 */
public void xxxtestCAbLbUb5Strict() throws Exception {
    log.debug("testCAbLbUb5Strict");

    String problemId = "5";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt");
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0];
    double expectedTol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "tolerance" + problemId + ".txt")[0];

    int nOfSlackVariables = 0;
    for (int i = 0; i < c.length; i++) {
        double lbi = lb[i];
        int lbCompare = Double.compare(lbi, 0.);
        if (lbCompare != 0 && !Double.isNaN(lbi)) {
            nOfSlackVariables++;
        }
        if (!Double.isNaN(ub[i])) {
            nOfSlackVariables++;
        }
    }
    int expectedS = nOfSlackVariables;

    //standard form conversion
    boolean strictlyStandardForm = true;
    LPStandardConverter lpConverter = new LPStandardConverter(strictlyStandardForm);
    lpConverter.toStandardForm(c, null, null, A, b, lb, ub);

    int n = lpConverter.getStandardN();
    int s = lpConverter.getStandardS();
    c = lpConverter.getStandardC().toArray();
    A = lpConverter.getStandardA().toArray();
    b = lpConverter.getStandardB().toArray();
    lb = lpConverter.getStandardLB().toArray();
    ub = (lpConverter.getStandardUB() == null) ? null : ub;
    log.debug("n : " + n);
    log.debug("s : " + s);
    log.debug("c : " + ArrayUtils.toString(c));
    log.debug("A : " + ArrayUtils.toString(A));
    log.debug("b : " + ArrayUtils.toString(b));
    log.debug("lb : " + ArrayUtils.toString(lb));
    //log.debug("ub : " + ArrayUtils.toString(ub));

    //check consistency
    assertEquals(expectedS, s);
    assertEquals(lb.length, n);
    assertTrue(ub == null);

    //check constraints
    RealMatrix AStandard = new Array2DRowRealMatrix(A);
    RealVector bStandard = new ArrayRealVector(b);
    double[] expectedStandardSol = lpConverter.getStandardComponents(expectedSol);
    RealVector expectedStandardSolVector = new ArrayRealVector(expectedStandardSol);

    for (int i = 0; i < expectedStandardSolVector.getDimension(); i++) {
        assertTrue(expectedStandardSolVector.getEntry(i) + 1.E-8 >= 0.);
    }

    RealVector Axmb = AStandard.operate(expectedStandardSolVector).subtract(bStandard);
    for (int i = 0; i < Axmb.getDimension(); i++) {
        assertEquals(0., Axmb.getEntry(i), expectedTol);
    }

    Utils.writeDoubleArrayToFile(new double[] { s },
            "target" + File.separator + "standardS_" + problemId + ".txt");
    Utils.writeDoubleArrayToFile(c, "target" + File.separator + "standardC_" + problemId + ".txt");
    Utils.writeDoubleMatrixToFile(A, "target" + File.separator + "standardA_" + problemId + ".csv");
    Utils.writeDoubleArrayToFile(b, "target" + File.separator + "standardB_" + problemId + ".txt");
    Utils.writeDoubleArrayToFile(lb, "target" + File.separator + "standardLB_" + problemId + ".txt");
    //ub is null Utils.writeDoubleArrayToFile(ub, "target" + File.separator   + "standardUB_"+problemId+".txt");
}

From source file:com.att.aro.core.util.Util.java

public static Comparator<String> getFloatSorter() {
    if (floatValComparator == null) {
        floatValComparator = new Comparator<String>() {
            @Override//from   www .  j  a v a2s .c  o  m
            public int compare(String o1, String o2) {
                return Double.compare(Double.parseDouble(o1), Double.parseDouble(o2));
            }
        };
    }
    return floatValComparator;
}

From source file:gobblin.compaction.mapreduce.MRCompactorJobRunner.java

@Override
public int compareTo(MRCompactorJobRunner o) {
    return Double.compare(o.dataset.priority(), this.dataset.priority());
}

From source file:ml.shifu.shifu.core.processor.StatsModelProcessor.java

/**
 * Compute correlation value according to correlation statistics from correlation MR job.
 * /*from  w  w  w.  jav a 2  s. c o  m*/
 * @param corrMap
 *            CorrelationWritable map read from MR job output file
 * @throws IOException
 *             any IOException to write correlation value to csv file.
 */
private void computeCorrValue(SortedMap<Integer, CorrelationWritable> corrMap) throws IOException {
    boolean hasCandidates = CommonUtils.hasCandidateColumns(this.columnConfigList);
    String localCorrelationCsv = super.pathFinder.getLocalCorrelationCsvPath();
    ShifuFileUtils.createFileIfNotExists(localCorrelationCsv, SourceType.LOCAL);
    BufferedWriter writer = null;
    Map<Integer, double[]> finalCorrMap = new HashMap<>();
    try {
        writer = ShifuFileUtils.getWriter(localCorrelationCsv, SourceType.LOCAL);
        writer.write(getColumnIndexes());
        writer.newLine();
        writer.write(getColumnNames());
        writer.newLine();

        for (Entry<Integer, CorrelationWritable> entry : corrMap.entrySet()) {
            ColumnConfig xColumnConfig = this.columnConfigList.get(entry.getKey());
            if (xColumnConfig.getColumnFlag() == ColumnFlag.Meta
                    || (hasCandidates && !ColumnFlag.Candidate.equals(xColumnConfig.getColumnFlag()))) {
                continue;
            }
            CorrelationWritable xCw = entry.getValue();
            double[] corrArray = new double[this.columnConfigList.size()];
            for (int i = 0; i < corrArray.length; i++) {
                ColumnConfig yColumnConfig = this.columnConfigList.get(i);
                if (yColumnConfig.getColumnFlag() == ColumnFlag.Meta) {
                    continue;
                }
                if (entry.getKey() > i) {
                    double[] reverseDoubleArray = finalCorrMap.get(i);
                    if (reverseDoubleArray != null) {
                        corrArray[i] = reverseDoubleArray[entry.getKey()];
                    } else {
                        corrArray[i] = 0d;
                    }
                    // not compute all, only up-right matrix are computed, such case, just get [i, j] from [j, i]
                    continue;
                }

                double numerator = xCw.getAdjustCount()[i] * xCw.getXySum()[i]
                        - xCw.getAdjustSumX()[i] * xCw.getAdjustSumY()[i];
                double denominator1 = Math.sqrt(xCw.getAdjustCount()[i] * xCw.getXxSum()[i]
                        - xCw.getAdjustSumX()[i] * xCw.getAdjustSumX()[i]);
                double denominator2 = Math.sqrt(xCw.getAdjustCount()[i] * xCw.getYySum()[i]
                        - xCw.getAdjustSumY()[i] * xCw.getAdjustSumY()[i]);
                if (Double.compare(denominator1, Double.valueOf(0d)) == 0
                        || Double.compare(denominator2, Double.valueOf(0d)) == 0) {
                    corrArray[i] = 0d;
                } else {
                    corrArray[i] = numerator / (denominator1 * denominator2);
                }

                // if(corrArray[i] > 1.0005d || (entry.getKey() == 54 && i == 2124)) {
                if (corrArray[i] > 1.0005d) {
                    log.warn("Correlation value for columns {} {} > 1, below is debug info.", entry.getKey(),
                            i);
                    log.warn(
                            "DEBUG: corr {}, value > 1d, numerator " + numerator + " denominator1 "
                                    + denominator1 + " denominator2 " + denominator2 + " {}, {}",
                            numerator / (denominator1 * denominator2), entry.getKey(), i);
                    log.warn(
                            "DEBUG: xCw.getAdjustCount()[i] * xCw.getXySum()[i] - xCw.getAdjustSumX()[i]  * xCw.getAdjustSumY()[i] : {} * {} - {} * {} ",
                            xCw.getAdjustCount()[i], xCw.getXySum()[i], xCw.getAdjustSumX()[i],
                            xCw.getAdjustSumY()[i]);
                    log.warn(
                            "DEBUG: xCw.getAdjustCount()[i] * xCw.getXxSum()[i] - xCw.getAdjustSumX()[i] * xCw.getAdjustSumX()[i] : {} * {} - {} * {} ",
                            xCw.getAdjustCount()[i], xCw.getXxSum()[i], xCw.getAdjustSumX()[i],
                            xCw.getAdjustSumX()[i]);
                    log.warn(
                            "DEBUG: xCw.getAdjustCount()[i] * xCw.getYySum()[i] - xCw.getAdjustSumY()[i] * xCw.getAdjustSumY()[i] : {} * {} -  {} * {} ",
                            xCw.getAdjustCount()[i], xCw.getYySum()[i], xCw.getAdjustSumY()[i],
                            xCw.getAdjustSumY()[i]);
                }

            }

            // put to current map
            finalCorrMap.put(entry.getKey(), corrArray);

            // write to csv
            String corrStr = Arrays.toString(corrArray);
            String adjustCorrStr = corrStr.substring(1, corrStr.length() - 1);
            writer.write(entry.getKey() + "," + this.columnConfigList.get(entry.getKey()).getColumnName() + ","
                    + adjustCorrStr);
            writer.newLine();
        }
    } finally {
        IOUtils.closeQuietly(writer);
    }
}