Example usage for java.util SortedMap entrySet

List of usage examples for java.util SortedMap entrySet

Introduction

In this page you can find the example usage for java.util SortedMap entrySet.

Prototype

Set<Map.Entry<K, V>> entrySet();

Source Link

Document

Returns a Set view of the mappings contained in this map.

Usage

From source file:at.illecker.hama.rootbeer.examples.matrixmultiplication.compositeinput.cpu.MatrixMultiplicationBSPCpu.java

@Override
public void cleanup(BSPPeer<IntWritable, TupleWritable, IntWritable, VectorWritable, MatrixRowMessage> peer)
        throws IOException {

    // MasterTask accumulates result
    if (peer.getPeerName().equals(masterTask)) {

        // SortedMap because the final matrix rows should be in order
        SortedMap<Integer, Vector> accumlatedRows = new TreeMap<Integer, Vector>();
        MatrixRowMessage currentMatrixRowMessage = null;

        // Collect messages
        while ((currentMatrixRowMessage = peer.getCurrentMessage()) != null) {
            int rowIndex = currentMatrixRowMessage.getRowIndex();
            Vector rowValues = currentMatrixRowMessage.getRowValues().get();

            if (isDebuggingEnabled) {
                logger.writeChars("bsp,gotMsg,key=" + rowIndex + ",value=" + rowValues.toString() + "\n");
            }//w w w. ja  v  a2  s . c o  m

            if (accumlatedRows.containsKey(rowIndex)) {
                accumlatedRows.get(rowIndex).assign(rowValues, Functions.PLUS);
            } else {
                accumlatedRows.put(rowIndex, new RandomAccessSparseVector(rowValues));
            }
        }

        // Write accumulated results
        for (Map.Entry<Integer, Vector> row : accumlatedRows.entrySet()) {
            if (isDebuggingEnabled) {
                logger.writeChars(
                        "bsp,write,key=" + row.getKey() + ",value=" + row.getValue().toString() + "\n");
            }
            peer.write(new IntWritable(row.getKey()), new VectorWritable(row.getValue()));
        }

    }
}

From source file:hivemall.topicmodel.OnlineLDAModel.java

@Nonnull
public SortedMap<Float, List<String>> getTopicWords(@Nonnegative final int k, @Nonnegative int topN) {
    double lambdaSum = 0.d;
    final SortedMap<Float, List<String>> sortedLambda = new TreeMap<Float, List<String>>(
            Collections.reverseOrder());

    for (Map.Entry<String, float[]> e : _lambda.entrySet()) {
        final float lambda_k = e.getValue()[k];
        lambdaSum += lambda_k;/*from w  w  w.  j a  va2 s  .c  o m*/

        List<String> labels = sortedLambda.get(lambda_k);
        if (labels == null) {
            labels = new ArrayList<String>();
            sortedLambda.put(lambda_k, labels);
        }
        labels.add(e.getKey());
    }

    final SortedMap<Float, List<String>> ret = new TreeMap<Float, List<String>>(Collections.reverseOrder());

    topN = Math.min(topN, _lambda.keySet().size());
    int tt = 0;
    for (Map.Entry<Float, List<String>> e : sortedLambda.entrySet()) {
        float key = (float) (e.getKey().floatValue() / lambdaSum);
        ret.put(Float.valueOf(key), e.getValue());

        if (++tt == topN) {
            break;
        }
    }

    return ret;
}

From source file:org.optaplanner.benchmark.impl.DefaultPlannerBenchmark.java

private List<List<SolverBenchmark>> createSameRankingListList(
        List<SolverBenchmark> rankableSolverBenchmarkList) {
    List<List<SolverBenchmark>> sameRankingListList = new ArrayList<List<SolverBenchmark>>(
            rankableSolverBenchmarkList.size());
    if (solverBenchmarkRankingComparator != null) {
        Comparator<SolverBenchmark> comparator = Collections.reverseOrder(solverBenchmarkRankingComparator);
        Collections.sort(rankableSolverBenchmarkList, comparator);
        List<SolverBenchmark> sameRankingList = null;
        SolverBenchmark previousSolverBenchmark = null;
        for (SolverBenchmark solverBenchmark : rankableSolverBenchmarkList) {
            if (previousSolverBenchmark == null
                    || comparator.compare(previousSolverBenchmark, solverBenchmark) != 0) {
                // New rank
                sameRankingList = new ArrayList<SolverBenchmark>();
                sameRankingListList.add(sameRankingList);
            }/*from  w w w . ja va  2  s. co  m*/
            sameRankingList.add(solverBenchmark);
            previousSolverBenchmark = solverBenchmark;
        }
    } else if (solverBenchmarkRankingWeightFactory != null) {
        SortedMap<Comparable, List<SolverBenchmark>> rankedMap = new TreeMap<Comparable, List<SolverBenchmark>>(
                new ReverseComparator());
        for (SolverBenchmark solverBenchmark : rankableSolverBenchmarkList) {
            Comparable rankingWeight = solverBenchmarkRankingWeightFactory
                    .createRankingWeight(rankableSolverBenchmarkList, solverBenchmark);
            List<SolverBenchmark> sameRankingList = rankedMap.get(rankingWeight);
            if (sameRankingList == null) {
                sameRankingList = new ArrayList<SolverBenchmark>();
                rankedMap.put(rankingWeight, sameRankingList);
            }
            sameRankingList.add(solverBenchmark);
        }
        for (Map.Entry<Comparable, List<SolverBenchmark>> entry : rankedMap.entrySet()) {
            sameRankingListList.add(entry.getValue());
        }
    } else {
        throw new IllegalStateException("Ranking is impossible"
                + " because solverBenchmarkRankingComparator and solverBenchmarkRankingWeightFactory are null.");
    }
    return sameRankingListList;
}

