List of usage examples for java.util TreeMap lowerEntry
public Map.Entry<K, V> lowerEntry(K key)
From source file:Main.java
public static void main(String[] args) { TreeMap<Integer, String> treemap = new TreeMap<Integer, String>(); // populating tree map treemap.put(2, "two"); treemap.put(1, "one"); treemap.put(3, "three"); treemap.put(6, "six"); treemap.put(5, "from java2s.com"); // getting lower entry System.out.println("Checking lower entry"); System.out.println("Value is: " + treemap.lowerEntry(5)); }
From source file:edu.utexas.cs.tactex.interfaces.TariffOptimizerBase.java
protected TariffSpecification extractBestTariffSpec(TreeMap<Double, TariffSpecification> sortedTariffs) { Entry<Double, TariffSpecification> bestEntry = sortedTariffs.lastEntry(); while (bestEntry != null && bestEntry.getValue() == null) { bestEntry = sortedTariffs.lowerEntry(bestEntry.getKey()); }//w w w . ja v a 2 s . c o m if (bestEntry != null) { return bestEntry.getValue(); } else { return null; } }
From source file:edu.utexas.cs.tactex.utilityestimation.UtilityArchitectureActionGenerator.java
private List<TariffMessage> selectBestActions(int currentTimeslot, TreeMap<Double, TariffMessage> sortedTariffActions, Set<TariffSpecification> myExistingTariffs) { Double bestUtil = sortedTariffActions.lastEntry().getKey(); TariffMessage tariffAction = sortedTariffActions.lastEntry().getValue(); if (currentTimeslot < 360 + 6) { if (null == tariffAction) { log.warn("Force publishing before ts 366"); Entry<Double, TariffMessage> lowerEntry = sortedTariffActions .lowerEntry(sortedTariffActions.lastEntry().getKey()); while (lowerEntry.getValue().getClass() != TariffSpecification.class) { lowerEntry = sortedTariffActions.lowerEntry(lowerEntry.getKey()); }/*from w w w. j a v a 2 s .c o m*/ bestUtil = lowerEntry.getKey(); tariffAction = lowerEntry.getValue(); } } // currently returning just one - the estimated best List<TariffMessage> tariffActions = new ArrayList<TariffMessage>(); if (tariffAction != null) { // null is no-op tariffActions.add(tariffAction); } return tariffActions; }
From source file:edu.utexas.cs.tactex.utilityestimation.UtilityArchitectureActionGenerator.java
private TariffSpecification tieBreakForLowerRates(TreeMap<Double, TariffSpecification> sortedTariffs, Double bestUtil, TariffSpecification specToAdd, Set<TariffSpecification> myExistingTariffs) { log.info("specToAdd before tie-break: util = " + bestUtil + " spec = " + specToAdd); final double UTILITY_TOLERANCE = 0.97; double bestAvgRate = avgRate(specToAdd, myExistingTariffs); double currentUtil = bestUtil; // used just for printing for (Entry<Double, TariffSpecification> candidateEntry = sortedTariffs.lowerEntry(bestUtil); // initialize to next null != candidateEntry && candidateEntry.getKey() > bestUtil * UTILITY_TOLERANCE; // currently within 97% of best candidateEntry = sortedTariffs.lowerEntry(candidateEntry.getKey())) { // advance to lower double currentAvgRate = avgRate(candidateEntry.getValue(), myExistingTariffs); if (currentAvgRate > bestAvgRate) { // i.e. more value for customer (since rates are negative) bestAvgRate = currentAvgRate; specToAdd = candidateEntry.getValue(); currentUtil = candidateEntry.getKey(); // used just for printing }//from ww w . j a v a 2s .c o m } log.info("specToAdd after tie-break: util = " + currentUtil + " spec = " + specToAdd); return specToAdd; }
From source file:edu.utexas.cs.tactex.tariffoptimization.TariffOptimizerBinaryOneShot.java
private TreeMap<Double, TariffSpecification> binarySearchOptimize(List<TariffSpecification> suggestedSpecs, HashMap<TariffSpecification, HashMap<CustomerInfo, Integer>> tariffSubscriptions, List<TariffSpecification> competingTariffs, CostCurvesPredictor costCurvesPredictor, int currentTimeslot, Broker me, HashMap<CustomerInfo, HashMap<TariffSpecification, ShiftedEnergyData>> customer2ShiftedEnergy, HashMap<CustomerInfo, ArrayRealVector> customer2NonShiftedEnergy, HashMap<CustomerInfo, HashMap<TariffSpecification, Double>> customer2RelevantTariffCharges) { TreeMap<Double, TariffSpecification> result = new TreeMap<Double, TariffSpecification>(); // a value of null means no-op ArrayList<TariffSpecification> consideredTariffActions = new ArrayList<TariffSpecification>(); consideredTariffActions.add(null);// w w w . j av a2 s .co m TreeMap<Double, TariffSpecification> sortedTariffs = utilityEstimator.estimateUtilities( consideredTariffActions, tariffSubscriptions, competingTariffs, customer2RelevantTariffCharges, customer2ShiftedEnergy, customer2NonShiftedEnergy, marketPredictionManager, costCurvesPredictor, currentTimeslot, me); result.putAll(sortedTariffs); // here do the binary search // // initialize with edges and middle TreeMap<Double, Integer> utilToIndex = new TreeMap<Double, Integer>(); int numTariffs = suggestedSpecs.size(); int[] initialIndexes = { 0, numTariffs / 2, numTariffs - 1 }; for (int index : initialIndexes) { evaluateAndRecord(index, utilToIndex, result, suggestedSpecs, consideredTariffActions, tariffSubscriptions, competingTariffs, costCurvesPredictor, currentTimeslot, me, customer2ShiftedEnergy, customer2NonShiftedEnergy, customer2RelevantTariffCharges); } int bestIndex = utilToIndex.lastEntry().getValue(); int secondBestIndex = utilToIndex.lowerEntry(utilToIndex.lastKey()).getValue(); // // binary search while (Math.abs(secondBestIndex - bestIndex) >= 2) { //log.info("evaluating, bestIndex=" + bestIndex + ", secondBestIndex=" + secondBestIndex); int midIndex = (secondBestIndex + bestIndex) / 2; evaluateAndRecord(midIndex, utilToIndex, result, suggestedSpecs, consideredTariffActions, tariffSubscriptions, competingTariffs, costCurvesPredictor, currentTimeslot, me, customer2ShiftedEnergy, customer2NonShiftedEnergy, customer2RelevantTariffCharges); bestIndex = utilToIndex.lastEntry().getValue(); secondBestIndex = utilToIndex.lowerEntry(utilToIndex.lastKey()).getValue(); // TODO: handling a non-convex case (how come happens?) if (midIndex != bestIndex && midIndex != secondBestIndex) { log.warn("non-convex utility values found during binary search. breaking..."); break; } } //log.info("evaluating, bestIndex=" + bestIndex + ", secondBestIndex=" + secondBestIndex); return result; }