Can we specify forbidden edge constraints in Google OR Tools? - or-tools

I'm trying to solve a classic Traveling Salesman Problem (TSP). I'm using Google OR Tools default TSP wrapper code. I want some arcs to be forbidden, in the sense that perhaps there is no path between node 10 and node 12. In this instance, I set the value to a large number, like 10^6, but the solver still uses that arc.
I verified that this particular instance has a feasible solution, in the sense that when I run the same code twice or extend the time limit of the solver from 30s to 60s, the solver found a solution that does not use this arc.
The question I have: is there a way to specify a hard constraint that this can't be used, instead of setting the arc cost to ∞? I imagine that in a classic TSP Linear Program, a constraint can be set on the binary decision variable.

You have two options:
Create a routing dimension so you can set a vehicle capacity (i.e. vehicle longest distance allowed). Thus any arc whose transit cost is above this capacity is effectively forbidden (vehicle capacity is an hard constraint).
e.g.
# Add Distance constraint.
dimension_name = 'Distance'
routing.AddDimension(
transit_callback_index, # you can reuse the same callback use in SetArcCost
0, # no slack
42_000, # vehicle maximum travel distance
True, # start cumul to zero
dimension_name)
now every arcs above 42_000 is forbidden.
You can remove some arcs by tweaking the routing.NextArc()
e.g. to remove arc {I,J}
i_index = manager.NodeToIndex(I)
j_index = manager.NodeToIndex(J)
routing.NextVar(i_index).RemoveValue(j_index)
note: you also have the RemoveValues([....]) to suppress a list of outgoing arcs.

Related

Observation Space for race strategy development - Reinforcement learning

I refrained from asking for help until now, but as my thesis' deadline creeps ever closer and I do not know anybody with experience in RL, I'm trying my luck here.
TLDR;
I have not found an academic/online resource which helps me understand the correct representation of the environment as an observation space. I would be very thankful for any links or for giving me a starting point of how to model the specifics of my environment in an observation space.
Short thematic introduction
The goal of my research is to determine the viability of RL for strategy development in motorsports. This is currently achieved by simulating (lots of!) races and calculating the resulting race time (thus end-position) of different strategic decisions (which are the timing of pit stops + amount of laps to refuel for). This demands a manual input of expected inlaps (the lap a pit stop occurs) for all participants, which implicitly limits the possible strategies by human imagination as well as the amount of possible simulations.
Use of RL
A trained RL agent could decide on its own when to perform a pit stop and how much fuel should be added, in order to minizime the race time and react to probabilistic events in the simulation.
The action space is discrete(4) and represents the options to continue, pit and refuel for 2,4,6 laps respectively.
Problem
The observation space is of POMDP nature and needs to model the agent's current race position (which I hope is enough?). How would I implement the observation space accordingly?
The training is performed using OpenAI's Gym framework, but a general explanation/link to article/publication would also be appreciated very much!
Your observation could be just an integer which represents round or position the agent is in. This is obviously not a sufficient representation so you need to add more information.
A better observation could be the agents race position x1, the round the agent is in x2 and the current fuel in the tank x3. All three of these can be represented by a real number. Then you can create your observation by concating these to a vector obs = [x1, x2, x3].

OR-tools VRP with Multi-dimension. Optimize in one, constrains in others

I have a problem when I want to optimize the global Cost of the vehicules' route. So for that I register a Cost Dimension for returning the cost associated to each arc. But at the same time I have restrictions in another dimensions such as Time & Distance. How can I achieve this? Maybe I just only have to use AddDimension for each callback, but I don't know how to set the Objective function in RoutingModel.
All in your question: Does restriction aka constraint should be part of the objective cost ? or this is just, well constraints the solver need to fulfill ?
Adding new dimension using AddDimension() is a good start IMHO.
note: You may also add the span or global span of any dimension to the objective cost by setting the corresponding coefficient (zero by default thus dimension won't participate to the objective cost and only add "constraint" to the solution)
ref: https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/ortools/constraint_solver/routing.h#L2482-L2496

Get the current SearchDepth within the distance cost function of a Vehicle Routing Problem

I'm working on a Vehicle Routing Problem. In my cost function I need to find the current search depth in order to calculate a deferred cost which is dependent on the current length of the intermediate solution. Is this information available via some method? This is my distance cost function:
def distance_callback(from_index, to_index):
"""Returns the shortest path distance between the two nodes"""
from_node = self.routing_manager.IndexToNode(from_index)
to_node = self.routing_manager.IndexToNode(to_index)
return self.distance_matrix[from_node][to_node]
See discussion on:
https://groups.google.com/forum/#!topic/or-tools-discuss/lw_zdalvm6k
The current approach is not possible as the distance callback is called in many places, and is usually cached, especially if written in python.
The original request is to have time dependent demands. It can be modeled with duplicate nodes, in disjunctions, with non overlapping time windows, and different demands.

How to merge clustering results for different clustering approaches?

Problem: It appears to me that a fundamental property of a clustering method c() is whether we can combine the results c(A) and c(B) by some function f() of two clusterings in a way that we do not have to apply the full clustering c(A+B) again but instead do f(c(A),c(B)) and still end up with the same result:
c(A+B) == f(c(A),c(B))
I suppose that a necessary condition for some c() to have this property is that it is determistic, that is the order of its internal processing is irrelevant for the result. However, this might not be sufficient.
It would be really nice to have some reference where to look up which cluster methods support this and what a good f() looks like in the respective case.
Example: At the moment I am thinking about DBSCAN which should be deterministic if I allow border points to belong to multiple clusters at the same time (without connecting them):
One point is reachable from another point if it is in its eps-neighborhood
A core point is a point with at least minPts reachable
An edge goes from every core point to all points reachable from it
Every point with incoming edge from a core point is in the same cluster as the latter
If you miss the noise points then assume that each core node reaches itself (reflexivity) and afterwards we define noise points to be clusters of size one. Border points are non-core points. Afterwards if we want a partitioning, we can assign randomly the border points that are in multiple clusters to one of them. I do not consider this relevant for the method itself.
Supposedly the only clustering where this is efficiently possible is single linkage hierarchical clustering, because edges removed from A x A and B x B are not necessary for finding the MST of the joined set.
For DBSCAN precisely, you have the problem that the core point property can change when you add data. So c(A+B) likely has core points that were not core in either A not B. This can cause clusters to merge. f() supposedly needs to re-check all data points, i.e., rerun DBSCAN. While you can exploit that core points of the subset must be core of the entire set, you'll still need to find neighbors and missing core points.

Finding a path: SAT solving

We are given an n*m grid, which has obstacles at various points,the starting and ending location of the bot. The task is to find a collision free path from start to end. This problem is to be modelled as a SAT problem.
Please guide me on what should be done in this case to get an optimal solution.
I would assume that optimal means the shortest. Using the approach that I've described here you can do first steps:
define a grid
formulate a satisfiability task
At this stage, a solver returns to you random path that satisfies all constraints. An important thing to remember - you can define number of steps k which are required to reach a goal! So you just start with k = 0. Is it possible to reach the goal with 0 actions? Probably, not, until an agent is at the goal already. Then just increment k = 1. Is it possible now? If not, increment more! How to implement it? Just set all k's above a certain limit to False and increment this limit each iteration.
If you know upper limits, you can use binary search to find the shortest possible path, which could be more efficient.
If you care for other properties of a path, you can use pseudo-boolean constraints. By leveraging this approach, you can minimize, for example, a number of right turns. Create a Boolean counter for all possible right turns and limit number of available turns via assumptions.