From source file:com.alibaba.cobar.client.CobarSqlMapClientTemplate.java

public List<Object> executeInConcurrency(SqlMapClientCallback action, SortedMap<String, DataSource> dsMap) {
    List<ConcurrentRequest> requests = new ArrayList<ConcurrentRequest>();

    for (Map.Entry<String, DataSource> entry : dsMap.entrySet()) {
        ConcurrentRequest request = new ConcurrentRequest();
        request.setAction(action);/*from   w  ww. j a  va 2  s  . c  o  m*/
        request.setDataSource(entry.getValue());
        request.setExecutor(getDataSourceSpecificExecutors().get(entry.getKey()));
        requests.add(request);
    }

    List<Object> results = getConcurrentRequestProcessor().process(requests);
    return results;
}

From source file:uk.ac.ucl.excites.sapelli.shared.io.text.CharsetHelpers.java

/**
 * Parses all .charsetinfo files in the input folder and works out which is the 
 * maximum "maxBytesPerChar" value for each known Charset across all input files.
 * If this summarised information is in anyway different from the information in
 * {@link #CharsetMaxMaxBytesPerCharBundle} (or if {@code force} is {@code true})
 * then a new CharsetMaxMaxBytesPerChar.properties is created (or overwritten!)
 * in the output folder.//from w w w.ja v  a 2 s . c  o m
 * 
 * This method is called from a Groovy script in the pom.xml of the Sapelli Library.
 * 
 * @param inputFolderPath path of a directory containing CharsetInfo files (with *.charsetinfo extension!) to process
 * @param outputFolderPath path of a (resource) directory in which to create the new/updated CharsetMaxMaxBytesPerChar.properties file
 * @param force when {@code true} a new CharsetMaxMaxBytesPerChar.properties file will always be generated
 * @return whether or not a new or updated CharsetMaxMaxBytesPerChar.properties file was created
 * @throws IOException
 */
static public final boolean GeneratePropertiesFile(String inputFolderPath, String outputFolderPath,
        boolean force) throws IOException {
    File inputFolder = new File(inputFolderPath);
    if (!inputFolder.isDirectory() || !inputFolder.exists())
        throw new IllegalArgumentException("Please provide a valid and existing folder!");

    SortedMap<String, String> fileHeaders = new TreeMap<String, String>();
    SortedMap<String, Float> tempCN2MBPC = new TreeMap<String, Float>();

    // Process all charsetinfo files in the folder:
    for (File charsetFile : inputFolder
            .listFiles((FileFilter) new WildcardFileFilter("*." + CHARSETINFO_FILE_EXTENSION))) {
        try (UnicodeBOMInputStream input = new UnicodeBOMInputStream(new FileInputStream(charsetFile));
                BufferedReader reader = new BufferedReader(input.getReader(CHARSETINFO_FILE_CHARSET))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.isEmpty())
                    continue; // skip blank lines
                if (line.charAt(0) == COMMENT_LINE_MARKER) {
                    if (!fileHeaders.containsKey(charsetFile.getName()))
                        fileHeaders.put(charsetFile.getName(), line.substring(1).trim());
                    continue; // skip comment lines
                }
                // Parse Charset info:
                CharsetInfo csInfo = CharsetInfo.Parse(line);
                // Store/replace max maxBytesPerChar value:
                if (csInfo.maxBytesPerChar > 0.0f && (!tempCN2MBPC.containsKey(csInfo.name)
                        || tempCN2MBPC.get(csInfo.name).floatValue() < csInfo.maxBytesPerChar))
                    tempCN2MBPC.put(csInfo.name, csInfo.maxBytesPerChar);
            }
            if (!fileHeaders.containsKey(charsetFile.getName()))
                fileHeaders.put(charsetFile.getName(), "");
        }
    }

    // Compare information loaded from charsetinfo files with that in the resource bundle:
    boolean different = force || CharsetMaxMaxBytesPerCharBundle == null
            || tempCN2MBPC.size() != getNumberOfKnownCharsets();
    if (!different)
        for (Map.Entry<String, Float> mapping : tempCN2MBPC.entrySet())
            try {
                if (!Float.valueOf(CharsetMaxMaxBytesPerCharBundle.getString(mapping.getKey()))
                        .equals(mapping.getValue())) // getString throws Exception if key is not found
                    throw new Exception("maxBytesPerChar mismatch!");
            } catch (Exception e) {
                different = true;
                break;
            }

    // If the information is different...
    if (different) { // Write new properties file (in package-specific subfolder of the given output folder):
        File outputFolder = new File(new File(outputFolderPath),
                ClassHelpers.classPackageAsResourcePath(CharsetHelpers.class));
        FileHelpers.createDirectory(outputFolder);
        try (FileOutputStream fos = new FileOutputStream(
                new File(outputFolder, CMMBPC_PROPERTIES_FILE_NAME + "." + PROPERTIES_FILE_EXTENSION));
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(fos, PROPERTIES_FILE_CHARSET))) {
            // Header:
            writer.write(COMMENT_LINE_MARKER + " Generated on "
                    + TimeUtils.getISOTimestamp(System.currentTimeMillis(), false) + " from input files:"
                    + EOL);
            for (Map.Entry<String, String> fileAndHeader : fileHeaders.entrySet()) {
                writer.write(COMMENT_LINE_MARKER + "\t- " + fileAndHeader.getKey() + ":" + EOL);
                writer.write(COMMENT_LINE_MARKER + "\t\t" + fileAndHeader.getValue() + EOL);
            }
            writer.write(EOL);
            // Body:
            for (Map.Entry<String, Float> mapping : tempCN2MBPC.entrySet())
                writer.write(mapping.getKey() + "=" + mapping.getValue().toString() + EOL);
        }
    }

    return different;
}

