-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReina.scala
146 lines (113 loc) · 3.94 KB
/
Reina.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import scala.actors.{Actor, OutputChannel, TIMEOUT}
import scala.actors.Actor._
import scala.actors.remote.{RemoteActor, Node}
import scala.actors.remote.RemoteActor._
import scala.actors.Debug
import scala.collection.mutable.Map
import Params._
import java.util.Properties
import java.io.FileInputStream
object ReinaMain {
def main(args: Array[String]) {
if (args.length < 2) {
println("Reina input minutos")
exit
}
// arranco la reina
val delta = if (args.length > 2) args(2).toDouble else δ
val reina = new Reina(args(0), args(1).toInt, delta, 9010, 'ACS)
reina.start()
// arranco hormigas en los nucleos
val cores = Runtime.getRuntime().availableProcessors()
var v = true
for (i <- 1 to cores) {
if (v) {
new RAR("localhost", 9010, 'ACS, i%2!=0).start()
}
else {
new Formica("localhost", 9010, 'ACS).start()
}
// v = !v
}
}
}
class Reina(file: String, min: Int, delta: Double, port: Int, name: Symbol) extends Actor {
RemoteActor.classLoader = getClass().getClassLoader()
val hormigas = Map.empty[String, OutputChannel[Any]]
val counter = Map.empty[String, Int]
val properties = new Properties()
try {
properties.load(new FileInputStream("params.properties"));
}
val rotura:Double = java.lang.Double.parseDouble(properties.getProperty("rotura", "50"))
val roturaV:Double = java.lang.Double.parseDouble(properties.getProperty("roturaV", "50"))
val lsRAR:Boolean = java.lang.Boolean.parseBoolean(properties.getProperty("lsRAR", "false"))
val lsRARV:Boolean = java.lang.Boolean.parseBoolean(properties.getProperty("lsRARV", "false"))
val inst = Solomon.load(file)
val solver = new NearestNeighbour(inst)
// nearest neighbour optimizado
var mejor = new LocalSearch(inst, solver.solve).search()
var mejorLargo = inst.solLength(mejor)
var mejorVehiculos = mejor.length
inst.globalTau(mejor)
τ0 = 1 / inst.customers.length * mejorLargo
// actualizo el maximo de vehiculos permitidos a lo que me dio NN
inst.vehiculos = mejorVehiculos
if (!inst.factible(mejor)) {
println("NN no factible!!")
println("visitados: " + mejor.foldLeft(0)(_ + _.size - 1))
exit
}
println("NN: " + mejorLargo + " | " + mejorVehiculos + " | prom/vehiculo: " +
mejor.foldLeft(0)(_ + _.size - 1).toFloat / mejorVehiculos.toFloat)
println("δ: " + delta)
val queenActress = select(Node("localhost", 9010), 'ACS)
// helper para cortar el main loop
actor {
Thread.sleep(min * 60 * 1000)
queenActress ! TIMEOUT
}
def act {
alive(port)
register(name, self)
var running = true
val startTime = System.currentTimeMillis
while(running) {
receive {
case Hello(id) => {
// una hormiga comun
hormigas + ((id, sender))
sender ! Start(inst, mejor, delta, rotura, roturaV, lsRAR, lsRARV)
}
case Mejor(newMejor, id) => {
// chequeo que efectivamente sea mejor
val newLargo = inst.solLength(newMejor)
val newVehiculos = newMejor.length
if (newVehiculos < mejorVehiculos ||
(newVehiculos == mejorVehiculos && newLargo < mejorLargo)) {
mejor = newMejor
mejorLargo = newLargo
mejorVehiculos = newVehiculos
// actualizo a las hormigas largueras
hormigas.filterKeys(uid => uid != id).foreach(p => p._2 ! Mejor(mejor, ""))
counter + ((id, counter.getOrElse(id, 0)+1))
val t = System.currentTimeMillis - startTime
println("["+t+"] " + id + " --> Mejor: " + newLargo + " | " + newVehiculos)
// sobreescribo feromonas, para mandar lo actualizado si se une una hormiga nueva
//inst overwriteTau(newTau)
}
}
case TIMEOUT => {
println("TIMEOUT")
running = false
println("mejor solucion = " + mejor.map(_.map(_.num)))
println("largo = " + inst.solLength(mejor))
println("vehiculos = " + mejor.length)
println(counter)
// fue, mando Stop al resto
hormigas.foreach(p => p._2 ! Stop)
}
}
}
}
}