modifying google ortools vrptw example fails - or-tools

I done some experiments with this exmalple:
https://developers.google.com/optimization/routing/vrptw
My Data set is this:
data['time_windows'] = [(0, 1), (0, 84), (0, 84), (0, 84), (0, 84), (0, 84), (12, 36), (12, 36)]
data['time_matrix'] = [[0, 8, 7, 8, 4, 6, 8, 7], [0, 0, 10, 11, 4, 9, 8, 7], [0, 10, 0, 11, 4, 9, 8, 7], [0, 10, 11, 0, 4, 9, 8, 7], [0, 10, 11, 4, 0, 9, 8, 7], [0, 10, 11, 4, 9, 0, 8, 7], [0, 10, 11, 4, 9, 8, 0, 7], [0, 10, 11, 4, 9, 8, 7, 0]]
I want to have only one vehicle for a test. But if I set " data['num_vehicles'] = 1 " I get no output but a clean exit. Why? I guess this is just a standard TSP, if I reduce the vehicles to 1.

Well, as mentioned by Laurent Perron in the comment, there was a maximum time per vehicle in this example.
routing.AddDimension(
transit_callback_index,
30, # allow waiting time
30, # maximum time per vehicle <<<<<<<<<<<<<<<<<<<
False, # Don't force start cumul to zero.
time)
I changed it to a bigger number (99) and all the crap works very fine :)

Related

How do I compare List<List<int>> to exist in other List<List<int>>

I want to know in Flutter if my sequence exists in the other sequenceTwo. For the example, I made two identical List<List<int>> that work. But I also want to know if it exists even if there are more record before or after the sequence as in the next check I do in the code below.
List<List<int>> sequence = [];
sequence.add(Uint8List.fromList([42, 6, 1, 8, 6, 63, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 9, 66, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 0, 57, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 3, 60, 13, 10]));
List<List<int>> sequenceTwo = [];
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 6, 63, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 9, 66, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 0, 57, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 3, 60, 13, 10]));
Function deepEqOne = const DeepCollectionEquality().equals;
debugPrint(
'DeepCollection: ' + deepEqOne(sequence, sequenceTwo).toString()); // true so works!
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 9, 3, 60, 13, 10]));
debugPrint(
'DeepCollection: ' + deepEqOne(sequence, sequenceTwo).toString()); // false but I want this also to work somehow

How to manage the hidden state dims when using pad_sequence?

Using Pytorch LSTM architecture trying to build a text generation model. For every batch, I'm using pad_sequence to have min padding for every sequence, therefore I have a variable dims batch (batch_size * seq_len). I'm applying also pack_padded_seq to only give the non-zero (non-padding) tokens to the LSTM. But, the variable dims batch throws an error while feeding it to the LSTM as following; Expected hidden[0] size (1, 8, 16), got (1, 16, 16). In this error, I have provided batch size 16 with 8 tokens for every sequence, but the hidden state is 16 * 16.
I have tried to create the hidden state in the forward function, but that did not work well. How can I create the hidden state such that it can accept variable dims batch and it will not be lost for the whole epoche?
class RNNModule(nn.Module):
def __init__(self, n_vocab, seq_size, embedding_size, lstm_size):
super(RNNModule, self).__init__()
self.seq_size = seq_size
self.lstm_size = lstm_size
self.embedding, num_embeddings, embedding_dim = create_emb_layer(weight_matrix, False)
self.lstm = nn.LSTM(embedding_size,
lstm_size,
num_layers=flags.n_layers,
batch_first=True
)
self.dense = nn.Linear(lstm_size, n_vocab)
def forward(self, x,length,prev_state):
embed = self.embedding(x)
packed_input = db.pack_src(embed,length)
packed_output, state = self.lstm(packed_input,prev_state)
padded,_ = db.pad_pack(packed_output)
logits = self.dense(padded)
return logits, state
def zero_state(self, batch_size = flags.batch_size):
return (torch.zeros(flags.n_layers, batch_size, self.lstm_size),
torch.zeros(flags.n_layers, batch_size, self.lstm_size))
input: tensor([[ 19, 9, 4, 3, 68, 8, 6, 2],
[ 19, 9, 4, 3, 7, 8, 6, 2],
[ 3, 12, 17, 10, 6, 40, 2, 0],
[ 4, 3, 109, 7, 6, 2, 0, 0],
[ 188, 6, 7, 18, 3, 2, 0, 0],
[ 4, 3, 12, 6, 7, 2, 0, 0],
[ 6, 7, 3, 13, 2, 0, 0, 0],
[ 3, 28, 17, 69, 2, 0, 0, 0],
[ 6, 3, 12, 11, 2, 0, 0, 0],
[ 3, 13, 6, 7, 2, 0, 0, 0],
[ 3, 6, 7, 13, 2, 0, 0, 0],
[ 6, 3, 23, 7, 2, 0, 0, 0],
[ 3, 28, 10, 2, 0, 0, 0, 0],
[ 6, 3, 23, 2, 0, 0, 0, 0],
[ 3, 6, 37, 2, 0, 0, 0, 0],
[1218, 2, 0, 0, 0, 0, 0, 0]])
Zero tokens are padding.
Embedding size: 64
LSTM size: 16
batch size: 16
The size of the hidden state you create has the correct size, but your input does not. When you pack it with nn.utils.rnn.pack_padded_sequence you've set batch_first=False, but your data has size [batch_size, seq_len, embedding_size] when you pass it to the packing, so that has batch_size as the first dimension. Also for the LSTM you use batch_first=True, which is appropriate for your data.
You only need to pack it correctly by setting batch_first=True as well, to match the order of your data.
rnn_utils.pack_padded_sequence(embed,length,batch_first=True)