From source file:net.sourceforge.seqware.pipeline.plugins.checkdb.plugins.WorkflowRunConventionsPlugin.java

@Override
public void check(SelectQueryRunner qRunner, SortedMap<Level, Set<String>> result) throws SQLException {
    try {/*from  w w  w.  j a v a 2s . co  m*/
        /**
         * May not be true for downsteam workflow runs
        * List<Integer> executeQuery = qRunner.executeQuery("select sw_accession from workflow_run WHERE workflow_run_id NOT IN (select workflow_run_id FROM ius_workflow_runs);", new ColumnListHandler<Integer>());
        * CheckDB.processOutput(result, Level.TRIVIAL,  "Workflow runs not connected to an IUS via ius_workflow_runs: " , executeQuery);
        **/
        // workflow runs not connected to a study
        String query = IOUtils.toString(
                AttributePlugin.class.getResourceAsStream("workflow_runs_not_connected_to_study.sql"));
        List<Object[]> workflow_run_study_pairs = qRunner.executeQuery(query, new ArrayListHandler());

        List<Integer> unreachableByStudy = new ArrayList<>();
        // number studies -> workflow runs
        SortedMap<Integer, SortedSet<Integer>> reachableByMultipleStudies = new TreeMap<>();

        for (Object[] pair : workflow_run_study_pairs) {
            int studyCount = Integer.valueOf(pair[1].toString());
            if (pair[0] == null) {
                continue;
            }
            int sw_accession = Integer.valueOf(pair[0].toString());
            if (studyCount == 0) {
                unreachableByStudy.add(sw_accession);
            } else if (studyCount > 1) {
                if (!reachableByMultipleStudies.containsKey(studyCount)) {
                    reachableByMultipleStudies.put(studyCount, new TreeSet<Integer>());
                }
                reachableByMultipleStudies.get(studyCount).add(sw_accession);
            }
        }
        CheckDB.processOutput(result, Level.SEVERE, "'Completed' Workflow runs not reachable by studies: ",
                unreachableByStudy);
        // workflow runs connected to more than one study
        if (reachableByMultipleStudies.size() > 0) {
            for (Entry<Integer, SortedSet<Integer>> e : reachableByMultipleStudies.entrySet()) {
                CheckDB.processOutput(result, Level.WARNING,
                        "'Completed' Workflow runs reachable by " + e.getKey() + " studies: ",
                        new ArrayList<>(e.getValue()));
            }
        }
        query = IOUtils.toString(
                AttributePlugin.class.getResourceAsStream("workflow_runs_not_connected_in_hierarchy.sql"));
        List<Integer> executeQuery = qRunner.executeQuery(query, new ColumnListHandler<Integer>());
        CheckDB.processOutput(result, Level.SEVERE,
                "'Completed' Workflow runs reachable by ius_workflow_runs but not via the processing_hierarchy: ",
                executeQuery);

        query = IOUtils.toString(AttributePlugin.class.getResourceAsStream("new_input_files_versus_old.sql"));
        executeQuery = qRunner.executeQuery(query, new ColumnListHandler<Integer>());
        CheckDB.processOutput(result, Level.TRIVIAL,
                "Workflow runs with input files via workflow_run_input_files but not via the processing hierarchy: ",
                executeQuery);

        query = IOUtils.toString(AttributePlugin.class.getResourceAsStream("old_input_files_versus_new.sql"));
        executeQuery = qRunner.executeQuery(query, new ColumnListHandler<Integer>());
        CheckDB.processOutput(result, Level.TRIVIAL,
                "Workflow runs with input files via the processing hierarchy but not via workflow_run_input_files: ",
                executeQuery);

    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }

}

