-
Notifications
You must be signed in to change notification settings - Fork 0
/
PsoFlinkIt.scala
183 lines (149 loc) · 5.27 KB
/
PsoFlinkIt.scala
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
package org.apache.flink.quickstart
import org.apache.flink.api.scala._
import scala.collection.mutable
/**
* Created by hajira on 10/15/15.
*/
// very basic version mainly due to moving code around to see the updates work....
object PsoFlinkIt {
val dim: Int = 4
var gbest: PSOParticle = new PSOParticle()
var tbest: PSOParticle = new PSOParticle()
class PSOParticle() {
//FlatMapFunction[(PSOParticle), (PSOParticle) ]{
var dimension: Int = dim
var pfit: Double = 100000.0
var fit: Double = 10000000.0
var position: Array[Double] = (1 to dim toArray) map (x => Math.random())
var velocity: Array[Double] = (1 to dim toArray) map (x => Math.random())
var pbest: Array[Double] = (1 to dim toArray) map (x => Math.random())
def Copy(particle: PSOParticle): Unit = {
0 until particle.dimension foreach { a =>
this.velocity(a) = particle.velocity(a)
this.position(a) = particle.position(a)
}
this.fit = particle.fit
}
def stringString(): String = {
var st = new StringBuilder()
0 until this.dimension foreach { a =>
st.append("\n Vel")
st.append(this.velocity(a).toString)
st.append("\n Pos")
st.append(this.position(a).toString)
}
st.append("\n \n--Fitness =")
st.append(this.fit.toString)
println("updating gbbes" + st)
var temp = new String()
temp.addString(st)
return temp
}
// override def flatMap(particle: (PSOParticle), out: Collector[PSOParticle]): Unit = {
// var sum = 0.0
//
// 0 until particle.dimension foreach { j =>
//
// sum = sum + particle.position(j) * particle.position(j)
// }
// particle.fit = sum
// if (particle.pfit > sum) {
// particle.pfit = sum
// particle.position.copyToArray(particle.pbest)
// ChkGbest(particle)
// }
// out.collect(particle)
// }
}
//override def Map ()
def sphere(particle: PSOParticle): PSOParticle = {
var sum = 0.0
0 until particle.dimension foreach { j =>
sum = sum + particle.position(j) * particle.position(j)
}
particle.fit = sum
if (particle.pfit > sum) {
particle.pfit = sum
particle.position.copyToArray(particle.pbest)
ChkGbest(particle)
}
particle
}
def ChkGbest(particle: PSOParticle): Unit = {
if (particle.fit < gbest.fit) {
gbest.Copy(particle)
println("updating gbbest................\n" + tbest.fit.toString + gbest.fit.toString)
}
}
// works !
// private def getPop(env: ExecutionEnvironment, size: Int): DataSet[PSOParticle] = {
// val pop = ((1 to size) map (x => new PSOParticle()))
// env.fromCollection(pop)
// }
def updateP(particle: PSOParticle): PSOParticle = {
val c1 = 1.49618
val c2 = 1.49618
val W = 0.7298
0 until particle.dimension foreach { a =>
particle.velocity(a) = W * particle.velocity(a) + (Math.random() * c1 * (gbest.position(a) - particle.position(a))) + (Math.random() * c2 * (particle.pbest(a) - particle.position(a)))
particle.position(a) = particle.velocity(a) + particle.position(a)
}
return particle
}
def main(args: Array[String]): Unit = {
// execution parameters
val iter = 3
val populationsize = 5
val size = populationsize
// environment settings
val env = ExecutionEnvironment.getExecutionEnvironment
// val population = env.fromCollection( Array[PSOParticle]= ((1 to size toArray) map (x => new PSOParticle(dim))))//:
//val population : DataSet[PSOParticle] = getPop(env, size)
//val pop = mutable.Iterable((1 to size) map(x => new PSOParticle()))
//var it = asScalaIteratorConverter(pop)
//val pop = ((1 to size) map (x => new PSOParticle())) // something wrong here????? works
var p1 = new PSOParticle()
var p2 = new PSOParticle()
var p3 = new PSOParticle()
val population = env.fromElements(p1, p2, p3)
//val population = env.fromParallelCollection(pop)//works
println(population.getType())
// to DO
var gbest: PSOParticle = new PSOParticle()
println(gbest.stringString())
var tbest: PSOParticle = new PSOParticle()
// Iterator try !!!!!! with inline and
// Create initial DataSet
val initial = env.fromElements(p1, p2, p3)
val count = initial.iterate(20) { iterationInput: DataSet[PSOParticle] =>
var initial2 = iterationInput.map { x => {
var sum = 0.0
0 until x.dimension foreach { j =>
sum = sum + x.position(j) * x.position(j)
}
x.fit = sum
}
x
} // not being updated
initial2
}
// Basic version with inline function
0 until iter foreach { j =>
population.map(x => {
var sum = 0.0
0 until x.dimension foreach { j =>
sum = sum + x.position(j) * x.position(j)
}
x.fit = sum
})
population.map(x => updateP(_)) // NOT BEING UPDATED
population.collect()
population.print()
// population.groupBy(pop[1].fit).sum("fit").print()
println("........................................................." + gbest.stringString())
}
//population.map(x=>x.stringString())
//population.writeAsCsv("test.txt","/n", " ")
env.execute()
}
}