Searching mapped list to return higher values

My current system can find the last value of a list of integers, shown in the first file below. Then it displays this as the "Current" value. I need to be able to find all of the "Current" values when I enter a search and return all of the results higher than the one I searched for.The list is saved as a Map(String, List[Int]).
SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6
This is what I am trying to get; using the data file from above, If I searched for "SK5" the system would return;
Figures higher than SK5 - 3
SK4 - 8
SK6 - 9
SK8 - 6
SK9 - 6
Here is my current code to find the last number in the list
//5 - Show Current Stock Level (W)
def handleFive(): Boolean = {
mnuShowSingleDataStock(currentStockLevel)
true
}
// Returns a single result, not a list
def mnuShowSingleDataStock(stock: (String) => (String,Int)) = {
print("Stock > ")
val data = stock(readLine)
println(s"${data._1}: ${data._2}")
}
//Show higher than stocks
def higherThan(stock: String): List[(String, List[Int])] = {
mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(_.last).getOrElse(0))
}
Sort based on the last value and then take while current stock value is greater than the searched stock value
def higherThan(stock: String): List[(String, List[Int])] = {
mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(_.last).getOrElse(0))
}

Formatting output scala lists

Currently I can search for a value, say SK5 and it will return all values higher than SK5. However the format it is returned in as shown below;
List((SK6,List(2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2,
4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9)), (SK4,List(2, 9, 5, 7, 0, 8, 6,
6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8)),
(SK8,List(2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7,
0, 9, 5, 2, 5, 0, 2, 1, 8, 6)), (SK9,List(7, 1, 8, 8, 4, 4, 2, 2, 7,
4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6)),
(SK5,List(2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9,
3, 8, 6, 7, 8, 7, 4, 1, 2, 3)))
What I want is SK6 - 9, SK4 - 8 etc etc
All bundled together, How would I split this up and only show the last number in the list? I thought I had already filtered this out however apparently not.
Below is my code. Mapdata is saved as Map(String, List[Int])
//functionality to find the last tail element, the "Current" stock price
def findLast(list:List[Int]) = list.last
//8 - Show Stocks Higher Than (W) THIS ONE THIS ONE THIS ONE
def handleEight(): Boolean = {
mnuShowPointsForStockHigher(higherThan2)
true
}
//Returns a list value
def mnuShowPointsForStockHigher(stock: (String) => List[(String, List[Int])]) = {
print("Enter Stock > ")
val data = stock(readLine)
println(s"${data}")
//println(s"${data._1}: ${data._2.last}")
}
def higherThan2(stock: String): List[(String, List[Int])] = {
mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(findLast(_)).getOrElse(0))
}
If you are trying to get the last value in each list, Best option will be map + last. Sorry, don't need to use flatten. My bad .. Should be something line: list.map(x => x.last)

Scala Type Mismatch Mapping

I am trying to create a search function, so the user can search through a list using the key value, however the method Im trying to use returns a type mismatch, Have taken out needless code and shown what is required. How do I set points to take an Int and not "Any"?
"type Any does not conform to type Int"
val mapdata = readFile("data.txt")
def handleTwo(): Boolean = {
mnuShowPointsForTeam(currentPointsForTeam)
true
}
def mnuShowPointsForTeam(f: (String) => (String, Int)) = {
print("Team>")
val data = f(readLine)
println(s"${data._1}: ${data._2}")
}
def currentPointsForTeam(team: String): (String, Int) = {
val points = mapdata.get(team) match{
case Some(p) => p
case None => 0
}
(team, points)
}
The data.txt
SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6
It looks like you want to return a tuple with a List[Int], not just a single Int.
If so
def currentPointsForTeam(team: String): (String, List[Int]) =
(team, mapdata.get(team).getOrElse(List.empty))
// Or maybe List(0) instead of List.empty
If you do want to return a single Int, you have to say how to go from the List[Int] in the map to a single value. Maybe a sum?
def currentPointsForTeam(team: String): (String, Int) =
(team, mapdata.get(team).map(_.sum).getOrElse(0))