Compare commits

..

7 Commits

Author SHA1 Message Date
685bdef816 fix panic in the runtime 2022-11-19 20:10:59 +01:00
eb57a18a02 set initial params in hardcode 2022-11-16 18:57:18 +01:00
206796cf0f fix logic 2022-11-16 18:02:35 +01:00
d1bb82d2a1 write to file 2022-11-16 17:09:16 +01:00
659b913130 write to file 2022-11-15 09:05:27 +01:00
1a11deb7bc the ugliest code ever written 2022-11-14 22:34:25 +01:00
17d2680dd0 try addign drone 2022-11-14 21:27:44 +01:00
3 changed files with 93 additions and 23 deletions

10
.drone.yml Normal file
View File

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

View File

@ -1,19 +1,24 @@
- step: 10 - step: 10
amount: 100 amount: 100
neighbours: 10 neighbours: 1
- step: 100 - step: 100
amount: 100 amount: 100
neighbours: 50 neighbours: 50
- step: 200 - step: 200
amount: 150 amount: 500
neighbours: 20 neighbours: 20
- step: 300
amount: 40
neighbours: 10
- step: 400 - step: 400
amount: 10 amount: 10
neighbours: 5 neighbours: 5
- step: 500 - step: 500
amount: 1 amount: 1
neighbours: 1 neighbours: 1
- step: 600
amount: 500
neighbours: 50
- step: 1500
amount: 10
neighbours: 1
- step: 2000
amount: 1
neighbours: 1

89
main.go
View File

@ -12,6 +12,7 @@ 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"
@ -21,32 +22,42 @@ 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 = 20 var populationSize = 0
var loops = 500 var loops = 3000
var numNeighbours = 10 var numNeighbours = 5
var separationFactor = float64(goidSize * 5) var separationFactor = float64(goidSize * 5)
var coherenceFactor = 8 var coherenceFactor = 8
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.txt") fo, err := os.Create("/tmp/goids.yaml")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -65,24 +76,68 @@ func main() {
hideCursor() hideCursor()
var goids []*Goid var goids []*Goid
for i := 0; i < populationSize; i++ { // for i := 0; i < populationSize; i++ {
g := createRandomGoid() // g := createRandomGoid()
goids = append(goids, &g) // 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, td[current_stage].Amount) goids, populationSize = fixGoids(goids, populationSize, td[current_stage].Amount)
numNeighbours = td[current_stage].NumNeighbours if td[current_stage].NumNeighbours >= populationSize {
if i > td[current_stage].Step { numNeighbours = populationSize
} else {
numNeighbours = td[current_stage].NumNeighbours
}
if i == td[current_stage+1].Step {
current_stage += 1 current_stage += 1
} }
move(goids, fo) move(goids, fo, i)
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
@ -128,13 +183,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) { func move(goids []*Goid, file *os.File, loop int) {
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("- coor: %d\n data:\n x: %d\n y: %d\n", i, goid.X, goid.Y) position := fmt.Sprintf("- goid: %d\n loop: %d\n data:\n x: %d\n y: %d\n", i, loop, goid.X, goid.Y)
file.Write([]byte(position)) file.Write([]byte(position))
stayInWindow(goid) stayInWindow(goid)
} }