You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
@@ -41,8 +44,7 @@ In CSO, there is an absence of a direct random velocity component, which is an i
41
44
Chicks follow their mother hens. They update their positions based on their mother's positions with some random factor to simulate the dependent behavior.
42
45
43
46
##Requirements
44
-
45
-
This project requires numpy and matplotlib.
47
+
This project requires numpy, pandas, and matplotlib for the full demos. To run the optimizer without visualization, only numpy and pandas are requirements
46
48
47
49
Use 'pip install -r requirements.txt' to install the following dependencies:
48
50
@@ -55,15 +57,139 @@ kiwisolver==1.4.5
55
57
matplotlib==3.8.4
56
58
numpy==1.26.4
57
59
packaging==24.0
60
+
pandas==2.2.3
58
61
pillow==10.3.0
59
62
pyparsing==3.1.2
60
63
python-dateutil==2.9.0.post0
64
+
pytz==2025.1
61
65
six==1.16.0
66
+
tzdata==2025.1
62
67
zipp==3.18.1
63
68
64
69
```
65
70
71
+
Optionally, requirements can be installed manually with:
72
+
73
+
```python
74
+
pip install matplotlib, numpy, pandas
75
+
76
+
```
77
+
This is an example for if you've had a difficult time with the requirements.txt file. Sometimes libraries are packaged together.
78
+
66
79
##Implementation
80
+
81
+
###Initialization
82
+
83
+
```python
84
+
# swarm variables
85
+
TOL=10**-6# Convergence Tolerance
86
+
MAXIT=10000# Maximum allowed iterations
87
+
BOUNDARY=1# int boundary 1 = random, 2 = reflecting
This optimizer uses a state machine structure to control the movement of the particles, call to the objective function, and the evaluation of current positions. The state machine implementation preserves the initial algorithm while making it possible to integrate other programs, classes, or functions as the objective function.
154
+
155
+
A controller with a`while loop` to check the completion status of the optimizer drives the process. Completion status is determined by at least 1) a set MAX number of iterations, and 2) the convergence to a given target using the L2 norm. Iterations are counted by calls to the objective function.
156
+
157
+
Within this`while loop` are three function calls to control the optimizer class:
158
+
***complete**: the`complete function` checks the status of the optimizer and if it has met the convergence or stop conditions.
159
+
***step**: the`step function` takes a boolean variable (suppress_output) as an input to control detailed printout on current particle (or agent) status. This function moves the optimizer one step forward.
160
+
***call_objective**: the`call_objective function` takes a boolean variable (allow_update) to control if the objective function is able to be called. In most implementations, this value will always be true. However, there may be cases where the controller or a program running the state machine needs to assert control over this function without stopping the loop.
161
+
162
+
Additionally,**get_convergence_data** can be used to preview the current status of the optimizer, including the current best evaluation and the iterations.
163
+
164
+
The code below is an example of this process:
165
+
166
+
```python
167
+
whilenot myOptimizer.complete():
168
+
# step through optimizer processing
169
+
# this will update particle or agent locations
170
+
myOptimizer.step(suppress_output)
171
+
# call the objective function, control
172
+
# when it is allowed to update and return
173
+
# control to optimizer
174
+
myOptimizer.call_objective(allow_update)
175
+
# check the current progress of the optimizer
176
+
# iter: the number of objective function calls
177
+
# eval: current 'best' evaluation of the optimizer
178
+
iter,eval= myOptimizer.get_convergence_data()
179
+
if (eval< best_eval)and (eval!=0):
180
+
best_eval=eval
181
+
182
+
# optional. if the optimizer is not printing out detailed
183
+
# reports, preview by checking the iteration and best evaluation
184
+
185
+
if suppress_output:
186
+
ifiter%100==0:#print out every 100th iteration update
187
+
print("Iteration")
188
+
print(iter)
189
+
print("Best Eval")
190
+
print(best_eval)
191
+
```
192
+
67
193
###Constraint Handling
68
194
Users must create their own constraint function for their problems, if there are constraints beyond the problem bounds. This is then passed into the constructor. If the default constraint function is used, it always returns true (which means there are no constraints).
69
195
@@ -75,8 +201,77 @@ Some updates have not incorporated appropriate handling for all boundary conditi
75
201
###Multi-Object Optimization
76
202
The no preference method of multi-objective optimization, but a Pareto Front is not calculated. Instead the best choice (smallest norm of output vectors) is listed as the output.
77
203
204
+
78
205
###Objective Function Handling
79
-
The optimizer minimizes the absolute value of the difference from the target outputs and the evaluated outputs. Future versions may include options for function minimization absent target values.
206
+
The optimizer minimizes the absolute value of the difference of the target outputs and the evaluated outputs. Future versions may include options for function minimization when target values are absent.
207
+
208
+
####Creating a Custom Objective Function
209
+
210
+
Custom objective functions can be used by creating a directory with the following files:
211
+
* configs_F.py
212
+
* constr_F.py
213
+
* func_F.py
214
+
215
+
`configs_F.py` contains lower bounds, upper bounds, the number of input variables, the number of output variables, the target values, and a global minimum if known. This file is used primarily for unit testing and evaluation of accuracy. If these values are not known, or are dynamic, then they can be included experimentally in the controller that runs the optimizer's state machine.
216
+
217
+
`constr_F.py` contains a function called`constr_F` that takes in an array,`X`, of particle positions to determine if the particle or agent is in a valid or invalid location.
218
+
219
+
`func_F.py` contains the objective function,`func_F`, which takes two inputs. The first input,`X`, is the array of particle or agent positions. The second input,`NO_OF_OUTS`, is the integer number of output variables, which is used to set the array size. In included objective functions, the default value is hardcoded to work with the specific objective function.
220
+
221
+
Below are examples of the format for these files.
222
+
223
+
`configs_F.py`:
224
+
```python
225
+
OBJECTIVE_FUNC= func_F
226
+
CONSTR_FUNC= constr_F
227
+
OBJECTIVE_FUNC_NAME="one_dim_x_test.func_F"#format: FUNCTION NAME.FUNCTION
228
+
CONSTR_FUNC_NAME="one_dim_x_test.constr_F"#format: FUNCTION NAME.FUNCTION
229
+
230
+
# problem dependent variables
231
+
LB= [[0]]# Lower boundaries
232
+
UB= [[1]]# Upper boundaries
233
+
IN_VARS=1# Number of input variables (x-values)
234
+
OUT_VARS=1# Number of output variables (y-values)
235
+
TARGETS= [0]# Target values for output
236
+
GLOBAL_MIN= []# Global minima sample, if they exist.
@@ -94,9 +289,9 @@ Each function has four files in a directory:
94
289
Other multi-objective functions can be applied to this project by following the same format (and several have been collected into a compatible library, and will be released in a separate repo)
<img src="https://github.com/LC-Linkous/chicken_swarm_python/blob/main/media/obj_func_pareto.png" alt="Function Feasible Decision Space and Objective Space with Pareto Front" height="200">
308
+
<img src="media/obj_func_pareto.png" alt="Function Feasible Decision Space and Objective Space with Pareto Front" height="200">
116
309
</p>
117
310
<palign="center">Plotted Multi-Objective Function Feasible Decision Space and Objective Space with Pareto Front</p>
<img src="https://github.com/LC-Linkous/chicken_swarm_python/blob/main/media/1D_test_plots.png" alt="Function Feasible Decision Space and Objective Space with Pareto Front" height="200">
325
+
<img src="media/1D_test_plots.png" alt="Function Feasible Decision Space and Objective Space with Pareto Front" height="200">
134
326
</p>
135
327
<palign="center">Plotted Single Input, Single-objective Function Feasible Decision Space and Objective Space with Pareto Front</p>
136
328
@@ -145,33 +337,31 @@ Local minima at $(0.444453, -0.0630916)$
145
337
146
338
Global minima at $(0.974857, -0.954872)$
147
339
148
-
149
340
##Example Implementations
150
341
151
342
###Basic Swarm Example
152
-
main_test.py provides a sample use case of the optimizer.
343
+
`main_test.py` provides a sample use case of the optimizer.
153
344
154
345
###Detailed Messages
155
-
main_test_details.py provides an example using a parent class, and the self.suppress_outputand detailedWarnings flags to control error messages that are passed back to the parent class to be printed with a timestamp. This implementation sets up the hooks for integration with AntennaCAT in order to provide the user feedback of warnings and errors.
346
+
`main_test_details.py` provides an example using a parent class, and the self.suppress_outputflag to control error messages that are passed back to the parent class to be printed with a timestamp. This implementation sets up the hooks for integration with AntennaCAT in order to provide the user feedback of warnings and errors.
main_test_graph.py provides an example using a parent class, and the self.suppress_outputand detailedWarnings flags to control error messages that are passed back to the parent class to be printed with a timestamp. Additionally, a realtime graph shows particle locations at every step.
354
+
`main_test_graph.py` provides an example using a parent class, and the self.suppress_outputflag to control error messages that are passed back to the parent class to be printed with a timestamp. Additionally, a realtime graph shows particle locations at every step.
164
355
165
356
NOTE: if you close the graph as the code is running, the code will continue to run, but the graph will not re-open.
166
357
167
358
##References
168
359
169
360
[1] X. B. Meng, Y. Liu, X. Gao, and H. Zhang, "A new bio-inspired algorithm: Chicken swarm optimization," in Proc. Int. Conf. Swarm Intell. Cham, Switzerland, Springer, 2014, pp. 86–94.
170
361
171
-
##Publications andIntegration
362
+
##RelatedPublications andRepositories
172
363
This software works as a stand-alone implementation, and as one of the optimizers integrated into AntennaCAT.
173
364
174
-
Publications featuring the code in this repo will be added as they become public.