From source file:org.kurento.rabbitmq.RabbitTemplate.java

/**
 * Gets unconfirmed correlatiom data older than age and removes them.
 * /*from  w  ww . j ava  2  s  . co  m*/
 * @param age
 *            in millseconds
 * @return the collection of correlation data for which confirms have not
 *         been received.
 */
public Collection<CorrelationData> getUnconfirmed(long age) {
    Set<CorrelationData> unconfirmed = new HashSet<CorrelationData>();
    synchronized (this.pendingConfirms) {
        long threshold = System.currentTimeMillis() - age;
        for (Entry<Object, SortedMap<Long, PendingConfirm>> channelPendingConfirmEntry : this.pendingConfirms
                .entrySet()) {
            SortedMap<Long, PendingConfirm> channelPendingConfirms = channelPendingConfirmEntry.getValue();
            Iterator<Entry<Long, PendingConfirm>> iterator = channelPendingConfirms.entrySet().iterator();
            PendingConfirm pendingConfirm;
            while (iterator.hasNext()) {
                pendingConfirm = iterator.next().getValue();
                if (pendingConfirm.getTimestamp() < threshold) {
                    unconfirmed.add(pendingConfirm.getCorrelationData());
                    iterator.remove();
                } else {
                    break;
                }
            }
        }
    }
    return unconfirmed.size() > 0 ? unconfirmed : null;
}

From source file:playground.sergioo.workplaceCapacities2012.MainWorkplaceCapacities.java

