This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TemporalGradientRL.scala
151 lines (137 loc) · 6.17 KB
/
TemporalGradientRL.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
package it.unibo.scafi.casestudy.algorithm.gradient
import cats.data.NonEmptySet
import it.unibo.Logging
import it.unibo.alchemist.model.implementations.nodes.NodeManager
import it.unibo.alchemist.model.scafi.ScafiIncarnationForAlchemist.{ScafiAlchemistSupport, _}
import it.unibo.learning.Q.MutableQ
import it.unibo.learning.{Policy, Q, QLearning}
import it.unibo.scafi.casestudy.algorithm.RLLike
import it.unibo.scafi.casestudy.algorithm.RLLike.AlgorithmHyperparameter
import it.unibo.scafi.casestudy.algorithm.gradient.TemporalGradientRL._
import it.unibo.scafi.casestudy.{GlobalStore, GradientLikeLearning, TemporalStateManagement}
import it.unibo.storage.LocalStorage
import upickle.default.{macroRW, ReadWriter => RW}
import scala.util.Random
trait TemporalGradientRL extends RLLike {
self: AggregateProgram
with TemporalStateManagement
with GradientLikeLearning
with ScafiAlchemistSupport
with StandardSensors =>
// move to another part?
implicit class DoubleWithAlmostEquals(val d: Double) {
def default = 0.00001
def ~=(d2: Double) = (d - d2).abs < default
}
class TemporalRLAlgorithm(
parameter: AlgorithmHyperparameter,
actionSet: NonEmptySet[Action],
radius: Double,
maxBound: Int,
bucketsCount: Int,
windowDifferenceSize: Int
)(implicit rand: Random)
extends AlgorithmTemplate[History, Action] {
private val max = maxBound * bucketsCount
private val steps = maxBound * 2
private val iterableSteps = -max to max by steps
private val buckets =
iterableSteps.reverse.drop(1).reverse.zip(iterableSteps.drop(1)).map { case (min, max) =>
Slot(min / bucketsCount.toDouble, max / bucketsCount.toDouble)
}
override val name: String = "temporalRL"
override protected def learning: QLearning.Type[History, Action] =
QLearning.Hysteretic[History, Action](actionSet, parameter.alpha, parameter.beta, parameter.gamma)
override protected def state(output: Double, action: Action): History = {
def evalState(out: Double): GradientDifference = {
val diff = output - out
if ((diff ~= 0)) {
Same
} else if (diff > maxBound * radius) {
GreaterBound
} else if (diff < maxBound * -radius) {
SmallerBound
} else {
@SuppressWarnings(Array("org.wartremover.warts.All")) // because serialization with case object
val slot =
buckets.find { case Slot(min, max) => diff > min * radius && diff < max * radius }.getOrElse(Same)
slot
}
}
val minOutput = evalState(minHood(nbr(output)))
val maxOutput = evalState(maxHood(nbr(output)))
val recent = recentValues(windowDifferenceSize, State(maxOutput, minOutput))
History(recent.toList)
}
override protected def actionEffect(oldOutput: Double, state: History, action: Action): Double =
mux(source)(0.0) {
val result = minHoodPlus(nbr(oldOutput) + nbrRange())
action match {
case TemporalGradientRL.ConsiderNeighbourhood => result
case TemporalGradientRL.Ignore(upVelocity) =>
oldOutput + upVelocity * (deltaTime().toMillis.toDouble / 1000.0)
}
}
override protected def initialState: History = History(Seq.empty)
override protected def q: Q[History, Action] = {
if (node.has("simulation_id")) {
GlobalStore.get[Q[History, Action]](node.get("simulation_id"))
} else {
TemporalGradientRL.q
}
}
override def episodeEnd(nodes: Iterable[NodeManager]): Unit = {
Logging().warn(":::::CHECK GREEDY POLICY:::::")
q match {
case MutableQ(initialConfig) =>
val states = initialConfig.keys.map(_._1)
Logging().warn(s"STATE VISITED: ${states.size.toString}")
}
if (node.has("simulation_id")) {
storage.save(node.get("simulation_id"), q)
}
}
override protected def rewardSignal(output: Double): Double = {
if (((peekReference - output) ~= 0) || (output.isInfinite && peekReference.isInfinite)) {
0
} else { -1 }
}
}
}
object TemporalGradientRL {
sealed trait GradientDifference
case object GreaterBound extends GradientDifference
case object SmallerBound extends GradientDifference
case object Same extends GradientDifference
case class Slot(startMultiplier: Double, endMultiplier: Double) extends GradientDifference
case class State(minDifference: GradientDifference, maxDifference: GradientDifference)
case class History(states: Seq[State])
sealed trait Action
implicit def ordering: Ordering[Action] = (x: Action, y: Action) =>
(x, y) match {
case (ConsiderNeighbourhood, Ignore(_)) => 1
case (Ignore(_), ConsiderNeighbourhood) => -1
case (ConsiderNeighbourhood, ConsiderNeighbourhood) => 0
case (Ignore(l), Ignore(r)) => Ordering.Double.IeeeOrdering.compare(l, r)
}
case class Ignore(upVelocity: Double) extends Action
case object ConsiderNeighbourhood extends Action
val storage = new LocalStorage[String]("table")
val q: MutableQ[History, Action] = new MutableQ[History, Action](Map.empty.withDefault(_ => 0.0))
//new MutableQ(
// storage.load[MutableQ[History, Action]]("q").initialConfig.withDefault(_ => 0.0)
//) //
// for the storage
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForGradientDifference: RW[GradientDifference] = macroRW[GradientDifference]
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForSlot: RW[Slot] = macroRW[Slot]
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForAction: RW[Action] = macroRW[Action]
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForIgnore: RW[Ignore] = macroRW[Ignore]
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForState: RW[State] = macroRW[State]
@SuppressWarnings(Array("org.wartremover.warts.All")) // because of macro expansion
implicit def storageForHistory: RW[History] = macroRW[History]
}