Compare commits

..

No commits in common. "v0.0.8-dev" and "main" have entirely different histories.

3 changed files with 41 additions and 123 deletions

View File

@ -1,10 +0,0 @@
---
kind: pipeline
type: kubernetes
name: proto pipeline
steps:
- name: Run goids
image: golang:alpine3.16
commands:
- go run main.go

View File

@ -1,30 +1,19 @@
last_step: 100 - step: 10
init_amount: 10 amount: 100
steps: neighbours: 10
- step: 10 - step: 100
amount: 1 amount: 100
neighbours: 1 neighbours: 50
- step: 20 - step: 200
amount: 100 amount: 150
neighbours: 1 neighbours: 20
- step: 30 - step: 300
amount: 1 amount: 40
neighbours: 1 neighbours: 10
- step: 40 - step: 400
amount: 10 amount: 10
neighbours: 2 neighbours: 5
- step: 50 - step: 500
Amount: 2 amount: 1
neighbours: 1 neighbours: 1
- step: 60
amount: 2
neighbours: 1
- step: 70
amount: 3
neighbours: 2
- step: 80
amount: 3
neighbours: 1
- step: 3000
amount: 3
neighbours: 1

105
main.go
View File

@ -12,7 +12,6 @@ import (
"math/rand" "math/rand"
"os" "os"
"sort" "sort"
"strconv"
"github.com/llgcode/draw2d/draw2dimg" "github.com/llgcode/draw2d/draw2dimg"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -22,47 +21,32 @@ import (
var windowWidth, windowHeight = 1000, 700 var windowWidth, windowHeight = 1000, 700
var goidSize = 3 var goidSize = 3
var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency var goidColor = color.RGBA{200, 200, 100, 255} // gray, 50% transparency
var populationSize = 0 var populationSize = 20
var loops = 3000 var loops = 500
var numNeighbours = 5 var numNeighbours = 10
var separationFactor = float64(goidSize * 5) var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8 var coherenceFactor = 8
type configuration struct {
Steps []steps `yaml:"steps"`
LastStep int `yaml:"last_step"`
InitAmount int `yaml:"init_amount"`
}
type steps struct { type steps struct {
Step int `yaml:"step"` Step int `yaml:"step"`
Amount int `yaml:"amount"` Amount int `yaml:"amount"`
NumNeighbours int `yaml:"neighbours"` NumNeighbours int `yaml:"neighbours"`
} }
type Coor struct {
X int `yaml:"x"`
Y int `yaml:"y"`
}
type OutPut struct {
Goid int `yaml:"goid"`
Loop int `yaml:"loop"`
Data Coor `yaml:"data"`
}
func fixGoids(gs []*Goid, actual int, wished int) ([]*Goid, int) { func fixGoids(gs []*Goid, actual int, wished int) ([]*Goid, int) {
if actual < wished { if actual < wished {
g := createRandomGoid() g := createRandomGoid()
gs = append(gs, &g) gs = append(gs, &g)
return gs, actual + 1 return gs, actual + 1
} else if wished < actual { } else if wished < actual {
return gs[:len(gs)-1], actual - 1 return gs[:len(gs)-1], actual -1
} else { } else {
return gs, actual return gs, actual
} }
} }
func main() { func main() {
fo, err := os.Create("/tmp/goids.yaml") fo, err := os.Create("/tmp/goids.txt")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -70,11 +54,8 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
config := configuration{} td := []steps{}
err = yaml.Unmarshal(conf, &config) err = yaml.Unmarshal(conf, &td)
steps := config.Steps
loops = config.LastStep
populationSize = config.InitAmount
defer func() { defer func() {
if err := fo.Close(); err != nil { if err := fo.Close(); err != nil {
panic(err) panic(err)
@ -83,67 +64,25 @@ func main() {
clearScreen() clearScreen()
hideCursor() hideCursor()
var goids []*Goid var goids []*Goid
for i := 0; i < populationSize; i++ {
g := createRandomGoid() for i := 0; i < populationSize; i++ {
goids = append(goids, &g) g := createRandomGoid()
} goids = append(goids, &g)
}
current_stage := 0 current_stage := 0
for i := 0; i < loops; i++ { for i := 0; i < loops; i++ {
goids, populationSize = fixGoids(goids, populationSize, steps[current_stage].Amount) goids, populationSize = fixGoids(goids, populationSize, td[current_stage].Amount)
if steps[current_stage].NumNeighbours >= populationSize { numNeighbours = td[current_stage].NumNeighbours
numNeighbours = populationSize if i > td[current_stage].Step {
} else {
numNeighbours = steps[current_stage].NumNeighbours
}
if i == steps[current_stage+1].Step {
current_stage += 1 current_stage += 1
} }
move(goids, fo, i) move(goids, fo)
frame := draw(goids) frame := draw(goids)
printImage(frame.SubImage(frame.Rect)) printImage(frame.SubImage(frame.Rect))
} }
showCursor() showCursor()
out, err := ioutil.ReadFile("/tmp/goids.yaml")
if err != nil {
panic(err)
}
output := []OutPut{}
err = yaml.Unmarshal(out, &output)
var total int = 0
for _, goid := range output {
if total < goid.Goid {
total = goid.Goid
}
}
outfile, err := os.Create("./goids.txt")
if err != nil {
panic(err)
}
for i := 0; i <= total; i++ {
var x []string
var y []string
outfile.Write([]byte(fmt.Sprintf("GOID: %d\n", i)))
for z := 0; z <= loops; z++ {
check := false
for _, g := range output {
if g.Goid == i && g.Loop == z {
check = true
x = append(x, strconv.Itoa(g.Data.X))
y = append(y, strconv.Itoa(g.Data.Y))
break
}
}
if !check {
x = append(x, "-")
y = append(y, "-")
}
}
outfile.Write([]byte(fmt.Sprintf("%v\n", x)))
outfile.Write([]byte(fmt.Sprintf("%v\n", y)))
}
} }
// Goid represents a drawn goid // Goid represents a drawn goid
@ -189,13 +128,13 @@ func (g *Goid) distance(n Goid) float64 {
} }
// move the goids with the 3 classic boid rules // move the goids with the 3 classic boid rules
func move(goids []*Goid, file *os.File, loop int) { func move(goids []*Goid, file *os.File) {
for i, goid := range goids { for i, goid := range goids {
neighbours := goid.nearestNeighbours(goids) neighbours := goid.nearestNeighbours(goids)
separate(goid, neighbours) separate(goid, neighbours)
align(goid, neighbours) align(goid, neighbours)
cohere(goid, neighbours) cohere(goid, neighbours)
position := fmt.Sprintf("- goid: %d\n loop: %d\n data:\n x: %d\n y: %d\n", i, loop, goid.X, goid.Y) position := fmt.Sprintf("- coor: %d\n data:\n x: %d\n y: %d\n", i, goid.X, goid.Y)
file.Write([]byte(position)) file.Write([]byte(position))
stayInWindow(goid) stayInWindow(goid)
} }