Search in sources :

Example 6 with ToDoubleFunction

use of java.util.function.ToDoubleFunction in project graphhopper by graphhopper.

the class IsochroneResource method doGet.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response doGet(@Context UriInfo uriInfo, @QueryParam("profile") String profileName, @QueryParam("buckets") @Range(min = 1, max = 20) @DefaultValue("1") IntParam nBuckets, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("point") @NotNull GHPointParam point, @QueryParam("time_limit") @DefaultValue("600") LongParam timeLimitInSeconds, @QueryParam("distance_limit") @DefaultValue("-1") LongParam distanceLimitInMeter, @QueryParam("weight_limit") @DefaultValue("-1") LongParam weightLimit, @QueryParam("type") @DefaultValue("json") ResponseType respType, @QueryParam("tolerance") @DefaultValue("0") double toleranceInMeter, @QueryParam("full_geometry") @DefaultValue("false") boolean fullGeometry) {
    StopWatch sw = new StopWatch().start();
    PMap hintsMap = new PMap();
    RouteResource.initHints(hintsMap, uriInfo.getQueryParameters());
    hintsMap.putObject(Parameters.CH.DISABLE, true);
    hintsMap.putObject(Parameters.Landmark.DISABLE, true);
    if (Helper.isEmpty(profileName)) {
        profileName = profileResolver.resolveProfile(hintsMap).getName();
        removeLegacyParameters(hintsMap);
    }
    errorIfLegacyParameters(hintsMap);
    Profile profile = graphHopper.getProfile(profileName);
    if (profile == null)
        throw new IllegalArgumentException("The requested profile '" + profileName + "' does not exist");
    LocationIndex locationIndex = graphHopper.getLocationIndex();
    Graph graph = graphHopper.getGraphHopperStorage();
    Weighting weighting = graphHopper.createWeighting(profile, hintsMap);
    BooleanEncodedValue inSubnetworkEnc = graphHopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName));
    if (hintsMap.has(Parameters.Routing.BLOCK_AREA)) {
        GraphEdgeIdFinder.BlockArea blockArea = GraphEdgeIdFinder.createBlockArea(graph, locationIndex, Collections.singletonList(point.get()), hintsMap, new FiniteWeightFilter(weighting));
        weighting = new BlockAreaWeighting(weighting, blockArea);
    }
    Snap snap = locationIndex.findClosest(point.get().lat, point.get().lon, new DefaultSnapFilter(weighting, inSubnetworkEnc));
    if (!snap.isValid())
        throw new IllegalArgumentException("Point not found:" + point);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    TraversalMode traversalMode = profile.isTurnCosts() ? EDGE_BASED : NODE_BASED;
    ShortestPathTree shortestPathTree = new ShortestPathTree(queryGraph, queryGraph.wrapWeighting(weighting), reverseFlow, traversalMode);
    double limit;
    if (weightLimit.get() > 0) {
        limit = weightLimit.get();
        shortestPathTree.setWeightLimit(limit + Math.max(limit * 0.14, 2_000));
    } else if (distanceLimitInMeter.get() > 0) {
        limit = distanceLimitInMeter.get();
        shortestPathTree.setDistanceLimit(limit + Math.max(limit * 0.14, 2_000));
    } else {
        limit = timeLimitInSeconds.get() * 1000;
        shortestPathTree.setTimeLimit(limit + Math.max(limit * 0.14, 200_000));
    }
    ArrayList<Double> zs = new ArrayList<>();
    double delta = limit / nBuckets.get();
    for (int i = 0; i < nBuckets.get(); i++) {
        zs.add((i + 1) * delta);
    }
    ToDoubleFunction<ShortestPathTree.IsoLabel> fz;
    if (weightLimit.get() > 0) {
        fz = l -> l.weight;
    } else if (distanceLimitInMeter.get() > 0) {
        fz = l -> l.distance;
    } else {
        fz = l -> l.time;
    }
    Triangulator.Result result = triangulator.triangulate(snap, queryGraph, shortestPathTree, fz, degreesFromMeters(toleranceInMeter));
    ContourBuilder contourBuilder = new ContourBuilder(result.triangulation);
    ArrayList<Geometry> isochrones = new ArrayList<>();
    for (Double z : zs) {
        logger.info("Building contour z={}", z);
        MultiPolygon isochrone = contourBuilder.computeIsoline(z, result.seedEdges);
        if (fullGeometry) {
            isochrones.add(isochrone);
        } else {
            Polygon maxPolygon = heuristicallyFindMainConnectedComponent(isochrone, isochrone.getFactory().createPoint(new Coordinate(point.get().lon, point.get().lat)));
            isochrones.add(isochrone.getFactory().createPolygon(((LinearRing) maxPolygon.getExteriorRing())));
        }
    }
    ArrayList<JsonFeature> features = new ArrayList<>();
    for (Geometry isochrone : isochrones) {
        JsonFeature feature = new JsonFeature();
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("bucket", features.size());
        if (respType == geojson) {
            properties.put("copyrights", ResponsePathSerializer.COPYRIGHTS);
        }
        feature.setProperties(properties);
        feature.setGeometry(isochrone);
        features.add(feature);
    }
    ObjectNode json = JsonNodeFactory.instance.objectNode();
    sw.stop();
    ObjectNode finalJson = null;
    if (respType == geojson) {
        json.put("type", "FeatureCollection");
        json.putPOJO("features", features);
        finalJson = json;
    } else {
        json.putPOJO("polygons", features);
        final ObjectNode info = json.putObject("info");
        info.putPOJO("copyrights", ResponsePathSerializer.COPYRIGHTS);
        info.put("took", Math.round((float) sw.getMillis()));
        finalJson = json;
    }
    logger.info("took: " + sw.getSeconds() + ", visited nodes:" + shortestPathTree.getVisitedNodes());
    return Response.ok(finalJson).header("X-GH-Took", "" + sw.getSeconds() * 1000).build();
}
Also used : ProfileResolver(com.graphhopper.routing.ProfileResolver) LoggerFactory(org.slf4j.LoggerFactory) Subnetwork(com.graphhopper.routing.ev.Subnetwork) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Range(org.hibernate.validator.constraints.Range) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) IntParam(io.dropwizard.jersey.params.IntParam) Profile(com.graphhopper.config.Profile) TraversalMode(com.graphhopper.routing.util.TraversalMode) Graph(com.graphhopper.storage.Graph) NODE_BASED(com.graphhopper.routing.util.TraversalMode.NODE_BASED) GraphHopper(com.graphhopper.GraphHopper) org.locationtech.jts.geom(org.locationtech.jts.geom) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) ResponsePathSerializer(com.graphhopper.jackson.ResponsePathSerializer) LocationIndex(com.graphhopper.storage.index.LocationIndex) RouteResource.errorIfLegacyParameters(com.graphhopper.resources.RouteResource.errorIfLegacyParameters) LongParam(io.dropwizard.jersey.params.LongParam) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) NotNull(javax.validation.constraints.NotNull) ResponseType.geojson(com.graphhopper.resources.IsochroneResource.ResponseType.geojson) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GHPointParam(com.graphhopper.http.GHPointParam) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Weighting(com.graphhopper.routing.weighting.Weighting) ToDoubleFunction(java.util.function.ToDoubleFunction) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Snap(com.graphhopper.storage.index.Snap) UriInfo(javax.ws.rs.core.UriInfo) EDGE_BASED(com.graphhopper.routing.util.TraversalMode.EDGE_BASED) Collections(java.util.Collections) RouteResource.removeLegacyParameters(com.graphhopper.resources.RouteResource.removeLegacyParameters) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) TraversalMode(com.graphhopper.routing.util.TraversalMode) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) LocationIndex(com.graphhopper.storage.index.LocationIndex) Graph(com.graphhopper.storage.Graph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 7 with ToDoubleFunction

use of java.util.function.ToDoubleFunction in project CoreNLP by stanfordnlp.

the class SVMLightClassifierFactory method crossValidateSetC.

/**
 * This method will cross validate on the given data and number of folds
 * to find the optimal C.  The scorer is how you determine what to
 * optimize for (F-score, accuracy, etc).  The C is then saved, so that
 * if you train a classifier after calling this method, that C will be used.
 */
public void crossValidateSetC(GeneralDataset<L, F> dataset, int numFolds, final Scorer<L> scorer, LineSearcher minimizer) {
    System.out.println("in Cross Validate");
    useAlphaFile = true;
    boolean oldUseSigmoid = useSigmoid;
    useSigmoid = false;
    final CrossValidator<L, F> crossValidator = new CrossValidator<>(dataset, numFolds);
    final ToDoubleFunction<Triple<GeneralDataset<L, F>, GeneralDataset<L, F>, CrossValidator.SavedState>> score = fold -> {
        GeneralDataset<L, F> trainSet = fold.first();
        GeneralDataset<L, F> devSet = fold.second();
        alphaFile = (File) fold.third().state;
        // train(trainSet,true,true);
        SVMLightClassifier<L, F> classifier = trainClassifierBasic(trainSet);
        fold.third().state = alphaFile;
        return scorer.score(classifier, devSet);
    };
    DoubleUnaryOperator negativeScorer = cToTry -> {
        C = cToTry;
        if (verbose) {
            System.out.print("C = " + cToTry + " ");
        }
        Double averageScore = crossValidator.computeAverage(score);
        if (verbose) {
            System.out.println(" -> average Score: " + averageScore);
        }
        return -averageScore;
    };
    C = minimizer.minimize(negativeScorer);
    useAlphaFile = false;
    useSigmoid = oldUseSigmoid;
}
Also used : java.util(java.util) LineSearcher(edu.stanford.nlp.optimization.LineSearcher) edu.stanford.nlp.util(edu.stanford.nlp.util) Redwood(edu.stanford.nlp.util.logging.Redwood) Datum(edu.stanford.nlp.ling.Datum) NumberFormat(java.text.NumberFormat) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) java.io(java.io) ToDoubleFunction(java.util.function.ToDoubleFunction) edu.stanford.nlp.stats(edu.stanford.nlp.stats) Pattern(java.util.regex.Pattern) GoldenSectionLineSearch(edu.stanford.nlp.optimization.GoldenSectionLineSearch) RVFDatum(edu.stanford.nlp.ling.RVFDatum) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 8 with ToDoubleFunction

