-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBPSO.nls
More file actions
163 lines (142 loc) · 7.13 KB
/
SBPSO.nls
File metadata and controls
163 lines (142 loc) · 7.13 KB
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
;# This is based on fsancho PSO.nls im using that as a template or almost
;# The thing is, SBPSO works in a different way compared to PSO. One thing is that graphically we can't say that elements are near or far away from each other, because we have
;# just a set of numbers with no positions and no order. Now, in terms of velocity and position, our position is a set of numbers contained in our universe set (U) and this
;# position has a value returned by a fitness function and with this value we can say what position is better. A velocity is another set of numbers with variable size but
;# it contains also an operator which indicates an addition or subtraction of that element in our current set of position, so if we have something like this:
;# Position: X = {a, c} (considering size of 2 elements it can be more..) and Velocity: V = {(+,b), (−, c)} (considering size of 2 pairs of elements it can be more..) but Fernando
;# suggested to have a list of 2 sublists considering the first sublist elements to add to the position and the second sublist elements to remove from the position.
;# then we will get a returned position of: ResultPosition: X'= {a,b}.
;# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
globals [
global-best-pos ;gonna be a set which stores the global best set (position)
global-best-value ;gonna be a set which stores the value of that position (fitness function return)
U ;gonna be a set, basically our search space
]
breed [particles particle]
particles-own [
personal-best-val ; mejor valor que he encontrado
personal-best-pos ; the attributes which make this the best
posi
velocity
]
;---------------------------------Update velocity and position---------------------------------
to updateVelocity [turt]
ask particle turt [
let unionAuxAi union (union posi personal-best-pos) global-best-pos
let Ai filter [x -> not member? x unionAuxAi] (sort U)
let auxSi (list posi personal-best-pos global-best-pos);Just putting up X, P.best and G.best in a list of 3 to reduce after
let Si reduce intersect auxSi ;Intersection of X P.best and G.best
let k count elements with [member? (element who) Ai]
set velocity (union (prodVel (#atraction-best-personal * (random-float 1.0)) (difference personal-best-pos posi))
(prodVel (#atraction-best-global * (random-float 1.0)) (difference global-best-pos posi)))
set velocity union velocity (k-tournamentSelection k posi)
let auxVel ((random-float 1.0) * (length Si))
set velocity union velocity (list [] (n-of auxVel Si))
let temp sentence (last velocity) (n-of auxVel Si)
set velocity (list first velocity temp)
]
end
to-report applyVel [posit vel] ;apply a velocity to a position (for position update)
let retPos posit
set retPos filter [x -> not member? x (last vel)] posit ;"deleting" items on second list of the velocity from the given position
set retPos se retPos (first vel) ;adding the first list of the velocity to the given position
set retPos remove-duplicates retPos ;to be sure that we doesn't have duplicates
report retPos
end
to run-SBPSO
ask particles [ ;checking if we have found a new best personal or global and update it if its the case
let posPoints evalPosition posi
if posPoints > personal-best-val [
set personal-best-pos posi
set personal-best-val posPoints
]
if posPoints > global-best-value [
set global-best-pos posi
set global-best-value posPoints
set-global-solution who
set weight-knapsack calculateWeight global-best-pos
print word "\nEncontrado nuevo mejor global!!: " global-best-pos
]
;Update the velocity of the particle
updateVelocity who
;Update the position of the particle
set posi applyVel posi velocity
]
tick
end
;---------------------------------Update velocity and position---------------------------------
;---------------------------------OPERATORS---------------------------------
to-report union [set1 set2] ;makes the union of two sets
let conj1 se (first set1) (first set2)
let conj2 se (last set1) (last set2)
let ret (list conj1 conj2)
report remove-duplicates ret
end
to-report difference [set1 set2]
let differenceSet1 filter [x -> not member? x set2] set1 ;elements in set1 but not in set2
let differenceSet2 filter [x -> not member? x set1] set2 ;elements in set2 but not in set1
report (list differenceSet1 differenceSet2) ;first set with additions and second one with deletions
end
to-report prodVel [eta V] ;picks a random subset from V with probability of eta
let n1 floor (eta * length first V)
let n2 floor (eta * length last V)
let aux1 n-of n1 (first V)
let aux2 n-of n2 (last V)
let ret (list aux1 aux2)
; let n floor (eta * length V) ;if we consider a list of pairs {(+,3) (+,4) (-,2) (+,6)..}
; print (ret)
; report n-of n V
report ret
end
to-report intersect [a b] ;auxiliar function to easily intersect 3 lists
report (filter [x -> member? x b] a)
end
to-report k-tournamentSelection [k sol]
let tempList sol ;make a temporary list which starts out to be the local solution of the turtle
let bestSol sol ;best solution is the same but will be modified turing the algoritm
let solPoints 0
let bestSolPoints 0
let velo [] ;initialize velocity set
let unionAuxAi union (union posi personal-best-pos) global-best-pos
let Ai filter [x -> not member? x unionAuxAi] (sort U)
if length Ai > 0 [
let elemToAdd one-of Ai
repeat k [
ask one-of Ai [ ;randomly choose one element which is not already in the solution
set tempList lput (element who) bestSol ; add this element to the temporary list in order to compare if this solution is better
set solPoints evalPosition tempList ; evaluate the solution
if solPoints > bestSolPoints [ ;update bestSolution
set bestSolPoints solPoints
set elemToAdd (element who) ;is the element which will be added as velocity
]
]
]
set velo lput elemToAdd velo ]
report list velo []
end
;---------------------------------OPERATORS---------------------------------
;---------------------------------Other functions---------------------------------
to set-random-solution ;making a random sol to initialize
ask particles [
set personal-best-pos []
repeat ( random (poblacion mod 7) + 1 ) [ ;put random elements in personal best to have a initialization
set personal-best-pos lput (one-of elements) personal-best-pos ;adding that elements to personal best list
set personal-best-val evalPosition personal-best-pos ;set the value of that list using the evaluation function
]
]
end
to set-global-solution[partic] ;update the global best set of elements
foreach global-best-pos [ ele -> ;making the old set of elements back blue graphically
ask ele [
set color blue
]
]
ask particle partic [
foreach global-best-pos [ ele -> ;making the old set of elements back blue graphically
ask ele [
set color red
]
]
]
end
;---------------------------------Other functions---------------------------------