private static Map<Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>, Tuple<Boolean, Double>> calculateAreaStopTravelTimes(
        SortedMap<String, Coord> stopsBase, Map<Id<TransitStopFacility>, Double> stopsCapacities,
        Network network) throws BadStopException, IOException, InstantiationException, IllegalAccessException,
        ClassNotFoundException, SQLException, NoConnectionException {
    List<Map<String, Id<Link>>> linksStops;
    try {/*from w  w w  . java 2s . c o m*/
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(LINKS_MAP_FILE));
        linksStops = (List<Map<String, Id<Link>>>) ois.readObject();
        ois.close();
    } catch (EOFException e) {
        linksStops = new ArrayList<Map<String, Id<Link>>>();
        for (int n = 0; n < NUM_NEAR; n++) {
            linksStops.add(new HashMap<String, Id<Link>>());
            for (Entry<String, Coord> stopBase : stopsBase.entrySet()) {
                Id<Link> nearest = network.getLinks().values().iterator().next().getId();
                double nearestDistance = CoordUtils.calcDistance(network.getLinks().get(nearest).getCoord(),
                        stopBase.getValue());
                for (Link link : network.getLinks().values())
                    if (link.getAllowedModes().contains("car")) {
                        boolean selected = false;
                        for (int p = 0; p < n; p++)
                            if (linksStops.get(p).get(stopBase.getKey()).equals(link.getId()))
                                selected = true;
                        if (!selected && CoordUtils.calcDistance(link.getToNode().getCoord(),
                                stopBase.getValue()) < nearestDistance) {
                            nearest = link.getId();
                            nearestDistance = CoordUtils.calcDistance(
                                    network.getLinks().get(nearest).getCoord(), stopBase.getValue());
                        }
                    }
                linksStops.get(n).put(stopBase.getKey(), nearest);
            }
        }
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(LINKS_MAP_FILE));
        oos.writeObject(linksStops);
        oos.close();
    }
    //Compute stops facilities weights
    TravelDisutility travelMinCost = new TravelDisutility() {
        @Override
        public double getLinkTravelDisutility(final Link link, final double time, final Person person,
                final Vehicle vehicle) {
            return getLinkMinimumTravelDisutility(link);
        }

        @Override
        public double getLinkMinimumTravelDisutility(Link link) {
            return link.getLength() / WALKING_SPEED;
        }
    };
    TravelTime timeFunction = new TravelTime() {

        @Override
        public double getLinkTravelTime(Link link, double time, Person person, Vehicle vehicle) {
            return link.getLength() / WALKING_SPEED;
        }
    };
    PreProcessLandmarks preProcessData = new PreProcessLandmarks(travelMinCost);
    preProcessData.run(network);
    AStarLandmarks aStarLandmarks = new AStarLandmarks(network, preProcessData, timeFunction);
    Map<Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>, Tuple<Boolean, Double>> weights = new HashMap<Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>, Tuple<Boolean, Double>>();
    Collection<Tuple<Id<TransitStopFacility>, Integer>> removeStops = new ArrayList<Tuple<Id<TransitStopFacility>, Integer>>();
    travelTimes = new ArrayList<List<Double>>();
    int s = 0;
    for (Entry<String, Coord> stop : stopsBase.entrySet()) {
        String stopKey = stop.getKey();
        boolean mrtStop = stopKey.startsWith("STN");
        /*Coord stopCoord = stop.getValue();
        boolean inStop = stopCoord.getX()>downLeft.getX() && stopCoord.getX()<upRight.getX() && stopCoord.getY()>downLeft.getY() && stopCoord.getY()<upRight.getY();
        if(inStop)*/
        travelTimes.add(new ArrayList<Double>());
        double maxTimeFromStop = 0;
        Collection<Id<Link>> linkIds = new ArrayList<Id<Link>>();
        for (int n = 0; n < NUM_NEAR; n++)
            if (CoordUtils.calcDistance(
                    network.getLinks().get(linksStops.get(n).get(stopKey)).getToNode().getCoord(),
                    stop.getValue()) < MAX_TRAVEL_TIME * WALKING_SPEED / 5)
                linkIds.add(linksStops.get(n).get(stopKey));
        Id<TransitStopFacility> stopId = Id.create(stopKey, TransitStopFacility.class);
        double maxCapacityNearFacilities = 0;
        int w = 0;
        for (MPAreaData mPArea : dataMPAreas.values()) {
            double distance = CoordUtils.calcDistance(stopsBase.get(stopKey), mPArea.getCoord());
            /*Coord areaCoord = mPArea.getCoord();
            boolean inArea = areaCoord.getX()>downLeft.getX() && areaCoord.getX()<upRight.getX() && areaCoord.getY()>downLeft.getY() && areaCoord.getY()<upRight.getY();
            if(inStop && inArea) {*/
            travelTimes.get(travelTimes.size() - 1).add(getCost(mrtStop, Math.floor(36000.0 + distance + 0.5)));
            w++;
            //}
            if (distance < MIN_TRAVEL_TIME * WALKING_SPEED) {
                weights.put(new Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>(stopId, mPArea.getId()),
                        new Tuple<Boolean, Double>(true, getCost(mrtStop, distance / WALKING_SPEED)));
                //if(inStop && inArea) {
                travelTimes.get(travelTimes.size() - 1).set(w - 1, getCost(mrtStop, distance / WALKING_SPEED));
                mPArea.addTravelTime(stopId, distance / WALKING_SPEED);
                //}
                if (distance / WALKING_SPEED > maxTimeFromStop)
                    maxTimeFromStop = distance / WALKING_SPEED;
                maxCapacityNearFacilities += maximumAreaCapacities.get(mPArea.getId());
            } else if (distance < MAX_TRAVEL_TIME * WALKING_SPEED) {
                double walkingTime = Double.MAX_VALUE;
                for (Id<Link> linkId : linkIds)
                    for (Id<Link> linkId2 : mPArea.getLinkIds()) {
                        double walkingTimeA = aStarLandmarks.calcLeastCostPath(
                                network.getLinks().get(linkId).getToNode(),
                                network.getLinks().get(linkId2).getFromNode(), 0, null, null).travelCost
                                + CoordUtils.calcDistance(
                                        network.getLinks().get(linkId2).getFromNode().getCoord(),
                                        mPArea.getCoord()) / WALKING_SPEED;
                        if (walkingTimeA < walkingTime)
                            walkingTime = walkingTimeA;
                    }
                if (walkingTime <= MAX_TRAVEL_TIME) {
                    weights.put(
                            new Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>(stopId, mPArea.getId()),
                            new Tuple<Boolean, Double>(true, getCost(mrtStop, walkingTime)));
                    //if(inStop && inArea) {
                    travelTimes.get(travelTimes.size() - 1).set(w - 1, getCost(mrtStop, walkingTime));
                    mPArea.addTravelTime(stopId, walkingTime);
                    //}
                    if (walkingTime > maxTimeFromStop)
                        maxTimeFromStop = walkingTime;
                    maxCapacityNearFacilities += maximumAreaCapacities.get(mPArea.getId());
                }
            }
        }
        if (stopsCapacities.get(stopId) > maxCapacityNearFacilities) {
            double maxCapacityNear2Facilities = maxCapacityNearFacilities;
            w = 0;
            for (MPAreaData mPArea : dataMPAreas.values()) {
                /*Coord areaCoord = mPArea.getCoord();
                boolean inArea = areaCoord.getX()>downLeft.getX() && areaCoord.getX()<upRight.getX() && areaCoord.getY()>downLeft.getY() && areaCoord.getY()<upRight.getY();
                if(inStop && inArea)*/
                w++;
                if (CoordUtils.calcDistance(stopsBase.get(stopKey),
                        mPArea.getCoord()) < (MAX_TRAVEL_TIME * 2 / 3) * PRIVATE_BUS_SPEED) {
                    double walkingTime = Double.MAX_VALUE;
                    for (Id<Link> linkId : linkIds)
                        for (Id<Link> linkId2 : mPArea.getLinkIds()) {
                            double walkingTimeA = aStarLandmarks.calcLeastCostPath(
                                    network.getLinks().get(linkId).getToNode(),
                                    network.getLinks().get(linkId2).getFromNode(), 0, null, null).travelCost
                                    + CoordUtils.calcDistance(
                                            network.getLinks().get(linkId2).getFromNode().getCoord(),
                                            mPArea.getCoord()) / WALKING_SPEED;
                            if (walkingTimeA < walkingTime)
                                walkingTime = walkingTimeA;
                        }
                    double privateBusTime = Double.MAX_VALUE;
                    for (Id<Link> linkId : linkIds)
                        for (Id<Link> linkId2 : mPArea.getLinkIds()) {
                            double privateBusTimeA = aStarLandmarks.calcLeastCostPath(
                                    network.getLinks().get(linkId).getToNode(),
                                    network.getLinks().get(linkId2).getFromNode(), 0, null, null).travelCost
                                    * WALKING_SPEED / PRIVATE_BUS_SPEED
                                    + CoordUtils.calcDistance(
                                            network.getLinks().get(linkId2).getFromNode().getCoord(),
                                            mPArea.getCoord()) / WALKING_SPEED;
                            if (privateBusTimeA < privateBusTime)
                                privateBusTime = privateBusTimeA;
                        }
                    if (walkingTime > MAX_TRAVEL_TIME && privateBusTime <= (MAX_TRAVEL_TIME * 2 / 3)) {
                        weights.put(
                                new Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>(stopId,
                                        mPArea.getId()),
                                new Tuple<Boolean, Double>(false, getCost(mrtStop, privateBusTime)));
                        //if(inStop && inArea) {
                        travelTimes.get(travelTimes.size() - 1).set(w - 1, getCost(mrtStop, privateBusTime));
                        mPArea.addTravelTime(stopId, privateBusTime);
                        //}
                        if (privateBusTime > maxTimeFromStop)
                            maxTimeFromStop = privateBusTime;
                        maxCapacityNear2Facilities += maximumAreaCapacities.get(mPArea.getId());
                    }
                }
            }
            if (stopsCapacities.get(stopId) > maxCapacityNear2Facilities) {
                System.out.println("Far" + stopId);
                double maxCapacityNear3Facilities = maxCapacityNear2Facilities;
                w = 0;
                for (MPAreaData mPArea : dataMPAreas.values()) {
                    /*Coord areaCoord = mPArea.getCoord();
                    boolean inArea = areaCoord.getX()>downLeft.getX() && areaCoord.getX()<upRight.getX() && areaCoord.getY()>downLeft.getY() && areaCoord.getY()<upRight.getY();
                    if(inStop && inArea)*/
                    w++;
                    if (CoordUtils.calcDistance(stopsBase.get(stopKey), mPArea.getCoord()) < MAX_TRAVEL_TIME
                            * PRIVATE_BUS_SPEED) {
                        double privateBusTime = Double.MAX_VALUE;
                        for (Id<Link> linkId : linkIds)
                            for (Id<Link> linkId2 : mPArea.getLinkIds()) {
                                double privateBusTimeA = aStarLandmarks.calcLeastCostPath(
                                        network.getLinks().get(linkId).getToNode(),
                                        network.getLinks().get(linkId2).getFromNode(), 0, null, null).travelCost
                                        * WALKING_SPEED / PRIVATE_BUS_SPEED
                                        + CoordUtils.calcDistance(
                                                network.getLinks().get(linkId2).getFromNode().getCoord(),
                                                mPArea.getCoord()) / WALKING_SPEED;
                                if (privateBusTimeA < privateBusTime)
                                    privateBusTime = privateBusTimeA;
                            }
                        if (privateBusTime > (MAX_TRAVEL_TIME * 2 / 3) && privateBusTime <= MAX_TRAVEL_TIME) {
                            weights.put(
                                    new Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>(stopId,
                                            mPArea.getId()),
                                    new Tuple<Boolean, Double>(false, getCost(mrtStop, privateBusTime)));
                            //if(inStop && inArea) {
                            travelTimes.get(travelTimes.size() - 1).set(w - 1,
                                    getCost(mrtStop, privateBusTime));
                            mPArea.addTravelTime(stopId, privateBusTime);
                            //}
                            if (privateBusTime > maxTimeFromStop)
                                maxTimeFromStop = privateBusTime;
                            maxCapacityNear3Facilities += maximumAreaCapacities.get(mPArea.getId());
                        }
                    }
                }
                if (stopsCapacities.get(stopId) > maxCapacityNear3Facilities) {
                    System.out.println("Very far" + stopId);
                    removeStops.add(new Tuple<Id<TransitStopFacility>, Integer>(stopId, s));
                }
            }
        }
        double totalTimeFromStop = 0;
        maxTimeFromStop++;
        for (Entry<Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>, Tuple<Boolean, Double>> weight : weights
                .entrySet())
            if (weight.getKey().getFirst().equals(stopId)) {
                double correctWeight = maxTimeFromStop - weight.getValue().getSecond();
                weights.put(weight.getKey(),
                        new Tuple<Boolean, Double>(weight.getValue().getFirst(), correctWeight));
                totalTimeFromStop += correctWeight;
            }
        if (totalTimeFromStop != 0)
            for (Entry<Tuple<Id<TransitStopFacility>, Id<ActivityFacility>>, Tuple<Boolean, Double>> weight : weights
                    .entrySet())
                if (weight.getKey().getFirst().equals(stopId))
                    weights.put(weight.getKey(), new Tuple<Boolean, Double>(weight.getValue().getFirst(),
                            weight.getValue().getSecond() / totalTimeFromStop));
        if (s++ % 100 == 0)
            System.out.println(s + " of " + stopsBase.size());
    }
    int num = 0;
    for (Tuple<Id<TransitStopFacility>, Integer> stopId : removeStops) {
        num += stopsCapacities.get(stopId.getFirst());
        /*stopsCapacities.remove(stopId.getFirst());
        stopsBase.remove(stopId.getFirst().toString());
        stopScheduleCapacities.remove(stopId.getSecond().intValue());
        travelTimes.remove(stopId.getSecond().intValue());*/
    }
    System.out.println(num + " workers lost.");
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(AREAS_MAP_FILE));
    oos.writeObject(dataMPAreas);
    oos.close();
    return weights;
}

From source file:org.mitre.ccv.canopy.CcvCanopyCluster.java

/**
 * Sets the thresholds 1 and 2 using MaxLike profile.
 *
 * Issues/Pittfalls://from  w  ww.java2  s . c  o m
 * <ol>
 * <ul>t2 might be to small and nothing is removed from the list
 * <ul>t1 might be to large and everything is added to a canopy
 * </ol>
 * @todo: figure out how to select threshold1 (not to big not to small)
 */
public double[] autoThreshold() throws Exception {
    LOG.info("autoThreshold: Generating distance distribution");
    //SortedMap<Double, Integer> sortMap = new TreeMap<Double, Integer>(new ReverseDoubleComparator());
    SortedMap<Double, Integer> sortMap = new TreeMap<Double, Integer>();
    // generate all the pairwise distances
    final int size = completeMatrix.getMatrix().getColumnDimension();
    for (int i = 0; i < size; ++i) {
        for (int j = i + 1; j < size; ++j) {
            // only calculate one triangle not full!
            Double d = this.cheapMetric.distance(i, j);
            //set.add(this.cheapMetric.distance(i, j));
            if (sortMap.containsKey(d)) {
                sortMap.put(d, sortMap.get(d) + 1);
            } else {
                sortMap.put(d, 1);
            }
        }
    }

    /**
    * $gnuplot
    * > set nokey
    * > set xlabel "Pairwise distance"
    * > set ylabel "Number of samples"
    * > plot "output.txt" using 1:2
    */
    /* */
    for (Iterator<Entry<Double, Integer>> i = sortMap.entrySet().iterator(); i.hasNext();) {
        Entry<Double, Integer> entry = i.next();

        //System.out.printf("%f\t%d\n", entry.getKey(), entry.getValue());
    }
    /* */

    /**
     * How many bins per samples do we want?
     * Using the two end cases at lower and upper bounds.
     */
    TH1D hist = new TH1D(completeMatrix.getMatrix().getColumnDimension() * 2, sortMap.firstKey(),
            sortMap.lastKey());
    LOG.info(String.format("autoThreshold: Packing into histogram with %d bins (%f, %f)", hist.getBins().length,
            hist.getLower(), hist.getUpper()));
    hist.pack(sortMap);
    int[] bins = hist.getBins();
    if (LOG.isDebugEnabled()) {
        if (hist.getNumberOverflows() != 0) {
            LOG.debug(
                    String.format("autoThreshold: Have %d overflows in histogram!", hist.getNumberOverflows()));
        }
        if (hist.getNumberUnderflows() != 0) {
            LOG.debug(String.format("autoThreshold: Have %d underflows in histogram!",
                    hist.getNumberUnderflows()));
        }
    }

    // print out histogram bins
    for (int i = 0; i < bins.length; i++) {
        //System.out.printf("%f\t%d\n", hist.getBinCenter(i), hist.getBinContent(i));
    }
    TSpectrum spectrum = new TSpectrum(); // use default values (sigma = 1, threshold = 0.5
    int numFound = spectrum.search(hist);
    LOG.info(String.format("autoThreshold: Found %d peaks", numFound));
    if (numFound == 0) {
        LOG.fatal("autoThreshold: No peaks found in data!");
        throw new Exception();
    }
    double xpeaks[] = spectrum.getPostionX();
    double[] rtn = new double[2]; // t1, t2
    if (numFound == 1) {
        int bin = hist.findBin(xpeaks[0]);
        // is this in the top or bottom half?
        // @todo: must be better way than this hack
        if (bin > 0) {
            bin--;
        }
        rtn[0] = hist.getBinCenter(bin); // threshold1 is only peak
        rtn[1] = (hist.getLower() + rtn[0]) / 2;
        return rtn;
    }

    // more than one peak
    /**
     * Several possible options:
     * - select t1 first than find a good t2
     * - select t2 first than find a good t1
     * 
     * make sure that there is enough samples below t2 and above t1
             
    if (xpeaks[0] > xpeaks[1]) {
    // what about sigma value: how many are between these two
    rtn[0] = xpeaks[0]; // t1
    rtn[1] = xpeaks[1];  //t2
    } else {
    rtn[0] = xpeaks[1];
    rtn[1] = xpeaks[0];
    }
    */

    // find the peak with the smallest this will be the basis for t2
    double minPeakX = hist.getUpper();
    int minPeakI = -1;
    for (int i = 0; i < numFound; i++) {
        final double x = xpeaks[i];
        if (x < minPeakX) {
            minPeakX = x;
            minPeakI = i;
        }
    }
    //System.err.printf("minPeakX=%f (%d)\n", minPeakX, minPeakI);

    // find next peak above the smallest
    // should try using something about the average and standard deviation
    // of the distribution of entries in picking this
    double min2PeakX = hist.getUpper();
    int min2PeakI = -1;
    for (int i = 0; i < numFound; i++) {
        final double x = xpeaks[i];
        if (i != minPeakI && x < min2PeakX) { // should check that it isn't equal or within sigma
            min2PeakX = x;
            min2PeakI = i;
        }
    }
    //System.err.printf("min2PeakX=%f (%d)\n", min2PeakX, min2PeakI);
    /**
    if (minPeakI + 1 < min2PeakI - 1) {
    rtn[0] = hist.getBinCenter(min2PeakI - 1);         // t1
    rtn[1] = hist.getBinCenter(minPeakI + 1);          // t2
    } else {
    // really close not good - these should be the centers
    LOG.info("autoThreshold: t1 and t2 are possbily from adjacent bins!");
    rtn[0] = min2PeakX;
    rtn[1] = minPeakX;
    }
    int t2bin = hist.findBin(minPeakX);
    if (t2bin - 1 > 0 ) {
    rtn[1] = hist.getBinCenter(t2bin - 1); // don't want the first bin?
    } else {
    rtn[1] = minPeakX;
    }
    int t1bin = hist.findBin(min2PeakX);
    if (t1bin + 1 < bins.length - 1) {  // don't want the last bin?
    rtn[0] = hist.getBinCenter(t1bin + 1);
    } else {
    rtn[0] = min2PeakX;
    }*/

    rtn[0] = min2PeakX;
    rtn[1] = minPeakX;

    /*
    double t1 = hist.getUpper();
    double t2 = hist.getLower(); */
    // print out what we found
    for (int p = 0; p < numFound; p++) {
        double xp = xpeaks[p];
        int bin = hist.findBin(xp);
        int yp = hist.getBinContent(bin); // double yp
        System.err.printf("%d\t%f\t%d\n", bin, xp, yp);
        // if(yp- Math.sqrt(yp) < fline.eval(xp)) continue
    }

    return rtn;
}

