diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/AtLeastTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/AtLeastTile.soda index f749492..e2d8c42 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/AtLeastTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/AtLeastTile.soda @@ -7,7 +7,9 @@ class AtLeastTile : TileMessage [Boolean] = TileMessageBuilder_ () .build (message .context) (message .outcome) ( ( (message .contents) - .map (lambda pair --> (pair .fst .compareTo (pair .snd) >= 0 ) ) + .map (lambda pair --> + (Comparator_ () + .compareToMeasure (pair .fst) (pair .snd) ) >= 0 ) .forall (lambda e --> e) ) ) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/CorrelationTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/CorrelationTile.soda index 6991b3e..e855f9a 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/CorrelationTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/CorrelationTile.soda @@ -3,7 +3,7 @@ class CorrelationTile abstract - _measure_zero = Measure_ (0) + _measure_zero : Measure = Some (0) _percentage_constant : Double = 100.0 @@ -16,7 +16,7 @@ class CorrelationTile else 1.0 to_measure (d : Double) : Measure = - Measure_ ( (d * _percentage_constant) .intValue) + Some ( (d * _percentage_constant) .intValue) get_fst_list (lists : Seq [TilePair [Measure] [Measure] ] ) : Seq [Double] = lists .map (lambda pair --> to_double (pair .fst) ) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/DecisionTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/DecisionTile.soda index c70ba4c..c14db89 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/DecisionTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/DecisionTile.soda @@ -5,7 +5,8 @@ class DecisionTile maximum_acceptable_bias_percentage : Measure to_boolean (m : Measure) : Boolean = - m .value <= maximum_acceptable_bias_percentage .value + ( Comparator_ () + .compareToMeasure (m) (maximum_acceptable_bias_percentage) ) <= 0 apply (message : TileMessage [Measure] ) : TileMessage [Boolean] = TileMessageBuilder_ () .build (message .context) (message .outcome) ( diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/FalsePosTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/FalsePosTile.soda index cf43c80..48e4567 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/FalsePosTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/FalsePosTile.soda @@ -3,9 +3,9 @@ class FalsePosTile abstract - _measure_zero = Measure_ (0) + _measure_zero : Measure = Some (0) - _measure_one = Measure_ (1) + _measure_one : Measure = Some (1) sigma (m0 : Measure) (m1 : Measure) : Measure = if (m0 == _measure_one) and (m1 == _measure_zero) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/Package.scala b/tiles/src/main/scala/soda/tiles/fairness/tile/Package.scala index 5cd816e..5940349 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/Package.scala +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/Package.scala @@ -6,8 +6,9 @@ package soda.tiles.fairness.tile import soda.tiles.fairness.tool.Actor import soda.tiles.fairness.tool.Assignment +import soda.tiles.fairness.tool.Comparator +import soda.tiles.fairness.tool.Comparator_ import soda.tiles.fairness.tool.Measure -import soda.tiles.fairness.tool.Measure_ import soda.tiles.fairness.tool.Outcome import soda.tiles.fairness.tool.Pearson import soda.tiles.fairness.tool.Pearson_ @@ -147,7 +148,9 @@ trait AtLeastTile : TileMessage [Boolean] = TileMessageBuilder_ () .build (message .context) (message .outcome) ( ( (message .contents) - .map ( pair => (pair .fst .compareTo (pair .snd) >= 0 ) ) + .map ( pair => + (Comparator_ () + .compareToMeasure (pair .fst) (pair .snd) ) >= 0 ) .forall ( e => e) ) ) @@ -188,7 +191,7 @@ trait CorrelationTile - private lazy val _measure_zero = Measure_ (0) + private lazy val _measure_zero : Measure = Some (0) private lazy val _percentage_constant : Double = 100.0 @@ -201,7 +204,7 @@ trait CorrelationTile else 1.0 def to_measure (d : Double) : Measure = - Measure_ ( (d * _percentage_constant) .intValue) + Some ( (d * _percentage_constant) .intValue) def get_fst_list (lists : Seq [TilePair [Measure, Measure] ] ) : Seq [Double] = lists .map ( pair => to_double (pair .fst) ) @@ -234,7 +237,8 @@ trait DecisionTile def maximum_acceptable_bias_percentage : Measure def to_boolean (m : Measure) : Boolean = - m .value <= maximum_acceptable_bias_percentage .value + ( Comparator_ () + .compareToMeasure (m) (maximum_acceptable_bias_percentage) ) <= 0 def apply (message : TileMessage [Measure] ) : TileMessage [Boolean] = TileMessageBuilder_ () .build (message .context) (message .outcome) ( @@ -335,9 +339,9 @@ trait FalsePosTile - private lazy val _measure_zero = Measure_ (0) + private lazy val _measure_zero : Measure = Some (0) - private lazy val _measure_one = Measure_ (1) + private lazy val _measure_one : Measure = Some (1) def sigma (m0 : Measure) (m1 : Measure) : Measure = if ( (m0 == _measure_one) && (m1 == _measure_zero) @@ -381,7 +385,7 @@ trait PredictionPTile def p : Resource => Measure - private lazy val _measure_zero : Measure = Measure_ (0) + private lazy val _measure_zero : Measure = Some (0) def measure_or (m0 : Measure) (m1 : Measure) : Measure = if ( (m0 == _measure_zero) @@ -410,7 +414,7 @@ trait ReceivedSigmaPTile private def _sigma2 (m0 : Measure , m1 : Measure) : Measure = sigma (m0) (m1) - private lazy val _measure_zero : Measure = Measure_ (0) + private lazy val _measure_zero : Measure = Some (0) def get_assignment (assignments : Seq [Assignment] ) (a : Actor) : Option [Assignment] = assignments . find ( assignment => (assignment .actor) == a) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/Package.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/Package.soda index a4b25f7..77b96c8 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/Package.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/Package.soda @@ -7,8 +7,9 @@ package soda.tiles.fairness.tile import soda.tiles.fairness.tool.Actor soda.tiles.fairness.tool.Assignment + soda.tiles.fairness.tool.Comparator + soda.tiles.fairness.tool.Comparator_ soda.tiles.fairness.tool.Measure - soda.tiles.fairness.tool.Measure_ soda.tiles.fairness.tool.Outcome soda.tiles.fairness.tool.Pearson soda.tiles.fairness.tool.Pearson_ diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/PredictionPTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/PredictionPTile.soda index 6e7a7bc..5e85cfc 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/PredictionPTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/PredictionPTile.soda @@ -4,7 +4,7 @@ class PredictionPTile abstract p : Resource -> Measure - _measure_zero : Measure = Measure_ (0) + _measure_zero : Measure = Some (0) measure_or (m0 : Measure) (m1 : Measure) : Measure = if (m0 == _measure_zero) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tile/ReceivedSigmaPTile.soda b/tiles/src/main/scala/soda/tiles/fairness/tile/ReceivedSigmaPTile.soda index 2e47dea..dbc99db 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tile/ReceivedSigmaPTile.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tile/ReceivedSigmaPTile.soda @@ -8,7 +8,7 @@ class ReceivedSigmaPTile _sigma2 (m0 : Measure , m1 : Measure) : Measure = sigma (m0) (m1) - _measure_zero : Measure = Measure_ (0) + _measure_zero : Measure = Some (0) get_assignment (assignments : Seq [Assignment] ) (a : Actor) : Option [Assignment] = assignments . find (lambda assignment --> (assignment .actor) == a) diff --git a/tiles/src/main/scala/soda/tiles/fairness/tool/Entity.soda b/tiles/src/main/scala/soda/tiles/fairness/tool/Entity.soda index 965e730..e61861c 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tool/Entity.soda +++ b/tiles/src/main/scala/soda/tiles/fairness/tool/Entity.soda @@ -11,23 +11,39 @@ class Resource = Identifier class Context = Identifier -class Measure - extends - Comparable [Measure] +class Measure = Option [Int] + + +class Comparator abstract - value : Int - minus_infinity = Int .MinValue + compareToIdentifier (identifier0 : Identifier) (identifier1 : Identifier) : Int = + identifier0 .compareTo (identifier1) - plus_infinity = Int .MaxValue + compareToActor (actor0 : Actor) (actor1 : Actor) : Int = + compareToIdentifier (actor0) (actor1) - compareTo (other : Measure) : Int = - value .compareTo (other .value) + compareToResource (resource0 : Resource) (resource1 : Resource) : Int = + compareToIdentifier (resource0) (resource1) - @override - toString : String = - value .toString + compareToContext (context0 : Context) (context1 : Context) : Int = + compareToIdentifier (context0) (context1) + + _compareNoneTo (measure : Measure) : Int = + match measure + case Some (value) ==> -1 + case None ==> 0 + + _compareSomeTo (value : Int) (measure : Measure) : Int = + match measure + case Some (other_value) ==> value - other_value + case None ==> 1 + + compareToMeasure (measure0 : Measure) (measure1 : Measure) : Int = + match measure0 + case Some (value) ==> _compareSomeTo (value) (measure1) + case None ==> _compareNoneTo (measure1) end diff --git a/tiles/src/main/scala/soda/tiles/fairness/tool/Package.scala b/tiles/src/main/scala/soda/tiles/fairness/tool/Package.scala index 8141284..3acf6ac 100644 --- a/tiles/src/main/scala/soda/tiles/fairness/tool/Package.scala +++ b/tiles/src/main/scala/soda/tiles/fairness/tool/Package.scala @@ -16,31 +16,50 @@ type Resource = Identifier type Context = Identifier -trait Measure - extends - Comparable [Measure] +type Measure = Option [Int] + +trait Comparator { - def value : Int - lazy val minus_infinity = Int .MinValue - lazy val plus_infinity = Int .MaxValue + def compareToIdentifier (identifier0 : Identifier) (identifier1 : Identifier) : Int = + identifier0 .compareTo (identifier1) - def compareTo (other : Measure) : Int = - value .compareTo (other .value) + def compareToActor (actor0 : Actor) (actor1 : Actor) : Int = + compareToIdentifier (actor0) (actor1) - override - lazy val toString : String = - value .toString + def compareToResource (resource0 : Resource) (resource1 : Resource) : Int = + compareToIdentifier (resource0) (resource1) + + def compareToContext (context0 : Context) (context1 : Context) : Int = + compareToIdentifier (context0) (context1) + + private def _compareNoneTo (measure : Measure) : Int = + measure match { + case Some (value) => -1 + case None => 0 + } + + private def _compareSomeTo (value : Int) (measure : Measure) : Int = + measure match { + case Some (other_value) => value - other_value + case None => 1 + } + + def compareToMeasure (measure0 : Measure) (measure1 : Measure) : Int = + measure0 match { + case Some (value) => _compareSomeTo (value) (measure1) + case None => _compareNoneTo (measure1) + } } -case class Measure_ (value : Int) extends Measure +case class Comparator_ () extends Comparator -object Measure { - def mk (value : Int) : Measure = - Measure_ (value) +object Comparator { + def mk : Comparator = + Comparator_ () } trait Assignment diff --git a/tiles/src/test/scala/soda/tiles/fairness/tile/Package.scala b/tiles/src/test/scala/soda/tiles/fairness/tile/Package.scala index 95c239b..d0a8c12 100644 --- a/tiles/src/test/scala/soda/tiles/fairness/tile/Package.scala +++ b/tiles/src/test/scala/soda/tiles/fairness/tile/Package.scala @@ -10,7 +10,6 @@ import soda.tiles.fairness.tool.Assignment import soda.tiles.fairness.tool.Assignment_ import soda.tiles.fairness.tool.Context import soda.tiles.fairness.tool.Measure -import soda.tiles.fairness.tool.Measure_ import soda.tiles.fairness.tool.Outcome import soda.tiles.fairness.tool.Outcome_ import soda.tiles.fairness.tool.Resource @@ -126,8 +125,17 @@ trait ResourceAllocationScenarioExample private def _mk_Assignment (actor : Actor) (resource : Resource) : Assignment = Assignment_ (actor, resource) + private def _addValueTo (value : Int) (m : Measure) : Measure = + m match { + case Some (other_value) => Some (value + other_value) + case None => None + } + def measure_sum (a : Measure) (b : Measure) : Measure = - Measure_ (a .value + b .value) + a match { + case Some (value) => _addValueTo (value) (b) + case None => None + } lazy val resource0 = "small box - 0.1 m" @@ -142,22 +150,22 @@ trait ResourceAllocationScenarioExample lazy val actor2 = "Charlie C" lazy val actor_need_map : Map [Actor, Measure] = Seq ( - Tuple2 [Actor, Measure] (actor0 , Measure_ (30) ) , - Tuple2 [Actor, Measure] (actor1 , Measure_ (10) ) , - Tuple2 [Actor, Measure] (actor2 , Measure_ (0) ) + Tuple2 [Actor, Measure] (actor0 , Some (30) ) , + Tuple2 [Actor, Measure] (actor1 , Some (10) ) , + Tuple2 [Actor, Measure] (actor2 , Some (0) ) ) .toMap lazy val resource_height_map : Map [Resource, Measure] = Seq ( - Tuple2 [Resource, Measure] (resource0 , Measure_ (10) ) , - Tuple2 [Resource, Measure] (resource1 , Measure_ (20) ) , - Tuple2 [Resource, Measure] (resource2 , Measure_ (30) ) + Tuple2 [Resource, Measure] (resource0 , Some (10) ) , + Tuple2 [Resource, Measure] (resource1 , Some (20) ) , + Tuple2 [Resource, Measure] (resource2 , Some (30) ) ) .toMap def actor_need (actor : Actor) : Measure = - actor_need_map .getOrElse (actor , Measure_ (-1) ) + actor_need_map .getOrElse (actor , Some (-1) ) def resource_height (resource : Resource) : Measure = - resource_height_map .getOrElse (resource , Measure_ (-1) ) + resource_height_map .getOrElse (resource , Some (-1) ) lazy val context = "context" @@ -235,9 +243,9 @@ trait ScoringScenarioExample private lazy val _resource_one : Resource = "1" - private lazy val _measure_zero : Measure = Measure_ (0) + private lazy val _measure_zero : Measure = Some (0) - private lazy val _measure_one : Measure = Measure_ (1) + private lazy val _measure_one : Measure = Some (1) lazy val seed_protected_attribute : Long = 127 @@ -251,7 +259,7 @@ trait ScoringScenarioExample lazy val protected_attribute : Seq [Measure] = Random_ () .get_next_seq (seed_protected_attribute) (actors .length) - .map ( x => Measure_ ( as_protected_attribute (x .intValue) ) ) + .map ( x => Some ( as_protected_attribute (x .intValue) ) ) lazy val protected_attribute_map : Map [Actor, Measure] = actors @@ -296,7 +304,7 @@ trait ScoringScenarioExample lazy val prediction_bias_on_attribute : Int = 40 - lazy val maximum_acceptable_bias_percentage : Measure = Measure_ (30) + lazy val maximum_acceptable_bias_percentage : Measure = Some (30) lazy val result : Seq [Measure] = result_values .map ( x => make_binary_measure (x) ) @@ -415,7 +423,7 @@ case class UnbiasednessTileSpec () check ( obtained = get_coefficient (ex .initial_unbiased) .contents ) ( - expected = Measure_ (0) + expected = Some (0) ) ) @@ -423,7 +431,7 @@ case class UnbiasednessTileSpec () check ( obtained = get_coefficient (ex .initial_biased) .contents ) ( - expected = Measure_ (42) + expected = Some (42) ) ) diff --git a/tiles/src/test/scala/soda/tiles/fairness/tile/Package.soda b/tiles/src/test/scala/soda/tiles/fairness/tile/Package.soda index 40f73e4..dd1604d 100644 --- a/tiles/src/test/scala/soda/tiles/fairness/tile/Package.soda +++ b/tiles/src/test/scala/soda/tiles/fairness/tile/Package.soda @@ -11,7 +11,6 @@ import soda.tiles.fairness.tool.Assignment_ soda.tiles.fairness.tool.Context soda.tiles.fairness.tool.Measure - soda.tiles.fairness.tool.Measure_ soda.tiles.fairness.tool.Outcome soda.tiles.fairness.tool.Outcome_ soda.tiles.fairness.tool.Resource diff --git a/tiles/src/test/scala/soda/tiles/fairness/tile/ResourceAllocationScenarioExample.soda b/tiles/src/test/scala/soda/tiles/fairness/tile/ResourceAllocationScenarioExample.soda index f839b6b..f2d67d3 100644 --- a/tiles/src/test/scala/soda/tiles/fairness/tile/ResourceAllocationScenarioExample.soda +++ b/tiles/src/test/scala/soda/tiles/fairness/tile/ResourceAllocationScenarioExample.soda @@ -6,8 +6,15 @@ class ResourceAllocationScenarioExample _mk_Assignment (actor : Actor) (resource : Resource) : Assignment = Assignment_ (actor) (resource) + _addValueTo (value : Int) (m : Measure) : Measure = + match m + case Some (other_value) ==> Some (value + other_value) + case None ==> None + measure_sum (a : Measure) (b : Measure) : Measure = - Measure_ (a .value + b .value) + match a + case Some (value) ==> _addValueTo (value) (b) + case None ==> None resource0 = "small box - 0.1 m" @@ -22,22 +29,22 @@ class ResourceAllocationScenarioExample actor2 = "Charlie C" actor_need_map : Map [Actor] [Measure] = Seq ( - Tuple2 [Actor] [Measure] (actor0 , Measure_ (30) ) , - Tuple2 [Actor] [Measure] (actor1 , Measure_ (10) ) , - Tuple2 [Actor] [Measure] (actor2 , Measure_ (0) ) + Tuple2 [Actor] [Measure] (actor0 , Some (30) ) , + Tuple2 [Actor] [Measure] (actor1 , Some (10) ) , + Tuple2 [Actor] [Measure] (actor2 , Some (0) ) ) .toMap resource_height_map : Map [Resource] [Measure] = Seq ( - Tuple2 [Resource] [Measure] (resource0 , Measure_ (10) ) , - Tuple2 [Resource] [Measure] (resource1 , Measure_ (20) ) , - Tuple2 [Resource] [Measure] (resource2 , Measure_ (30) ) + Tuple2 [Resource] [Measure] (resource0 , Some (10) ) , + Tuple2 [Resource] [Measure] (resource1 , Some (20) ) , + Tuple2 [Resource] [Measure] (resource2 , Some (30) ) ) .toMap actor_need (actor : Actor) : Measure = - actor_need_map .getOrElse (actor , Measure_ (-1) ) + actor_need_map .getOrElse (actor , Some (-1) ) resource_height (resource : Resource) : Measure = - resource_height_map .getOrElse (resource , Measure_ (-1) ) + resource_height_map .getOrElse (resource , Some (-1) ) context = "context" diff --git a/tiles/src/test/scala/soda/tiles/fairness/tile/ScoringScenarioExample.soda b/tiles/src/test/scala/soda/tiles/fairness/tile/ScoringScenarioExample.soda index 8bda98a..1fcb055 100644 --- a/tiles/src/test/scala/soda/tiles/fairness/tile/ScoringScenarioExample.soda +++ b/tiles/src/test/scala/soda/tiles/fairness/tile/ScoringScenarioExample.soda @@ -14,9 +14,9 @@ class ScoringScenarioExample _resource_one : Resource = "1" - _measure_zero : Measure = Measure_ (0) + _measure_zero : Measure = Some (0) - _measure_one : Measure = Measure_ (1) + _measure_one : Measure = Some (1) seed_protected_attribute : Long = 127 @@ -30,7 +30,7 @@ class ScoringScenarioExample protected_attribute : Seq [Measure] = Random_ () .get_next_seq (seed_protected_attribute) (actors .length) - .map (lambda x --> Measure_ ( as_protected_attribute (x .intValue) ) ) + .map (lambda x --> Some ( as_protected_attribute (x .intValue) ) ) protected_attribute_map : Map [Actor] [Measure] = actors @@ -75,7 +75,7 @@ class ScoringScenarioExample prediction_bias_on_attribute : Int = 40 - maximum_acceptable_bias_percentage : Measure = Measure_ (30) + maximum_acceptable_bias_percentage : Measure = Some (30) result : Seq [Measure] = result_values .map (lambda x --> make_binary_measure (x) ) diff --git a/tiles/src/test/scala/soda/tiles/fairness/tile/UnbiasednessTileSpec.soda b/tiles/src/test/scala/soda/tiles/fairness/tile/UnbiasednessTileSpec.soda index a96e23c..7aca290 100644 --- a/tiles/src/test/scala/soda/tiles/fairness/tile/UnbiasednessTileSpec.soda +++ b/tiles/src/test/scala/soda/tiles/fairness/tile/UnbiasednessTileSpec.soda @@ -42,7 +42,7 @@ class UnbiasednessTileSpec () check ( obtained := get_coefficient (ex .initial_unbiased) .contents ) ( - expected := Measure_ (0) + expected := Some (0) ) ) @@ -50,7 +50,7 @@ class UnbiasednessTileSpec () check ( obtained := get_coefficient (ex .initial_biased) .contents ) ( - expected := Measure_ (42) + expected := Some (42) ) )