Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use markdown code block #515

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions docs/Simple-Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Assume the following problem. We can employ one vehicle(-type) located at (10,10

First, build a vehicle with its vehicle-type:

<pre><code>/*
```java
/*
* get a vehicle type-builder and build a type with the typeId "vehicleType" and a capacity of 2
* you are free to add an arbitrary number of capacity dimensions with .addCapacityDimension(dimensionIndex,dimensionValue)
*/
Expand All @@ -29,21 +30,23 @@ VehicleImpl.Builder vehicleBuilder = VehicleImpl.Builder.newInstance("vehicle");
vehicleBuilder.setStartLocation(Location.newInstance(10, 10));
vehicleBuilder.setType(vehicleType);
VehicleImpl vehicle = vehicleBuilder.build();
</code></pre>
```

Second, define the deliveries as services. Make sure their size dimensions are in line with your vehicle capacity dimensions (thus here the weight-index is made final and also used to define services).
<pre><code>/*
```java
/*
* build services with id 1...4 at the required locations, each with a capacity-demand of 1.
* Note, that the builder allows chaining which makes building quite handy
*/
Service service1 = Service.Builder.newInstance("1").addSizeDimension(WEIGHT_INDEX,1).setLocation(Location.newInstance(5, 7)).build();
Service service2 = Service.Builder.newInstance("2").addSizeDimension(WEIGHT_INDEX,1).setLocation(Location.newInstance(5, 13)).build();
Service service3 = Service.Builder.newInstance("3").addSizeDimension(WEIGHT_INDEX,1).setLocation(Location.newInstance(15, 7)).build();
Service service4 = Service.Builder.newInstance("4").addSizeDimension(WEIGHT_INDEX,1).setLocation(Location.newInstance(15, 13)).build();
</code></pre>
```

and put vehicles and services together to setup the problem.
<pre><code>/*
```java
/*
* again define a builder to build the VehicleRoutingProblem
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
Expand All @@ -56,11 +59,11 @@ vrpBuilder.addJob(service1).addJob(service2).addJob(service3).addJob(service4);
* by default, transport costs are computed as Euclidean distances
*/
VehicleRoutingProblem problem = vrpBuilder.build();
</code></pre>

```

Third, solve the problem by defining and running an algorithm. Here it comes out-of-the-box.
<pre><code>/*
```java
/*
* get the algorithm out-of-the-box.
*/
VehicleRoutingAlgorithm algorithm = Jsprit.createAlgorithm(problem);
Expand All @@ -74,14 +77,17 @@ Collection&lt;VehicleRoutingProblemSolution&gt; solutions = algorithm.searchSolu
* use the static helper-method in the utility class Solutions to get the best solution (in terms of least costs)
*/
VehicleRoutingProblemSolution bestSolution = Solutions.bestOf(solutions);
</code></pre>
```

If you want to print a concise summary of the results to the console, do this:

<code>SolutionPrinter.print(problem, bestSolution, Print.CONCISE);</code>
```java
SolutionPrinter.print(problem, bestSolution, Print.CONCISE);
```

which results in
<pre><samp>+--------------------------+
```
+--------------------------+
| problem |
+---------------+----------+
| indicator | value |
Expand All @@ -99,14 +105,17 @@ which results in
| costs | 35.3238075793812 |
| nVehicles | 2 |
+----------------------------------------------------------+
</samp></pre>
```

If you want to have more information about individual routes, use the verbose level such as

<code>SolutionPrinter.print(problem, bestSolution, Print.VERBOSE);</code>
```
SolutionPrinter.print(problem, bestSolution, Print.VERBOSE);
```

and you get this addtionally:
<pre><samp>+--------------------------------------------------------------------------------------------------------------------------------+
```
+--------------------------------------------------------------------------------------------------------------------------------+
| detailed solution |
+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+
| route | vehicle | activity | job | arrTime | endTime | costs |
Expand All @@ -121,23 +130,29 @@ and you get this addtionally:
| 2 | vehicle | service | 4 | 12 | 12 | 12 |
| 2 | vehicle | end | - | 18 | undef | 18 |
+--------------------------------------------------------------------------------------------------------------------------------+
</samp></pre>
```

If you want to write out problem and solution, you require the writer that is located in jsprit-io. Thus, you need to add the
corresponding module to your pom.xml much like you added jsprit-core. Just use jsprit-io. You can then use this:

<pre><code>new VrpXMLWriter(problem, solutions).write("output/problem-with-solution.xml");
</code></pre>
```
new VrpXMLWriter(problem, solutions).write("output/problem-with-solution.xml");
```

which looks like this: [problem-with-solution.xml](https://github.com/jsprit/misc-rep/raw/master/wiki-images/problem-with-solution.xml).

If you want to further analyse your solution, add the module jsprit-analysis to your pom. Then you can plot the routes like this

<code>new Plotter(problem,bestSolution).plot("output/solution.png", "solution");</code>
```
new Plotter(problem,bestSolution).plot("output/solution.png", "solution");
```

and you get [solution.png](https://github.com/jsprit/misc-rep/blob/master/wiki-images/solution.png)

or use the GraphStreamViewer which dynamically renders the problem and its according solution by coding

<code>new GraphStreamViewer(problem, bestSolution).setRenderDelay(100).display();</code>
```
new GraphStreamViewer(problem, bestSolution).setRenderDelay(100).display();
```

You can find the entire code [here](https://github.com/graphhopper/jsprit/blob/master/jsprit-examples/src/main/java/com/graphhopper/jsprit/examples/SimpleExample.java).