Parameters, CloseModelWithParameters and SolveFromAssignmentWithParameters

1,037 views
Skip to first unread message

Geoff Leyland

unread,
Oct 2, 2017, 7:30:56 PM10/2/17
to or-tools-discuss
Hi,

I'm solving a routing problem where I pass an initial solution built with RoutesToAssignment to Solve().  As I understand it, I need to call CloseModel() before I use RoutesToAssignment.

I learnt earlier that, parameters passed to SolveFromAssignmentWithParameters are ignored if CloseModel has been called, and that the correct thing to do is pass the parameters to CloseModelWithParameters.

At least, I thought I learned that.  Because now it seems that some parameters passed to CloseModelWithParameters are ignored, and they must be passed to SolveFromAssignmentWithParameters.

For example, as far as I can tell, a first solution strategy set with set_first_solution_strategy is ignored by SolveFromParametersWithAssignment after a call to CloseModel, and must be set with CloseModelWithParameters.

On the other hand, a time limit set with set_time_limit_ms is ignored by CloseModelWithParameters, and must be set in SolveFromParametersWithAssignment.

I find this all a little confusing.  Is it documented somewhere?  If not, could it be?

Thanks in advance,
Geoff

Paul Trow

unread,
Oct 3, 2017, 5:17:11 PM10/3/17
to or-tools-discuss
Hi Geoff,

I don't think it is necessary to call CloseModel() before using RoutesToAssignment, unless there is already a model open, which doesn't seem to be the case in your problem.

Are you referring to the following sentence in the reference doc for CloseModel()  (https://developers.google.com/optimization/reference/constraint_solver/routing/RoutingModel/#CloseModel)?

"Closes the current routing model; after this method is called, no modification to the model can be done, but RoutesToAssignment becomes available. "

I'm not sure, but I think this just means if there is an open model, you have to close it before using RoutesToAssignment.

Yes, this should be documented, and will be soon.

Best,

Paul

Geoff Leyland

unread,
Oct 5, 2017, 4:16:57 PM10/5/17
to or-tools...@googlegroups.com
Hi Paul,

Thanks for the reply, but now I'm confused. What's an open model?

In the code in question (C++) I construct a RoutingModel with a node count and a list of start, end pairs for each vehicle, and then go about setting vehicle costs and time limits. I had assumed that what I had was an "open" model, and if I take out the call to CloseModel, I get:

E1006 09:15:17.081075 2698150848 routing.cc:3254] The model is not closed yet
Assertion failed: (R.RoutesToAssignment(initial_tour_vector, true, true, initial_assignment)), function set_initial_solution, file src-c/or_tools_route.cpp, line 415.

so I think I'm on the right track, but I'd really appreciate any further explanation.

Warm Regards,
Geoff
Message has been deleted

Paul Trow

unread,
Oct 6, 2017, 7:39:43 AM10/6/17
to or-tools-discuss
Hi Geoff,

If you have created the model and haven't called the solver, you can use ReadAssignmentFromRoutes without doing CloseModel(). If you take the example here:


and replace the existing call to the solver with the following:

initial_routes = [[11], [17, 19, 2, 28, 4], [20, 5, 25, 10, 15, 9, 8, 23],
                      [27, 24, 6, 13, 21, 31, 3], [30, 26, 16, 12, 1, 7, 14, 29, 18, 22]]
initial_assignment = routing.ReadAssignmentFromRoutes(initial_routes, True)
assignment = routing.SolveFromAssignmentWithParameters(initial_assignment, search_parameters)

the search will begin at the specified initial routes. 

I think the point is that you can call ReadAssignmentFromRoutes if you haven't already called Solve on the model.

Paul

Paul Trow

unread,
Oct 9, 2017, 12:24:00 PM10/9/17
to or-tools-discuss
Geoff,

Just to clarify, you don't need to call CloseModel() before ReadAssignmentFromRoutes(), because the latter calls CloseModel() (as does Solve()). But you do need to call CloseModel() before RoutesToAssignment()

When you are setting up the model by adding constraints, etc., the model is open.   The model needs to be "closed" before the search can begin, so Solve()  automatically calls CloseModel().  So normally you don't need to call CloseModel() -
if you just want to start a search from a given set of routes, you can do it with ReadAssignmentFromRoutes(), as in the example link in my previous message.

Hope this helps.

Paul

Geoff Leyland

unread,
Oct 9, 2017, 1:50:20 PM10/9/17
to or-tools...@googlegroups.com
Thanks Paul,

I hadn't even noticed the difference between ReadAssignmentFromRoutes and RoutesToAssignment. However, if ReadAssignmentFromRoutes calls CloseModel, what parameters does in pass to CloseModel? Because, as I said, as far as I can tell, some parameters passed to SolveFromAssignmentWithParameters (or Solve?) are ignored, and only work if passed to CloseModelWithParameters. Is that right?

Warm Regards,
Geoff
> --
> You received this message because you are subscribed to a topic in the Google Groups "or-tools-discuss" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/or-tools-discuss/8A7WbkcvFU8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to or-tools-discu...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Paul Trow

unread,
Oct 11, 2017, 4:29:39 PM10/11/17
to or-tools-discuss
Geoff,

You were correct in the first place - you do have to call CloseModelWithParameters(search_parameters) first, before calling ReadAssignmentFromRoutes in order to use the search parameters.
(Except for some reason this doesn't apply to the search limits, such as time_limit_ms - that's what was confusing me. Incidentally, the parameter name is time_limit_ms - not set_time_limit_ms.  That
parameter can be passed by ReadAssignmentfFromRoutes, even without doing CloseModelWithParameters(search_parameters).

ReadAssignmentFromRoutes() does call CloseModel, which does not preserve the parameters. But if you do the CloseModelWithParameters first, the subsequent CloseModel() has no effect.

Sorry for the confusion.

Best,

Paul
Reply all
Reply to author
Forward
0 new messages