OR-Tools  7.1
TspCircuitBoard.java
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // [START program]
15 // [START import]
16 
23 import java.util.logging.Logger;
24 
25 // [END import]
26 
28 public class TspCircuitBoard {
29  static {
30  System.loadLibrary("jniortools");
31  }
32 
33  private static final Logger logger = Logger.getLogger(TspCircuitBoard.class.getName());
34 
35  // [START data_model]
36  static class DataModel {
37  public final int[][] locations = {{288, 149}, {288, 129}, {270, 133}, {256, 141}, {256, 157},
38  {246, 157}, {236, 169}, {228, 169}, {228, 161}, {220, 169}, {212, 169}, {204, 169},
39  {196, 169}, {188, 169}, {196, 161}, {188, 145}, {172, 145}, {164, 145}, {156, 145},
40  {148, 145}, {140, 145}, {148, 169}, {164, 169}, {172, 169}, {156, 169}, {140, 169},
41  {132, 169}, {124, 169}, {116, 161}, {104, 153}, {104, 161}, {104, 169}, {90, 165},
42  {80, 157}, {64, 157}, {64, 165}, {56, 169}, {56, 161}, {56, 153}, {56, 145}, {56, 137},
43  {56, 129}, {56, 121}, {40, 121}, {40, 129}, {40, 137}, {40, 145}, {40, 153}, {40, 161},
44  {40, 169}, {32, 169}, {32, 161}, {32, 153}, {32, 145}, {32, 137}, {32, 129}, {32, 121},
45  {32, 113}, {40, 113}, {56, 113}, {56, 105}, {48, 99}, {40, 99}, {32, 97}, {32, 89},
46  {24, 89}, {16, 97}, {16, 109}, {8, 109}, {8, 97}, {8, 89}, {8, 81}, {8, 73}, {8, 65},
47  {8, 57}, {16, 57}, {8, 49}, {8, 41}, {24, 45}, {32, 41}, {32, 49}, {32, 57}, {32, 65},
48  {32, 73}, {32, 81}, {40, 83}, {40, 73}, {40, 63}, {40, 51}, {44, 43}, {44, 35}, {44, 27},
49  {32, 25}, {24, 25}, {16, 25}, {16, 17}, {24, 17}, {32, 17}, {44, 11}, {56, 9}, {56, 17},
50  {56, 25}, {56, 33}, {56, 41}, {64, 41}, {72, 41}, {72, 49}, {56, 49}, {48, 51}, {56, 57},
51  {56, 65}, {48, 63}, {48, 73}, {56, 73}, {56, 81}, {48, 83}, {56, 89}, {56, 97}, {104, 97},
52  {104, 105}, {104, 113}, {104, 121}, {104, 129}, {104, 137}, {104, 145}, {116, 145},
53  {124, 145}, {132, 145}, {132, 137}, {140, 137}, {148, 137}, {156, 137}, {164, 137},
54  {172, 125}, {172, 117}, {172, 109}, {172, 101}, {172, 93}, {172, 85}, {180, 85}, {180, 77},
55  {180, 69}, {180, 61}, {180, 53}, {172, 53}, {172, 61}, {172, 69}, {172, 77}, {164, 81},
56  {148, 85}, {124, 85}, {124, 93}, {124, 109}, {124, 125}, {124, 117}, {124, 101}, {104, 89},
57  {104, 81}, {104, 73}, {104, 65}, {104, 49}, {104, 41}, {104, 33}, {104, 25}, {104, 17},
58  {92, 9}, {80, 9}, {72, 9}, {64, 21}, {72, 25}, {80, 25}, {80, 25}, {80, 41}, {88, 49},
59  {104, 57}, {124, 69}, {124, 77}, {132, 81}, {140, 65}, {132, 61}, {124, 61}, {124, 53},
60  {124, 45}, {124, 37}, {124, 29}, {132, 21}, {124, 21}, {120, 9}, {128, 9}, {136, 9},
61  {148, 9}, {162, 9}, {156, 25}, {172, 21}, {180, 21}, {180, 29}, {172, 29}, {172, 37},
62  {172, 45}, {180, 45}, {180, 37}, {188, 41}, {196, 49}, {204, 57}, {212, 65}, {220, 73},
63  {228, 69}, {228, 77}, {236, 77}, {236, 69}, {236, 61}, {228, 61}, {228, 53}, {236, 53},
64  {236, 45}, {228, 45}, {228, 37}, {236, 37}, {236, 29}, {228, 29}, {228, 21}, {236, 21},
65  {252, 21}, {260, 29}, {260, 37}, {260, 45}, {260, 53}, {260, 61}, {260, 69}, {260, 77},
66  {276, 77}, {276, 69}, {276, 61}, {276, 53}, {284, 53}, {284, 61}, {284, 69}, {284, 77},
67  {284, 85}, {284, 93}, {284, 101}, {288, 109}, {280, 109}, {276, 101}, {276, 93}, {276, 85},
68  {268, 97}, {260, 109}, {252, 101}, {260, 93}, {260, 85}, {236, 85}, {228, 85}, {228, 93},
69  {236, 93}, {236, 101}, {228, 101}, {228, 109}, {228, 117}, {228, 125}, {220, 125},
70  {212, 117}, {204, 109}, {196, 101}, {188, 93}, {180, 93}, {180, 101}, {180, 109},
71  {180, 117}, {180, 125}, {196, 145}, {204, 145}, {212, 145}, {220, 145}, {228, 145},
72  {236, 145}, {246, 141}, {252, 125}, {260, 129}, {280, 133}};
73  public final int vehicleNumber = 1;
74  public final int depot = 0;
75  }
76  // [END data_model]
77 
78  // [START euclidean_distance]
82  private static long[][] computeEuclideanDistanceMatrix(int[][] locations) {
83  // Calculate distance matrix using Euclidean distance.
84  long[][] distanceMatrix = new long[locations.length][locations.length];
85  for (int fromNode = 0; fromNode < locations.length; ++fromNode) {
86  for (int toNode = 0; toNode < locations.length; ++toNode) {
87  if (fromNode == toNode) {
88  distanceMatrix[fromNode][toNode] = 0;
89  } else {
90  distanceMatrix[fromNode][toNode] =
91  (long) Math.hypot(locations[toNode][0] - locations[fromNode][0],
92  locations[toNode][1] - locations[fromNode][1]);
93  }
94  }
95  }
96  return distanceMatrix;
97  }
98  // [END euclidean_distance]
99 
100  // [START solution_printer]
102  static void printSolution(
103  RoutingModel routing, RoutingIndexManager manager, Assignment solution) {
104  // Solution cost.
105  logger.info("Objective: " + solution.objectiveValue());
106  // Inspect solution.
107  logger.info("Route:");
108  long routeDistance = 0;
109  String route = "";
110  long index = routing.start(0);
111  while (!routing.isEnd(index)) {
112  route += manager.indexToNode(index) + " -> ";
113  long previousIndex = index;
114  index = solution.value(routing.nextVar(index));
115  routing.getArcCostForVehicle(previousIndex, index, 0);
116  }
117  route += manager.indexToNode(routing.end(0));
118  logger.info(route);
119  logger.info("Route distance: " + routeDistance);
120  }
121  // [END solution_printer]
122 
123  public static void main(String[] args) throws Exception {
124  // Instantiate the data problem.
125  // [START data]
126  final DataModel data = new DataModel();
127  // [END data]
128 
129  // Create Routing Index Manager
130  // [START index_manager]
131  RoutingIndexManager manager =
132  new RoutingIndexManager(data.locations.length, data.vehicleNumber, data.depot);
133  // [END index_manager]
134 
135  // Create Routing Model.
136  // [START routing_model]
137  RoutingModel routing = new RoutingModel(manager);
138  // [END routing_model]
139 
140  // Create and register a transit callback.
141  // [START transit_callback]
142  final long[][] distanceMatrix = computeEuclideanDistanceMatrix(data.locations);
143  final int transitCallbackIndex =
144  routing.registerTransitCallback((long fromIndex, long toIndex) -> {
145  // Convert from routing variable Index to user NodeIndex.
146  int fromNode = manager.indexToNode(fromIndex);
147  int toNode = manager.indexToNode(toIndex);
148  return distanceMatrix[fromNode][toNode];
149  });
150  // [END transit_callback]
151 
152  // Define cost of each arc.
153  // [START arc_cost]
154  routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
155  // [END arc_cost]
156 
157  // Setting first solution heuristic.
158  // [START parameters]
159  RoutingSearchParameters searchParameters =
161  .toBuilder()
163  .build();
164  // [END parameters]
165 
166  // Solve the problem.
167  // [START solve]
168  Assignment solution = routing.solveWithParameters(searchParameters);
169  // [END solve]
170 
171  // Print solution on console.
172  // [START print_solution]
173  printSolution(routing, manager, solution);
174  // [END print_solution]
175  }
176 }
177 // [END program]
static com.google.ortools.constraintsolver.RoutingSearchParameters defaultRoutingSearchParameters()
int registerTransitCallback(LongBinaryOperator callback)
long getArcCostForVehicle(long from_index, long to_index, long vehicle)
static void main(String[] args)
.lang.Override com.google.ortools.constraintsolver.RoutingSearchParameters build()
Builder setFirstSolutionStrategy(com.google.ortools.constraintsolver.FirstSolutionStrategy.Value value)
Assignment solveWithParameters(com.google.ortools.constraintsolver.RoutingSearchParameters search_parameters)
void setArcCostEvaluatorOfAllVehicles(int evaluator_index)