Java tutorial
/* * Copyright (c) 2013. Released under MIT license, see LICENSE.txt. */ package com.slothpetrochemical.bridgeprob; import com.slothpetrochemical.spdijklib.api.ProblemStateTransition; import com.slothpetrochemical.spdijklib.api.Searcher; import com.slothpetrochemical.spdijklib.api.TransitionAndTotalCost; import com.slothpetrochemical.spdijklib.pqueue.PriorityQueueSearcher; import org.apache.commons.cli.*; import java.util.Arrays; import java.util.Collection; /** * Simple demo CLI showing use of API for the flashlight/bridge problem * * http://en.wikipedia.org/wiki/Bridge_and_torch_problem * * To run, enter travel times as arguments on command line: * e.g. * com.slothpetrochemical.bridgeprob.BridgeProblemApp 1 2 5 10 * or * com.slothpetrochemical.bridgeprob.BridgeProblemApp 1 2 5 8 * */ public final class BridgeProblemApp { public static final Integer DEFAULT_MAX_ITER = 1000; private final BridgeProblemStateFactory problemStateFactory; private final Searcher<BridgeProblemSignature> searcher; public BridgeProblemApp(final Collection<Integer> crossingTimes) { this.problemStateFactory = new BridgeProblemStateFactory(crossingTimes); this.searcher = new PriorityQueueSearcher<BridgeProblemSignature>( this.problemStateFactory.getStartStateSignature(), this.problemStateFactory.getEndStateSignature(), this.problemStateFactory); } public void run() { this.searcher.execute(DEFAULT_MAX_ITER); Searcher.SearchStatus s = this.searcher.getStatus(); if (!s.equals(Searcher.SearchStatus.SUCCESS)) { System.out.println("Search ended with status: " + s.name()); } else { Collection<TransitionAndTotalCost<BridgeProblemSignature>> transitions = this.searcher .getShortestPath(); printSignature(transitions.iterator().next().getTransition().getSource(), 0.0f, 0.0f); for (TransitionAndTotalCost<BridgeProblemSignature> transitionAndTotalCost : transitions) { ProblemStateTransition<BridgeProblemSignature> transition = transitionAndTotalCost.getTransition(); printSignature(transition.getTarget(), transition.getCost(), transitionAndTotalCost.getTotalCost()); } System.out.print(String.format("Search took %d total steps", this.searcher.getStepsChecked())); } } private static void printSignature(final BridgeProblemSignature signature, final float cost, final float totalCost) { System.out.println(String.format("%s\t%.2f (%.2f)", signature.toString(), cost, totalCost)); } public static void main(final String[] args) throws ParseException { Options commandLineOptions = createOptions(); PosixParser clParser = new PosixParser(); CommandLine cl = clParser.parse(commandLineOptions, args); if (cl.getArgs().length == 0 || cl.hasOption("h")) { new HelpFormatter().printHelp("crossing_times...", commandLineOptions); } else { String[] clArgs = cl.getArgs(); Integer[] times = new Integer[clArgs.length]; for (int i = 0; i < clArgs.length; ++i) { Integer intTime = Integer.parseInt(clArgs[i]); times[i] = intTime; } Arrays.sort(times); BridgeProblemApp app = new BridgeProblemApp(Arrays.asList(times)); app.run(); } } private static Options createOptions() { Options options = new Options(); options.addOption("h", "help", false, "Print this usage message"); return options; } }