From source file:org.apache.james.mailbox.maildir.MaildirFolder.java

/**
 * Reads all uids from the uid list file which match the given filter
 * and returns as many of them as a sorted map as the limit specifies.
 * /*from w w  w. j a v  a  2  s .c o m*/
 * 
 * @param session
 * @param filter The file names of all returned items match the filter. 
 * The dir argument to {@link FilenameFilter}.accept(dir, name) will always be null.
 * @param limit The number of items; a limit smaller then 1 disables the limit
 * @return A {@link Map} with all uids and associated {@link MaildirMessageName}s
 * @throws MailboxException if there is a problem with the uid list file
 */
public SortedMap<Long, MaildirMessageName> getUidMap(MailboxSession session, FilenameFilter filter, int limit)
        throws MailboxException {
    SortedMap<Long, MaildirMessageName> allUids = getUidMap(session, 0, -1);
    SortedMap<Long, MaildirMessageName> filteredUids = new TreeMap<Long, MaildirMessageName>();
    int theLimit = limit;
    if (limit < 1)
        theLimit = allUids.size();
    int counter = 0;
    for (Entry<Long, MaildirMessageName> entry : allUids.entrySet()) {
        if (counter >= theLimit)
            break;
        if (filter.accept(null, entry.getValue().getFullName())) {
            filteredUids.put(entry.getKey(), entry.getValue());
            counter++;
        }
    }
    return filteredUids;
}