Skip to content

Commit

Permalink
Simplify Measure type
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmendez committed Feb 18, 2024
1 parent 074b5e8 commit 1e7d9ef
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class CorrelationTile

abstract

_measure_zero = Measure_ (0)
_measure_zero : Measure = Some (0)

_percentage_constant : Double = 100.0

Expand All @@ -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) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 13 additions & 9 deletions tiles/src/main/scala/soda/tiles/fairness/tile/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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)
)
)
Expand Down Expand Up @@ -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

Expand All @@ -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) )
Expand Down Expand Up @@ -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) (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tiles/src/main/scala/soda/tiles/fairness/tile/Package.soda
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 27 additions & 11 deletions tiles/src/main/scala/soda/tiles/fairness/tool/Entity.soda
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 34 additions & 15 deletions tiles/src/main/scala/soda/tiles/fairness/tool/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 24 additions & 16 deletions tiles/src/test/scala/soda/tiles/fairness/tile/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -415,15 +423,15 @@ case class UnbiasednessTileSpec ()
check (
obtained = get_coefficient (ex .initial_unbiased) .contents
) (
expected = Measure_ (0)
expected = Some (0)
)
)

test ("coefficient of biased sample") (
check (
obtained = get_coefficient (ex .initial_biased) .contents
) (
expected = Measure_ (42)
expected = Some (42)
)
)

Expand Down
1 change: 0 additions & 1 deletion tiles/src/test/scala/soda/tiles/fairness/tile/Package.soda
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 1e7d9ef

Please sign in to comment.