-
Notifications
You must be signed in to change notification settings - Fork 11
/
StandardAnnealingMonitor.scala
167 lines (145 loc) · 6.54 KB
/
StandardAnnealingMonitor.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package yuck.annealing
import scala.collection._
import yuck.core._
import yuck.util.arm.ManagedResource
import yuck.util.logging.LazyLogger
/**
* A managed resource for logging solver events.
*
* @author Michael Marte
*/
class StandardAnnealingMonitor(
logger: LazyLogger)
extends AnnealingMonitor with ManagedResource
{
private trait ThreadState
private case object ThreadIsIdle extends ThreadState
private case object SolverIsRunnable extends ThreadState
private case object SolverIsRunning extends ThreadState
private var solverState = new ThreadLocal[ThreadState] {
override def initialValue = ThreadIsIdle
}
private var costsOfBestProposal: Costs = null
override def open {
if (solverState.get == SolverIsRunnable) {
}
}
override def close {
if (solverState.get == SolverIsRunning) {
}
}
override def onSolverLaunched(result: AnnealingResult) {
require(solverState.get == ThreadIsIdle)
solverState.set(SolverIsRunnable)
open
logger.log("Launched solver")
solverState.set(SolverIsRunning)
}
override def onSolverSuspended(result: AnnealingResult) {
require(solverState.get == SolverIsRunning)
close
solverState.set(SolverIsRunnable)
if (result.roundLogs.isEmpty) {
logger.log("Suspended solver before first round")
} else {
logger.criticalSection {
logger.log("Suspended solver in round %d".format(result.roundLogs.size - 1))
logStatistics(result)
captureSolverStatistics(result)
}
}
}
override def onSolverResumed(result: AnnealingResult) {
require(solverState.get == SolverIsRunnable)
if (result.roundLogs.isEmpty) {
logger.log("Resumed solver that was suspended before first round")
} else {
logger.log("Resumed solver in round %d".format(result.roundLogs.size - 1))
}
solverState.set(SolverIsRunning)
open
}
override def onSolverFinished(result: AnnealingResult) {
require(solverState.get == SolverIsRunning)
close
solverState.set(ThreadIsIdle)
logger.criticalSection {
logger.log(
"Solver finished with proposal of quality %s in round %d".format(
result.costsOfBestProposal, result.roundLogs.size - 1))
logStatistics(result)
captureSolverStatistics(result)
}
}
override def onNextRound(result: AnnealingResult) {
logger.loggg("%s".format(result.roundLogs.last.toString))
}
override def onBetterProposal(result: AnnealingResult) {
synchronized {
if (costsOfBestProposal == null ||
result.objective.isLowerThan(result.costsOfBestProposal, costsOfBestProposal))
{
costsOfBestProposal = result.costsOfBestProposal
logger.log(
"Improved global proposal quality to %s in round %d".format(
costsOfBestProposal, result.roundLogs.size - 1))
} else {
logger.logg(
"Improved local proposal quality to %s in round %d".format(
result.costsOfBestProposal, result.roundLogs.size - 1))
}
if (result.isGoodEnough) {
logger.log("Objective achieved")
}
}
}
override def onReheatingStarted(result: AnnealingResult) {
val roundLog = result.roundLogs.last
logger.logg(
"Reheating started after round %d from uphill acceptance ratio %1.6f at temperature %3.6f".format(
result.roundLogs.size - 1, roundLog.uphillAcceptanceRatio, roundLog.temperature))
}
override def onReheatingFinished(result: AnnealingResult) {
val roundLog = result.roundLogs.last
logger.logg(
"Reheating finished after round %d with uphill acceptance ratio %1.6f at temperature %3.6f".format(
result.roundLogs.size - 1, roundLog.uphillAcceptanceRatio, roundLog.temperature))
}
override def onObjectiveTightened(x: AnyVariable) {
logger.logg("Reduced domain of objective variable %s to %s".format(x, x.domain))
}
private def logStatistics(result: AnnealingResult) {
logger.withLogScope("Solver statistics".format(result.solverName)) {
logger.log("Number of rounds: %d".format(result.roundLogs.size))
if (result.roundLogs.size > 0) {
logger.log("Moves per second: %f".format(result.movesPerSecond))
logger.log("Consultations per second: %f".format(result.consultationsPerSecond))
logger.log("Consultations per move: %f".format(result.consultationsPerMove))
logger.log("Commitments per second: %f".format(result.commitmentsPerSecond))
logger.log("Commitments per move: %f".format(result.commitmentsPerMove))
}
}
}
private class SolverStatistics(
val runtimeInSeconds: Double, val movesPerSecond: Double,
val consultationsPerSecond: Double, val consultationsPerMove: Double,
val commitmentsPerSecond: Double, val commitmentsPerMove: Double)
private val solverStatistics = new mutable.ArrayBuffer[SolverStatistics]
private def captureSolverStatistics(result: AnnealingResult) {
if (! result.roundLogs.isEmpty) {
solverStatistics +=
new SolverStatistics(
result.runtimeInSeconds, result.movesPerSecond,
result.consultationsPerSecond, result.consultationsPerMove,
result.commitmentsPerSecond, result.commitmentsPerMove)
}
}
def wasSearchRequired: Boolean = ! solverStatistics.isEmpty
def numberOfRestarts: Int = scala.math.max(0, solverStatistics.size - 1)
def runtimeInSeconds: Double = solverStatistics.toIterator.map(_.runtimeInSeconds).sum
def movesPerSecond: Double = solverStatistics.toIterator.map(_.movesPerSecond).sum / solverStatistics.size
def consultationsPerSecond: Double = solverStatistics.toIterator.map(_.consultationsPerSecond).sum / solverStatistics.size
def consultationsPerMove: Double = solverStatistics.toIterator.map(_.consultationsPerMove).sum / solverStatistics.size
def commitmentsPerSecond: Double = solverStatistics.toIterator.map(_.commitmentsPerSecond).sum / solverStatistics.size
def commitmentsPerMove: Double = solverStatistics.toIterator.map(_.commitmentsPerMove).sum / solverStatistics.size
}