Skip to content

Commit

Permalink
convert utility factor to policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jun 28, 2024
1 parent 7aea30e commit 50833a1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
68 changes: 68 additions & 0 deletions ve/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ve

import (
"fmt"
"math"
"slices"
)

Expand Down Expand Up @@ -141,6 +142,73 @@ func (v *Variables) SumOut(f *Factor, variable Variable) Factor {
return fNew
}

func (v *Variables) Policy(f *Factor, variable Variable) Factor {
newVars := []Variable{}
idx := -1

for i := range f.variables {
if f.variables[i].id == variable.id {
idx = i
} else {
newVars = append(newVars, f.variables[i])
}
}

if idx < 0 {
panic(fmt.Sprintf("variable %d not in this factor", variable.id))
}
newVars = append(newVars, f.variables[idx])

fNew := v.CreateFactor(newVars, nil)

oldIndex := make([]int, len(f.variables))
newIndex := make([]int, len(newVars))
idxNew := len(newVars) - 1

cols := int(f.variables[idx].outcomes)
rows := len(f.data) / cols

rowData := make([]float64, cols)
for row := 0; row < rows; row++ {
fNew.Outcomes(row*cols, newIndex)
for c := 0; c < cols; c++ {
newIndex[idxNew] = c

for j := 0; j < idx; j++ {
oldIndex[j] = newIndex[j]
}
for j := idx + 1; j < len(oldIndex); j++ {
oldIndex[j] = newIndex[j-1]
}
oldIndex[idx] = newIndex[idxNew]
rowData[c] = f.Get(oldIndex)
}
maxUtility := math.Inf(-1)
maxIdx := -1
for c, u := range rowData {
if u > maxUtility {
maxUtility = u
maxIdx = c
}
}

if maxIdx < 0 {
panic("no utility values to derive policy")
}

for c := 0; c < cols; c++ {
newIndex[idxNew] = c
if c == maxIdx {
fNew.Set(newIndex, 1)
} else {
fNew.Set(newIndex, 0)
}
}
}

return fNew
}

func (v *Variables) Product(factors ...*Factor) Factor {
if len(factors) == 1 {
return *factors[0]
Expand Down
28 changes: 28 additions & 0 deletions ve/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,31 @@ func TestVariablesProductScalar(t *testing.T) {

assert.Equal(t, []float64{2, 18, 10, 10, 16, 4}, f3.data)
}

func TestVariablesPolicy(t *testing.T) {
v := NewVariables()

v1 := v.Add(ChanceNode, 3)
v2 := v.Add(ChanceNode, 2)

f1 := v.CreateFactor([]Variable{v1, v2}, []float64{
0.4, 0.6,
0.9, 0.1,
0.2, 0.8,
})

p := v.Policy(&f1, v2)
assert.Equal(t, variables{v1, v2}, p.variables)
assert.Equal(t, []float64{
0, 1,
1, 0,
0, 1,
}, p.data)

p = v.Policy(&f1, v1)
assert.Equal(t, variables{v2, v1}, p.variables)
assert.Equal(t, []float64{
0, 1, 0,
0, 0, 1,
}, p.data)
}

0 comments on commit 50833a1

Please sign in to comment.