-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectiveFunction_Multi.py
776 lines (613 loc) · 27 KB
/
ObjectiveFunction_Multi.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
"""!
@file src/ObjectiveFunction.py
@package Gnowee
@defgroup ObjectiveFunction ObjectiveFunction
@brief Defines a class to perform objective function calculations.
This class contains the necessary functions and methods to create objective
functions and initialize the necessary parameters. The class is pre-stocked
with common benchmark functions for easy fishing.
Users can modify the this class to add additional functions following the
format of the functions currently in the class.
@author James Bevins
@date 23May17
@copyright <a href='../../licensing/COPYRIGHT'>© 2017 UC
Berkeley Copyright and Disclaimer Notice</a>
@license <a href='../../licensing/LICENSE'>GNU GPLv3.0+ </a>
"""
import numpy as np
import operator
from math import sqrt, exp, log, cos, pi
#-----------------------------------------------------------------------------#
class ObjectiveFunction_multi(object):
"""!
@ingroup ObjectiveFunction
This class creates a ObjectiveFunction object that can be used in
optimization algorithms.
"""
def __init__(self, method=None, objective=None):
"""!
Constructor to build the ObjectiveFunction class.
This class specifies the objective function to be used for a
optimization process.
@param self: <em> ObjectiveFunction pointer </em> \n
The ObjectiveFunction pointer. \n
@param method: \e string \n
The name of the objective function to evaluate. \n
@param objective: <em> integer, float, or numpy array </em> \n
The desired objective associated with the optimization. The
chosen value and type must be compatible with the optiization
function chosen. This is used in objective functions that involve
a comparison against a desired outcome. \n
"""
#CHANGES MADE BY ALEX:
#the program should now expect self to be a 1 by n array that
#stores the various objective functions
## @var _FUNC_DICT
# <em> dictionary of function handles: </em> Stores
# the mapping between the string names and function handles for
# the objective function evaluations in the class. This is a
# legacy private variable that is only used for error reporting.
self._FUNC_DICT = {'spring': self.spring,
'mi_spring': self.mi_spring,
'welded_beam': self.welded_beam,
'pressure_vessel': self.pressure_vessel,
'mi_pressure_vessel': self.mi_pressure_vessel,
'speed_reducer': self.speed_reducer,
'mi_chemical_process': self.mi_chemical_process,
'ackley': self.ackley,
'shifted_ackley': self.shifted_ackley,
'dejong': self.dejong,
'shifted_dejong': self.shifted_dejong,
'easom': self.easom,
'shifted_easom': self.shifted_easom,
'griewank': self.griewank,
'shifted_griewank': self.shifted_griewank,
'rastrigin': self.rastrigin,
'shifted_rastrigin': self.shifted_rastrigin,
'rosenbrock': self.rosenbrock,
'shifted_rosenbrock': self.shifted_rosenbrock,
'tsp': self.tsp}
## @var func
# <em> function handle: </em> The function handle for the
# objective function to be used for the optimization. The
# function must be specified as a method of the class.
if method != None and type(method) == str:
self.set_obj_func(method)
else:
self.func = method #
## @var objective
# <em> integer, float, or numpy array: </em> The desired outcome
# of the optimization.
self.objective = objective
def __repr__(self):
"""!
ObjectiveFunction class param print function.
@param self: \e ObjectiveFunction pointer \n
The ObjectiveFunction pointer. \n
"""
return "ObjectiveFunction({}, {})".format(self.func.__name__,
self.objective)
def __str__(self):
"""!
Human readable ObjectiveFunction print function.
@param self: \e ObjectiveFunction pointer \n
The ObjectiveFunction pointer. \n
"""
header = [" ObjectiveFunction:"]
header += ["Function: {}".format(self.func.__name__)]
header += ["Objective: {}".format(self.objective)]
return "\n".join(header)+"\n"
def set_obj_func(self, funcName):
"""!
Converts an input string name for a function to a function handle.
@param self: \e pointer \n
The ObjectiveFunction pointer. \n
@param funcName \e string \n
A string identifying the objective function to be used. \n
"""
if hasattr(funcName, '__call__'):
self.func = funcName
else:
try:
self.func = getattr(self, funcName)
assert hasattr(self.func, '__call__'), 'Invalid function handle'
except KeyError:
print ('ERROR: The function specified does not exist in the '
'ObjectiveFunction class or the _FUNC_DICT. Allowable '
'methods are {}'.format(self._FUNC_DICT))
#-----------------------------------------------------------------------------#
# The following sections are user modifiable to all for the use of new
# objective functions that have not yet been implemented. The same format must
# be followed to work with the standard Coeus call.
#
# Alternatively, the user can specify additional functions in their own files.
# Examples of both are shown in the runGnowee ipython notebook in the /src
# directory.
#-----------------------------------------------------------------------------#
def spring(self, u):
"""!
Spring objective function.
Nearly optimal Example: \n
u = [0.05169046, 0.356750, 11.287126] \n
fitness = 0.0126653101469
Taken from: "Solving Engineering Optimization Problems with the
Simple Constrained Particle Swarm Optimizer"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 3, ('Spring design needs to specify D, W, and L and '
'only those 3 parameters.')
assert u[0] != 0 and u[1] != 0 and u[2] != 0, ('Design values {} '
'cannot be zero.'.format(u))
# Evaluate fitness
fitness = ((2+u[2])*u[0]**2*u[1])
return fitness
def mi_spring(self, u):
"""!
Spring objective function.
Optimal Example: \n
u = [1.22304104, 9, 36] = [1.22304104, 9, 0.307]\n
fitness = 2.65856
Taken from Lampinen, "Mixed Integer-Discrete-Continuous Optimization
by Differential Evolution"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e float: The fitness associated with the specified input. \n
"""
assert len(u) == 3, ('Spring design needs to specify D, N, and d and '
'only those 3 parameters.')
# Set variables
D = u[0]
N = u[1]
d = u[2]
# Variable Definititions:
Fmax = 1000
S = 189000.0
Fp = 300
sigmapm = 6.0
sigmaw = 1.25
G = 11.5*10**6
lmax = 14
dmin = 0.2
Dmax = 3.0
K = G*d**4/(8*N*D**3)
sigmap = Fp/K
Cf = (4*(D/d)-1)/(4*(D/d)-4)+0.615*d/D
lf = Fmax/K+1.05*(N+2)*d
#Evaluate fitness
fitness = np.pi**2*D*d**2*(N+2)/4
return fitness
def welded_beam(self, u):
"""!
Welded Beam objective function.
Nearly optimal Example: \n
u = [0.20572965, 3.47048857, 9.0366249, 0.20572965] \n
fitness = 1.7248525603892848
Taken from: "Solving Engineering Optimization Problems with the
Simple Constrained Particle Swarm Optimizer"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 4, ('Welded Beam design needs to specify 4 '
'parameters.')
assert u[0] != 0 and u[1] != 0 and u[2] != 0 and u[3] != 0, ('Design'
'values {} cannot be zero'.format(u))
# Problem variable definitions
em = 6000.*(14+u[1]/2.)
r = sqrt(u[1]**2/4.+((u[0]+u[2])/2.)**2)
j = 2.*(u[0]*u[1]*sqrt(2)*(u[1]**2/12.+((u[0]+u[2])/2.)**2))
tau_p = 6000./(sqrt(2)*u[0]*u[1])
tau_dp = em*r/j
tau = sqrt(tau_p**2+2.*tau_p*tau_dp*u[1]/(2.*r)+tau_dp**2)
sigma = 504000./(u[3]*u[2]**2)
delta = 65856000./((30*10**6)*u[3]*u[2]**2)
pc = 4.013*(30.*10**6)*sqrt(u[2]**2*u[3]**6/36.)/196.*(1.-u[2] \
*sqrt((30.*10**6)/(4.*(12.*10**6)))/28.)
#Evaluate fitness
fitness = 1.10471*u[0]**2*u[1]+0.04811*u[2]*u[3]*(14.0+u[1])
return fitness
def pressure_vessel(self, u):
"""!
Pressure vessel objective function.
Nearly optimal obtained using Gnowee: \n
u = [0.778169, 0.384649, 40.319619, 199.999998] \n
fitness = 5885.332800
Taken from: "Solving Engineering Optimization Problems with the
Simple Constrained Particle Swarm Optimizer"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 4, ('Pressure vesseldesign needs to specify 4 '
'parameters.')
assert u[0] != 0 and u[1] != 0 and u[2] != 0 and u[3] != 0, ('Design'
'values {} cannot be zero'.format(u))
#Evaluate fitness
fitness = 0.6224*u[0]*u[2]*u[3]+1.7781*u[1]*u[2]**2+3.1661*u[0]**2 \
*u[3]+19.84*u[0]**2*u[2]
return fitness
def mi_pressure_vessel(self, u):
"""!
Mixed Integer Pressure vessel objective function.
Nearly optimal example: \n
u = [58.2298, 44.0291, 17, 9] \n
fitness = 7203.24
Optimal example obtained with Gnowee: \n
u = [38.819876, 221.985576, 0.750000, 0.375000] \n
fitness = 5855.893191
Taken from: "Nonlinear Integer and Discrete Programming in Mechanical
Design Optimization"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 4, ('MI Pressure vessel design needs to specify 4 '
'parameters.')
# Set variables
R = u[0]
L = u[1]
ts = u[2]
th = u[3]
#Evaluate fitness
fitness = 0.6224*R*ts*L+1.7781*R**2*th+3.1611*ts**2*L+19.8621*R*ts**2
return fitness
def speed_reducer(self, u):
"""!
Speed reducer objective function.
Nearly optimal example: \n
u = [58.2298, 44.0291, 17, 9] \n
fitness = 2996.34784914
Taken from: "Solving Engineering Optimization Problems with the
Simple Constrained Particle Swarm Optimizer"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 7, ('Speed reducer design needs to specify 7 '
'parameters.')
assert u[0] != 0 and u[1] != 0 and u[2] != 0 and u[3] != 0 and \
u[4] != 0 and u[5] != 0 and u[6] != 0, ('Design values cannot '
'be zero {}.'.format(u))
#Evaluate fitness
fitness = 0.7854*u[0]*u[1]**2*(3.3333*u[2]**2+14.9334*u[2]-43.0934) \
- 1.508*u[0]*(u[5]**2+u[6]**2) + 7.4777*(u[5]**3+u[6]**3) \
+ 0.7854*(u[3]*u[5]**2+u[4]*u[6]**2)
return fitness
def mi_chemical_process(self, u):
"""!
Chemical process design mixed integer problem.
Optimal example: \n
u = [(0.2, 0.8, 1.907878, 1, 1, 0, 1] \n
fitness = 4.579582
Taken from: "An Improved PSO Algorithm for Solving Non-convex
NLP/MINLP Problems with Equality Constraints"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated.
[x1, x2, x3, y1, y2, y3, y4] \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 7, ('Chemical process design needs to specify 7 '
'parameters.')
#Evaluate fitness
fitness = (u[3]-1)**2 + (u[4]-2)**2 + (u[5]-1)**2 - log(u[6]+1) \
+ (u[0]-1)**2 + (u[1]-2)**2 + (u[2]-3)**2
return fitness
def ackley(self, u):
"""!
Ackley Function: Mulitmodal, n dimensional
Optimal example: \n
u = [0, 0, 0, 0, ... n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Ackley Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = -20*exp(-1./5.*sqrt(1./len(u) \
*sum(u[i]**2 for i in range(len(u))))) \
- exp(1./len(u)*sum(cos( \
2*pi*u[i]) for i in range(len(u)))) + 20 + exp(1)
return fitness
def shifted_ackley(self, u):
"""!
Ackley Function: Mulitmodal, n dimensional
Ackley Function that is shifted from the symmetric 0, 0, 0, ..., 0
optimimum.
Optimal example: \n
u = [0, 1, 2, 3, ... n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Shifted Ackley Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = -20*exp(-1./5.*sqrt(1./len(u) \
*sum((u[i]-i)**2 for i in range(len(u))))) \
- exp(1./len(u)*sum(cos(2*pi* \
(u[i]-i)) for i in range(len(u)))) + 20 + exp(1)
return fitness
def dejong(self, u):
"""!
De Jong Function: Unimodal, n-dimensional
Optimal example: \n
u = [0, 0, 0, 0, ... n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The De Jong Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = sum(i**2 for i in u)
return fitness
def shifted_dejong(self, u):
"""!
De Jong Function: Unimodal, n-dimensional
De Jong Function that is shifted from the symmetric 0, 0, 0, ..., 0
optimimum.
Optimal example: \n
u = [0, 1, 2, 3, ... n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
Taken from: "Solving Engineering Optimization Problems with the
Simple Constrained Particle Swarm Optimizer"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Shifted De Jong Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = sum((u[i]-i)**2 for i in range(len(u)))
return fitness
def easom(self, u):
"""!
Easom Function: Multimodal, n-dimensional
Optimal example: \n
u = [pi, pi] \n
fitness = 1.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 2, 'The Easom Function must have a dimension of 2.'
#Evaluate fitness
fitness = -cos(u[0])*cos(u[1])*exp(-(u[0]-pi)**2 \
-(u[1]-pi)**2)
return fitness
def shifted_easom(self, u):
"""!
Easom Function: Multimodal, n-dimensional
Easom Function that is shifted from the symmetric pi, pi optimimum.
Optimal example: \n
u = [pi, pi+1] \n
fitness = 1.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) == 2, 'The Easom Function must have a dimension of 2.'
#Evaluate fitness
fitness = -cos(u[0])*cos(u[1]-1)*exp(-(u[0]-pi)**2 \
-(u[1]-1-pi)**2)
return fitness
def griewank(self, u):
"""!
Griewank Function: Multimodal, n-dimensional
Optimal example: \n
u = [0, 0, 0, ..., 0] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1 and len(u) <= 600, ('The Shifted Griewank Function '
'must have a dimension between 1 and 600.')
#Evaluate fitness
fitness = 1./4000.*sum((u[i])**2 for i in range(len(u))) \
- prod(cos(u[i]/sqrt(i+1)) for i in range(len(u))) +1.
return fitness
def shifted_griewank(self, u):
"""!
Griewank Function: Multimodal, n-dimensional
Griewank Function that is shifted from the symmetric 0, 0, 0, ..., 0
optimimum.
Optimal example: \n
u = [0, 1, 2, ..., n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
The design parameters to be evaluated. \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1 and len(u) <= 600, ('The Shifted Griewank Function '
'must have a dimension between 1 and 600.')
#Evaluate fitness
fitness = 1./4000.*sum((u[i]-i)**2 for i in range(len(u))) \
-prod(cos((u[i]-i)/sqrt(i+1)) for i in range(len(u))) +1.
return fitness
def rastrigin(self, u):
"""!
Rastrigin Function: Multimodal, n-dimensional
Optimal example: \n
u = [0, 0, 0, ..., 0] \n
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Rastrigin Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = 10.*len(u)+sum((u[i])**2 -10. \
*np.cos(2.*np.pi*u[i]) for i in range(len(u)))
return fitness
def shifted_rastrigin(self, u):
"""!
Rastrigin Function: Multimodal, n-dimensional
Rastrigin Function that is shifted from the symmetric 0, 0, 0, ..., 0
optimimum.
Optimal example: \n
u = [0, 1, 2, ..., n-1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Shifted Rastrigin Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = 10.*len(u)+sum((u[i]-i)**2 -10. \
*np.cos(2.*np.pi*(u[i]-i)) for i in range(len(u)))
return fitness
def rosenbrock(self, u):
"""!
Rosenbrock Function: uni-modal, n-dimensional.
Optimal example: \n
u = [1, 1, 1, ..., 1] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Rosenbrock Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = sum((u[i]-1)**2 +100. \
*(u[i+1]-u[i]**2)**2 for i in range(len(u)-1))
return fitness
def shifted_rosenbrock(self, u):
"""!
Rosenbrock Function: uni-modal, n-dimensional
Rosenbrock Function that is shifted from the symmetric 0,0,0...0
optimimum.
Optimal example: \n
u = [1, 2, 3, ...n] \n
fitness = 0.0
Taken from: "Nature-Inspired Optimization Algorithms"
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
assert len(u) >= 1, ('The Shifted Rosenbrock Function must have a '
'dimension greater than 1.')
#Evaluate fitness
fitness = sum((u[i]-1-i)**2 +100.*((u[i+1]-(i+1)) \
-(u[i]-i)**2)**2 for i in range(len(u)-1))
return fitness
def tsp(self, u):
"""!
Generic objective funtion to evaluate the TSP optimization by
calculating total distance traveled.
@param self: <em> pointer </em> \n
The ObjectiveFunction pointer. \n
@param u: \e array \n
@return \e array: The fitness associated with the specified input. \n
@return \e array: The assessed value for each constraint for the
specified input. \n
"""
#Evaluate fitness
fitness = 0
for i in range(1, len(u), 1):
fitness = fitness+round(sqrt((u[i][0]-u[i-1][0])**2 \
+(u[i][1]-u[i-1][1])**2))
#Complete the tour
fitness = fitness+round(sqrt((u[0][0]-u[-1][0])**2 \
+(u[0][1]-u[-1][1])**2))
return fitness
#-----------------------------------------------------------------------------#
def prod(iterable):
"""!
@ingroup ObjectiveFunction
Computes the product of a set of numbers (ie big PI, mulitplicative
equivalent to sum).
@param iterable: <em> list or array or generator </em>
Iterable set to multiply.
@return \e float: The product of all of the items in iterable
"""
return reduce(operator.mul, iterable, 1)