-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
196 lines (178 loc) · 4.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"math"
"runtime"
"sync"
"time"
)
var (
agentAmount int = 500
singleServiceAmount int = 50
slowmotion int = 0 // every step sleeps for this amount of milliseconds
border Border = Border{-1.5, -3.0, 4.0, 4.0, 0, 0, 0, 0}
agentList []*Agent = make([]*Agent, agentAmount, agentAmount)
bestAgent BestAgent = BestAgent{math.MaxFloat64, math.MaxFloat64, math.MaxFloat64, 0, 0, sync.Mutex{}}
newAgentChannel chan *Agent = make(chan *Agent, agentAmount)
fitnessChannel chan *Agent = make(chan *Agent, agentAmount)
getBestChannel chan *Agent = make(chan *Agent, agentAmount)
moveChannel chan *Agent = make(chan *Agent, agentAmount)
eventHorizonChannel chan *Agent = make(chan *Agent, agentAmount)
randomBuffer chan float64 = make(chan float64, agentAmount*5)
maxAccuracy chan bool = make(chan bool, 1)
endComputing chan bool = make(chan bool, singleServiceAmount+10)
exitProgram chan bool = make(chan bool, 1)
typeOfFunction TypeOfFunction_S = TypeOfFunction_S{Rastrigin: true}
//typeOfFunction TypeOfFunction_S = TypeOfFunction_S{Rosenbrock: true}
//typeOfFunction TypeOfFunction_S = TypeOfFunction_S{Easom: true}
//typeOfFunction TypeOfFunction_S = TypeOfFunction_S{McCormick: true}
//typeOfFunction TypeOfFunction_S = TypeOfFunction_S{StringEvaluation: "20 + pow(x, 2) + pow(y, 2) - 10 * (cos(2 * PI() * x) + cos(2 * PI() * y))"}
i int
)
func main() {
StartComputing()
Utils()
time.Sleep(100 * time.Second)
return
for {
select {
case <-exitProgram:
return
}
}
}
func StartComputing() {
for i := 0; i < singleServiceAmount; i++ {
go func() {
for {
select {
case <-endComputing:
return
default:
getBest(getBestChannel, <-fitnessChannel)
}
}
}()
go func() {
for {
select {
case <-endComputing:
return
default:
move(moveChannel, <-getBestChannel)
}
}
}()
go func() {
for {
select {
case <-endComputing:
return
default:
eventHorizon(eventHorizonChannel, <-moveChannel)
}
}
}()
go func() {
for {
select {
case <-endComputing:
return
default:
countFitness(fitnessChannel, <-eventHorizonChannel)
}
}
}()
}
}
func Utils() {
/// reports
go func() {
for {
select {
case <-endComputing:
return
default:
fmt.Println(bestAgent)
fmt.Println(runtime.NumGoroutine())
averageStepAmount := averageStepAmount()
fmt.Println("averageStepAmount: ", averageStepAmount)
time.Sleep(500 * time.Millisecond)
}
}
}()
/// check if got the best answer
go func() {
<-maxAccuracy
fmt.Println("It can't be bether:")
for i := 0; i < 4*singleServiceAmount+10; i++ {
endComputing <- true
}
fmt.Println(bestAgent)
averageStepAmount := averageStepAmount()
fmt.Println("averageStepAmount: ", averageStepAmount)
exitProgram <- true
return
}()
}
func averageStepAmount() uint64 {
var averageStepAmount uint64
for i := 0; i < agentAmount; i++ {
averageStepAmount += agentList[i].times
}
averageStepAmount /= uint64(agentAmount)
return averageStepAmount
}
func init() {
go func() {
for {
randomBuffer <- NextDouble()
}
}()
border.SetUp()
/// create agents
for i := 0; i < agentAmount; i++ {
agent := Agent{
x: <-randomBuffer*border.HorizontalLength - border.HorizontalLength/2 + border.HorizontalCenter,
y: <-randomBuffer*border.VerticalLength - border.VerticalLength/2 + border.VerticalCenter,
fitness: math.MaxFloat64,
border: border,
typeOfFunction: typeOfFunction}
agentList[i] = &agent
}
/// send agents to channel
for i := 0; i < agentAmount; i++ {
newAgentChannel <- agentList[i]
}
/// set pre-values for agents
for i := i; i < agentAmount; i++ {
countFitness(fitnessChannel, <-newAgentChannel)
}
/// this goroutine continuously evaluates Best Agent event horizon
//go func() {
// for {
// countEventHorizon()
// }
//}()
bestAgent.eventHorizon = math.SmallestNonzeroFloat64
}
func Connect() {
// waiting for connection:
c := make(chan string)
go func() {
for i := 0; ; i++ {
c <- fmt.Sprint("asd ", i)
}
}()
for {
select {
case s := <-c:
fmt.Println(s)
case <-time.After(time.Second * 10):
fmt.Println("Cannot estabilish a connection")
return
default:
fmt.Println("nothing")
}
}
}