-
Notifications
You must be signed in to change notification settings - Fork 4
/
Weighted.scala
153 lines (123 loc) · 5.43 KB
/
Weighted.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
/*
* Copyright 2021 Arman Bilge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schrodinger.montecarlo
import algebra.ring.CommutativeRig
import algebra.ring.MultiplicativeCommutativeGroup
import algebra.ring.MultiplicativeCommutativeMonoid
import algebra.ring.MultiplicativeGroup
import algebra.ring.MultiplicativeMonoid
import algebra.ring.Rig
import algebra.ring.Semifield
import algebra.ring.Semiring
import cats.Align
import cats.Applicative
import cats.CommutativeMonad
import cats.Eq
import cats.Functor
import cats.Id
import cats.Invariant
import cats.InvariantMonoidal
import cats.InvariantSemigroupal
import cats.Monad
import cats.Monoid
import cats.Semigroup
import cats.Show
import cats.data.Ior
import cats.kernel.CommutativeMonoid
import cats.kernel.CommutativeSemigroup
import cats.kernel.Hash
import cats.kernel.Order
import cats.syntax.all.*
import schrodinger.math.syntax.*
import schrodinger.montecarlo.Weighted.Heavy
import schrodinger.montecarlo.Weighted.Weightless
import scala.annotation.tailrec
import cats.kernel.Group
sealed abstract class Weighted[W, A] extends Product, Serializable:
def valueOption: Option[A]
def weight: W
def density: W
def isWeightless: Boolean
final def isHeavy: Boolean = !isWeightless
final def imap[B](f: A => B)(g: B => A): Weighted[W, B] = this match
case Heavy(w, d, a) => Heavy(w, d, f(a))
case Weightless(w) => new Weightless(w)
final def product[B](b: Weighted[W, B])(using Semiring[W], Eq[W]): Weighted[W, (A, B)] =
(this, b) match
case (Heavy(wa, da, a), Heavy(wb, db, b)) => Weighted(wa * wb, da * db, (a, b))
case (Weightless(w), _) => new Weightless(w)
case (_, Weightless(w)) => new Weightless(w)
final def importance(f: A => W)(using Semifield[W], Eq[W]): Weighted[W, A] =
importanceA[Id](f)
final def importanceA[F[_]: Applicative](
f: A => F[W])(using Semifield[W], Eq[W]): F[Weighted[W, A]] =
this match
case Heavy(w, d, a) =>
f(a).map(fa => Weighted(w * (fa / d), fa, a))
case weightless => weightless.pure
final override def toString: String =
show(using Show.fromToString, Show.fromToString)
final def show[B >: A: Show](using Show[W]): String = this match
case Heavy(w, d, a) => s"Heavy(${w.show}, ${d.show}, ${(a: B).show})"
case Weightless(w) => s"Weightless(${w.show})"
final def ===[B >: A: Eq](that: Weighted[W, B])(using Eq[W]): Boolean =
(this, that) match
case (Heavy(wa, da, a), Heavy(wb, db, b)) => wa === wb && da === db && b === a
case (Weightless(wa), Weightless(wb)) => wa === wb
case _ => false
object Weighted extends WeightedInstances:
def apply[W: Eq, A](weight: W, density: W, value: A)(using W: Semiring[W]): Weighted[W, A] =
if W.isZero(weight) then weightless[W, A]
else Heavy(weight, density, value)
def apply[W, A](density: W, value: A)(using W: MultiplicativeMonoid[W]): Weighted[W, A] =
Heavy(W.one, density, value)
def pure[W, A](a: A)(using W: MultiplicativeMonoid[W]): Weighted[W, A] =
Heavy(W.one, W.one, a)
def weightless[W, A](using W: Semiring[W]): Weighted[W, A] = Weightless[W, A]
final case class Heavy[W, A](weight: W, density: W, value: A) extends Weighted[W, A]:
def isWeightless: false = false
def valueOption: Some[A] = Some(value)
final case class Weightless[W, A](weight: W) extends Weighted[W, A]:
def density: W = weight
def isWeightless: true = true
def valueOption: None.type = None
object Weightless:
def apply[W, A](using W: Semiring[W]): Weightless[W, A] =
Weightless(W.zero)
sealed private[montecarlo] class WeightedInstances extends WeightedInstances0:
given [F[_], W](using Rig[W], Eq[W]): InvariantMonoidal[Weighted[W, _]] =
WeightedInvariantMonoidal[W]
given [W: Order, A: Order]: Order[Weighted[W, A]] =
Order.by(w => (w.weight, w.density, w.valueOption))
given [W: Show, A: Show]: Show[Weighted[W, A]] with
def show(t: Weighted[W, A]): String = t.show
sealed private[montecarlo] class WeightedInstances0 extends WeightedInstances1:
given [W]: Invariant[Weighted[W, _]] = WeightedInvariant[W]
given [W: Hash, A: Hash]: Hash[Weighted[W, A]] =
Hash.by(w => (w.weight, w.density, w.valueOption))
sealed private[montecarlo] class WeightedInstances1:
given [W: Eq, A: Eq]: Eq[Weighted[W, A]] with
def eqv(x: Weighted[W, A], y: Weighted[W, A]): Boolean = x === y
sealed private[montecarlo] class WeightedInvariant[W] extends Invariant[Weighted[W, _]]:
def imap[A, B](fa: Weighted[W, A])(f: A => B)(g: B => A) = fa.imap(f)(g)
sealed private[montecarlo] class WeightedInvariantSemigroupal[W](using Semiring[W], Eq[W])
extends WeightedInvariant[W],
InvariantSemigroupal[Weighted[W, _]]:
def product[A, B](wa: Weighted[W, A], wb: Weighted[W, B]) = wa.product(wb)
sealed private[montecarlo] class WeightedInvariantMonoidal[W](using Rig[W], Eq[W])
extends WeightedInvariantSemigroupal[W],
InvariantMonoidal[Weighted[W, _]]:
def unit = Weighted.pure(())