Or-tools: setTimeLimit example

Created on 17 Apr 2019  路  3Comments  路  Source: google/or-tools

I am having trouble setting the time limit value of the search parameters in Java. I would like to pass an int at "XXX" below but setTimeLimit expects a com.google.protobuf.Duration. A new instance of that Duration object can not be created with an integer either. It requires a "builder", something like com.google.protobuf.GeneratedMessageV3.Builder. Surely I am overcomplicating things here.

Can you please give a Java example of setting the time out?

RoutingSearchParameters searchParameters =
 main.defaultRoutingSearchParameters()
  .toBuilder()
   .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
   .setTimeLimit(XXX)
   .build();
Help Needed Java Routing Solver

All 3 comments

First import Duration

import com.google.protobuf.Duration;

then you can use:

    RoutingSearchParameters searchParameters =
        main.defaultRoutingSearchParameters()
            .toBuilder()
            .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
            .setLogSearch(true)
            .setTimeLimit(Duration.newBuilder().setSeconds(10).build())
            .build();

Using builder:
src: https://developers.google.com/protocol-buffers/docs/javatutorial#builders
src: https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Duration#newBuilder--

thank you!

Was this page helpful?
0 / 5 - 0 ratings