use of java.util.function.ToDoubleFunction in project CoreNLP by stanfordnlp.

the class LinearClassifierFactory method crossValidateSetSigma.

/**
 * Sets the sigma parameter to a value that optimizes the cross-validation score given by {@code scorer}.  Search for an optimal value
 * is carried out by {@code minimizer}.
 *
 * @param dataset the data set to optimize sigma on.
 */
public void crossValidateSetSigma(GeneralDataset<L, F> dataset, int kfold, final Scorer<L> scorer, LineSearcher minimizer) {
    logger.info("##in Cross Validate, folds = " + kfold);
    logger.info("##Scorer is " + scorer);
    featureIndex = dataset.featureIndex;
    labelIndex = dataset.labelIndex;
    final CrossValidator<L, F> crossValidator = new CrossValidator<>(dataset, kfold);
    final ToDoubleFunction<Triple<GeneralDataset<L, F>, GeneralDataset<L, F>, CrossValidator.SavedState>> scoreFn = fold -> {
        GeneralDataset<L, F> trainSet = fold.first();
        GeneralDataset<L, F> devSet = fold.second();
        double[] weights = (double[]) fold.third().state;
        double[][] weights2D;
        // must of course bypass sigma tuning here.
        weights2D = trainWeights(trainSet, weights, true);
        fold.third().state = ArrayUtils.flatten(weights2D);
        LinearClassifier<L, F> classifier = new LinearClassifier<>(weights2D, trainSet.featureIndex, trainSet.labelIndex);
        double score = scorer.score(classifier, devSet);
        // System.out.println("score: "+score);
        System.out.print(".");
        return score;
    };
    DoubleUnaryOperator negativeScorer = sigmaToTry -> {
        // sigma = sigmaToTry;
        setSigma(sigmaToTry);
        Double averageScore = crossValidator.computeAverage(scoreFn);
        logger.info("##sigma = " + getSigma() + " -> average Score: " + averageScore);
        return -averageScore;
    };
    double bestSigma = minimizer.minimize(negativeScorer);
    logger.info("##best sigma: " + bestSigma);
    setSigma(bestSigma);
}
Also used : edu.stanford.nlp.optimization(edu.stanford.nlp.optimization) Counters(edu.stanford.nlp.stats.Counters) IOUtils(edu.stanford.nlp.io.IOUtils) edu.stanford.nlp.util(edu.stanford.nlp.util) Redwood(edu.stanford.nlp.util.logging.Redwood) Datum(edu.stanford.nlp.ling.Datum) Scorer(edu.stanford.nlp.stats.Scorer) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) MultiClassAccuracyStats(edu.stanford.nlp.stats.MultiClassAccuracyStats) List(java.util.List) Counter(edu.stanford.nlp.stats.Counter) ArrayMath(edu.stanford.nlp.math.ArrayMath) ToDoubleFunction(java.util.function.ToDoubleFunction) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) BufferedReader(java.io.BufferedReader) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 9 with ToDoubleFunction

