-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocalInsert.scala
75 lines (59 loc) · 1.94 KB
/
LocalInsert.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import scala.collection.Map
class LocalInsert(inst: Instance, solucion: List[List[Customer]]) {
var mejor = solucion
val ininsertables = new scala.collection.mutable.ListBuffer[Customer]()
var nnv = List[Customer]()
def insert(nonvisit: List[Customer]): List[List[Customer]] = {
// println("\n********insert()********")
// println("nonvisit (" + nonvisit.size + ") = " + nonvisit.map(_.num))
// pruebo meter los no visitados
nonvisit.foreach(insert)
if (ininsertables.size == 0) {
mejor
}
else {
nnv = nonvisit
var i = 0
while (ininsertables.size > 0 && i < 15) {
i = i + 1
nnv = ininsertables.toList ++ (nnv -- ininsertables.toList)
println("retry " + i)
println("ininsertables = " + ininsertables.map(_.num).toList)
println("nueva nonvisit = " + nnv.map(_.num))
ininsertables.clear
mejor = solucion // intento con la solucion inicial
nnv.foreach(insert)
}
// if (ininsertables.size == 0) {
// println("try " + i + " es factible")
// }
// else {
// println("infactible...")
// }
mejor
}
}
private def tryInsert(cust: Customer): List[List[List[Customer]]] = {
var factibles = new scala.collection.mutable.ListBuffer[List[List[Customer]]]()
mejor.foreach { camion =>
for (pos <- 1 to camion.length) {
val nuevo = camion.take(pos) ++ (cust :: camion.drop(pos))
if (inst.camionFactible(nuevo)) {
factibles += nuevo :: (mejor - camion)
}
}
}
factibles.toList
}
private def insert(nv: Customer) = {
val factibles = tryInsert(nv)
// println("hay " + factibles.size + " opciones para insertar el cliente [" + nv.num + "]")
// me quedo con el de menor largo
if (factibles.size > 0) {
mejor = factibles.tail.foldLeft(factibles.head){ (a,b) => if (inst.solLength(a) < inst.solLength(b)) a else b }
}
else if (factibles.size == 0) {
ininsertables + nv
}
}
}