forked from sarah/microworlds
Much more accurate Snowflake Model
This commit is contained in:
parent
47eed67c89
commit
075c0f88d6
|
@ -15,21 +15,26 @@ var initialSureness = flag.Int("initialSureness", 2, "how sure an honest node is
|
|||
var byzantineTurtles = flag.Int("byzantineTurtles", 50, "in consensus simlulations, the number of turtles who will always vote 2")
|
||||
var cleverByzantineTurtles = flag.Bool("cleverByzantineTurtles", true, "byzantine turtles try to find each other")
|
||||
|
||||
var alpha = flag.Float64("alpha", 0.5, "the proportion of votes in a voting round that a color must have before it is considered a vote for that colour")
|
||||
|
||||
|
||||
type Snowball struct {
|
||||
color int
|
||||
Probability float64
|
||||
sureness float32
|
||||
Byztantine bool
|
||||
colourCounts []int
|
||||
}
|
||||
|
||||
func (sm *Snowball) Setup(env *core.Environment, t *core.Turtle) {
|
||||
num := rand.Intn(100)
|
||||
if num >= int(sm.Probability*100.0) {
|
||||
sm.color = 2
|
||||
} else {
|
||||
sm.color = 1
|
||||
} else {
|
||||
sm.color = 0
|
||||
}
|
||||
sm.sureness = float32(*initialSureness)
|
||||
sm.colourCounts = make([]int,2)
|
||||
}
|
||||
|
||||
func (sm *Snowball) GetColor() int {
|
||||
|
@ -44,7 +49,7 @@ func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
|
|||
t.FollowGradient(env, 5, 2, "3")
|
||||
t.Drop(env, 1, "3")
|
||||
}
|
||||
sm.color = 2
|
||||
sm.color = 1
|
||||
t.SetColor(color.RGBA{255, 0, 0, 0})
|
||||
t.Drop(env, 1, "2")
|
||||
|
||||
|
@ -57,31 +62,24 @@ func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
|
|||
t.Wiggle()
|
||||
am1 := t.AmountAll(env, 1, "1")
|
||||
am2 := t.AmountAll(env, 1, "2")
|
||||
k := float32(am1 + am2)
|
||||
|
||||
if am1 > sm.sureness || am2 > sm.sureness {
|
||||
if am1 > am2 {
|
||||
if sm.color == 2 {
|
||||
sm.sureness--
|
||||
} else {
|
||||
sm.sureness++
|
||||
}
|
||||
if sm.sureness == 0 {
|
||||
sm.color = 1
|
||||
}
|
||||
} else if am2 > am1 {
|
||||
if sm.color == 1 {
|
||||
sm.sureness--
|
||||
} else {
|
||||
sm.sureness++
|
||||
}
|
||||
if sm.sureness == 0 {
|
||||
sm.color = 2
|
||||
}
|
||||
}
|
||||
if am1 > k*float32(*alpha) && am1 > am2 {
|
||||
sm.colourCounts[0]++
|
||||
} else if am2 > k*0.5 && am2 > am1 {
|
||||
sm.colourCounts[1]++
|
||||
}
|
||||
|
||||
sm.sureness++
|
||||
|
||||
if sm.colourCounts[sm.color] < sm.colourCounts[(sm.color+1)%2] {
|
||||
sm.color = (sm.color+1) %2
|
||||
sm.sureness = 0
|
||||
}
|
||||
|
||||
// Add a vote for our new colour if we are sure
|
||||
if sm.sureness > 1 {
|
||||
t.Drop(env, 1, strconv.Itoa(sm.color))
|
||||
t.Drop(env, 1, strconv.Itoa(sm.color+1))
|
||||
}
|
||||
|
||||
t.Step(env)
|
||||
|
@ -106,7 +104,7 @@ func main() {
|
|||
experiment.InitPheromone("1", color.RGBA{0x00, 0xFF, 0x00, 0x00})
|
||||
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
|
||||
experiment.InitPheromone("3", color.RGBA{0xFF, 0x00, 0x00, 0x00})
|
||||
|
||||
fmt.Printf("Step, Votes for 1, Votes for 2\n")
|
||||
experiment.OnStep = func(env *core.Environment, turtles []*core.Turtle, step int) {
|
||||
num1 := 0
|
||||
num2 := 0
|
||||
|
@ -117,7 +115,7 @@ func main() {
|
|||
|
||||
for _, turtle := range turtles {
|
||||
agent := turtle.GetActor().(*Snowball)
|
||||
if agent.GetColor() == 1 {
|
||||
if agent.GetColor() == 0 {
|
||||
num1++
|
||||
} else {
|
||||
num2++
|
||||
|
|
Loading…
Reference in New Issue