use of java.util.function.ToDoubleFunction in project RecurrentComplex by Ivorforce.

the class CommandSearchStructure method execute.

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
    Parameters parameters = Parameters.of(args, expect()::declare);
    List<ToDoubleFunction<String>> ranks = new ArrayList<>();
    consider(ranks, parameters.get(0), Parameter::varargsList, (s, t) -> StructureSearch.searchRank(t, StructureSearch.keywords(StructureRegistry.INSTANCE.id(s), s)));
    consider(ranks, parameters.get("containing"), e -> RCP.expression(e, new BlockExpression(RecurrentComplex.specialRegistry)), StructureSearch::containedBlocks);
    consider(ranks, parameters.get("biome"), MCP::biome, StructureSearch::biome);
    consider(ranks, parameters.get("dimension"), MCP.dimension(server, sender), StructureSearch::dimension);
    consider(ranks, parameters.get("maze"), p -> p, StructureSearch::maze);
    consider(ranks, parameters.get("list"), p -> p, StructureSearch::list);
    consider(ranks, parameters.get("author"), p -> p, StructureSearch::author);
    boolean all = parameters.has("all");
    if (ranks.stream().noneMatch(Objects::nonNull))
        throw new WrongUsageException(getUsage(sender));
    postResultMessage("Results: ", sender, RCTextStyle::structure, search(all ? StructureRegistry.INSTANCE.ids() : StructureRegistry.INSTANCE.activeIDs(), name -> ranks.stream().filter(Objects::nonNull).mapToDouble(f -> f.applyAsDouble(name)).reduce(1, (a, b) -> a * b)));
}
Also used : BlockExpression(ivorius.reccomplex.utils.expression.BlockExpression) java.util(java.util) MCE(ivorius.mcopts.commands.parameters.expect.MCE) Structure(ivorius.reccomplex.world.gen.feature.structure.Structure) StructureRegistry(ivorius.reccomplex.world.gen.feature.structure.StructureRegistry) TextComponentBase(net.minecraft.util.text.TextComponentBase) RCConfig(ivorius.reccomplex.RCConfig) Function(java.util.function.Function) ServerTranslations(ivorius.mcopts.translation.ServerTranslations) ITextComponent(net.minecraft.util.text.ITextComponent) CommandException(net.minecraft.command.CommandException) MinecraftServer(net.minecraft.server.MinecraftServer) RecurrentComplex(ivorius.reccomplex.RecurrentComplex) Nonnull(javax.annotation.Nonnull) RCTextStyle(ivorius.reccomplex.commands.RCTextStyle) CommandExpecting(ivorius.mcopts.commands.CommandExpecting) Collectors(java.util.stream.Collectors) MCP(ivorius.mcopts.commands.parameters.MCP) TextComponentString(net.minecraft.util.text.TextComponentString) ToDoubleBiFunction(java.util.function.ToDoubleBiFunction) RCP(ivorius.reccomplex.commands.parameters.RCP) Expect(ivorius.mcopts.commands.parameters.expect.Expect) ICommandSender(net.minecraft.command.ICommandSender) WrongUsageException(net.minecraft.command.WrongUsageException) ToDoubleFunction(java.util.function.ToDoubleFunction) Doubles(com.google.common.primitives.Doubles) Parameter(ivorius.mcopts.commands.parameters.Parameter) Parameters(ivorius.mcopts.commands.parameters.Parameters) Parameters(ivorius.mcopts.commands.parameters.Parameters) BlockExpression(ivorius.reccomplex.utils.expression.BlockExpression) WrongUsageException(net.minecraft.command.WrongUsageException) RCTextStyle(ivorius.reccomplex.commands.RCTextStyle) Parameter(ivorius.mcopts.commands.parameters.Parameter) MCP(ivorius.mcopts.commands.parameters.MCP) ToDoubleFunction(java.util.function.ToDoubleFunction)

Aggregations

ToDoubleFunction (java.util.function.ToDoubleFunction)9 List (java.util.List)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 JsonObject (com.google.gson.JsonObject)3 Coordinate (com.simiacryptus.mindseye.lang.Coordinate)3 DataSerializer (com.simiacryptus.mindseye.lang.DataSerializer)3 DeltaSet (com.simiacryptus.mindseye.lang.DeltaSet)3 Layer (com.simiacryptus.mindseye.lang.Layer)3 LayerBase (com.simiacryptus.mindseye.lang.LayerBase)3 Result (com.simiacryptus.mindseye.lang.Result)3 Tensor (com.simiacryptus.mindseye.lang.Tensor)3 TensorArray (com.simiacryptus.mindseye.lang.TensorArray)3 TensorList (com.simiacryptus.mindseye.lang.TensorList)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 Function (java.util.function.Function)3 IntStream (java.util.stream.IntStream)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3