Custom cost function

1,509 views
Skip to first unread message

igor r

unread,
Jul 2, 2021, 1:58:52 PM7/2/21
to or-tools-discuss

Hi,

Is it possible to set a custom cost function?
If not which ways can it be adjusted?

I found only one example of this with SetGlobalSpanCostCoefficient

Mizux Seiha

unread,
Jul 5, 2021, 4:25:55 AM7/5/21
to or-tools-discuss
You can't have a custom cost function, BUT the cost function can integrate:

- [mandatory] The sum of all arc cost used (used by any initial first strategy)
  ref: https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/ortools/constraint_solver/routing.h#L901-L906
- [optional] The span cost of any dimension (e.d. one coefficient per vehicle, default 0)
- [optional] The global span cost of any dimension (e.d. one coefficient per dimension, default 0)
- [optional] A vehicle fixed cost (e.d. one cost per vehicle, default 0)
  ref: https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/ortools/constraint_solver/routing.h#L907-L911
- [optional] A penalty cost for any dropping node using AddDisjunction() (default all nodes are mandatory)
  ref: https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/ortools/constraint_solver/routing.h#L606-L624 
- [optional] A linear penalty cost per node if using SoftCumulVar Lower/Upper Cost (to simulate earliness/tardiness) (default only hard limit [0, vehicle max capacity aka horizon of the dimension])
Misc/Not tested yet:
- [optional] Soft constraint a group of node to be visited by the same vehicle

note: You can search for "soft" which means add extra cost to objective if not fulfilled while hard constraint must be respected... 

Priidik Vilumaa

unread,
Jul 5, 2021, 4:44:10 AM7/5/21
to or-tools...@googlegroups.com
Great list! Maybe this should be a pinned post or in the routing solver docs/guide to explain the objective function?

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/051e5bda-1cd1-4ab3-a467-8bd29d142d55n%40googlegroups.com.

igor r

unread,
Jul 6, 2021, 3:31:45 PM7/6/21
to or-tools-discuss
Thank you.
And is there a formula how they are all combined together to get a final score?

Laurent Perron

unread,
Jul 7, 2021, 2:10:57 AM7/7/21
to or-tools-discuss
the weighted sum of all of them with the coefficient given in the  API.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.

amihai...@gmail.com

unread,
Jul 7, 2021, 5:02:07 AM7/7/21
to or-tools-discuss
Great post mizux - thx!
Is there a way to add a coefficient to the arc cost's sum? Or it should be done via the callback?

Laurent Perron

unread,
Jul 7, 2021, 5:18:47 AM7/7/21
to or-tools-discuss
via the callback.

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


igor r

unread,
Jul 8, 2021, 4:04:52 PM7/8/21
to or-tools-discuss
Thanks.
1. Can the coefficients be: a) negative  b) float ?
2. Is it possible to set the engine to look for max instead min? 
3. Can the cost be set to be negative?

Laurent Perron

unread,
Jul 8, 2021, 4:08:17 PM7/8/21
to or-tools-discuss
all arc evaluators must be >= 0.
Demand evaluator can be negative, but the cumulvar are always >=0.
The solver does not support floats.

I do not know about cost weights. I would say > 0.

Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


igor r

unread,
Jul 9, 2021, 3:00:08 PM7/9/21
to or-tools-discuss
So, is there a proper way to find a maximum instead of a minimum?
How can I set the conditions to find the longest route?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages