-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathconstraints_handler.py
1632 lines (1474 loc) · 76.1 KB
/
constraints_handler.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""A collection of boundary and (in future) constraints handling classes.
"""
from __future__ import absolute_import, division, print_function #, unicode_literals
# __package__ = 'cma'
import warnings as _warnings
import collections as _collections
import functools as _functools
import numpy as np
from numpy import logical_and as _and, logical_or as _or, logical_not as _not
from .utilities.utils import rglen, is_
from .utilities.math import Mh as _Mh, moving_average
from .logger import Logger as _Logger # we can assign _Logger = cma.logger.LoggerDummy to turn off logging
from .transformations import BoxConstraintsLinQuadTransformation
from .optimization_tools import BestSolution2
from .utilities.python3for2 import range
del absolute_import, division, print_function #, unicode_literals
_warnings.filterwarnings('once', message="``import moarchiving`` failed.*")
class BoundaryHandlerBase(object):
"""quick hack versatile base class"""
def __init__(self, bounds):
"""bounds are not copied, but possibly modified and
put into a normalized form: ``bounds`` can be ``None``
or ``[lb, ub]`` where ``lb`` and ``ub`` are
either None or a vector (which can have ``None`` entries).
Generally, the last entry is recycled to compute bounds
for any dimension.
"""
if bounds in [None, (), []]:
self.bounds = None
else:
if not isinstance(bounds, (tuple, list)) or len(bounds) != 2:
raise ValueError(
"bounds must be None, empty, or a list of length 2"
" where each element may be a scalar, list, array,"
" or None; type(bounds) was: %s" % str(type(bounds)))
l = [None, None] # figure out lengths
for i in [0, 1]:
try:
l[i] = len(bounds[i])
except TypeError:
bounds[i] = [bounds[i]]
l[i] = 1
if all([bounds[i][j] is None or not np.isfinite(bounds[i][j])
for j in rglen(bounds[i])]):
bounds[i] = None
if bounds[i] is not None and any([bounds[i][j] == (-1)**i * np.inf
for j in rglen(bounds[i])]):
raise ValueError('lower/upper is +inf/-inf and ' +
'therefore no finite feasible solution is available')
self.bounds = bounds
def amend_bounds_for_integer_variables(self, integer_indices, at=0.5, offset=1e-9):
"""set bounds away from ``at=0.5`` such that
a repaired solution is always rounded into the feasible domain.
"""
if not integer_indices or not self.has_bounds():
return
def set_bounds(which, idx, integer_indices):
"""idx is a `bool` index array of ``bound % 1 == 0.5``"""
assert which in (0, 1), which
if not np.any(idx):
return
for i in np.nonzero(idx)[0]:
if i not in integer_indices:
idx[i] = False
if not np.any(idx):
return
dimension = max((len(self.bounds[which]),
np.max(np.nonzero(idx)[0]) + 1))
bounds = self.get_bounds(which, dimension)
if len(bounds) < len(idx):
idx = idx[:len(bounds)] # last nonzero entry determined len
elif len(bounds) > len(idx):
idx = np.hstack([idx, np.zeros(len(bounds) - len(idx), dtype=bool)])
bounds[idx] += (1 - 2 * which) * offset * np.maximum(1, np.abs(bounds[idx]))
self.bounds[which] = bounds
self._bounds_dict = {}
dimension = max(integer_indices) + 1
for which in (0, 1):
if not self.has_bounds('upper' if which else 'lower'):
continue
bounds = self.get_bounds(which, dimension)
set_bounds(which, np.mod(bounds, 1) == at, integer_indices)
def __call__(self, solutions, *args, **kwargs):
"""return penalty or list of penalties, by default zero(s).
This interface seems too specifically tailored to the derived
BoundPenalty class, it should maybe change.
"""
if np.isscalar(solutions[0]):
return 0.0
else:
return len(solutions) * [0.0]
def update(self, *args, **kwargs):
"""end-iteration callback of boundary handler (abstract/empty)"""
return self
def repair(self, x, copy_if_changed=True):
"""projects infeasible values on the domain bound, might be
overwritten by derived class """
copy = copy_if_changed
if self.bounds is None:
return x
for ib in [0, 1]:
if self.bounds[ib] is None:
continue
for i in rglen(x):
idx = min([i, len(self.bounds[ib]) - 1])
if self.bounds[ib][idx] is not None and \
(-1)**ib * x[i] < (-1)**ib * self.bounds[ib][idx]:
if copy:
x = np.array(x, copy=True)
copy = False
x[i] = self.bounds[ib][idx]
return x
def inverse(self, y, copy_if_changed=True):
"""inverse of repair if it exists, at least it should hold
``repair == repair o inverse o repair``"""
return y
def get_bounds(self, which, dimension):
"""``get_bounds('lower', 8)`` returns the lower bounds in 8-D"""
if which in ['lower', 0, '0']:
return self._get_bounds(0, dimension)
elif which in ['upper', 1, '1']:
return self._get_bounds(1, dimension)
else:
raise ValueError("argument which must be 'lower' or 'upper'")
def _get_bounds(self, ib, dimension):
"""ib == 0/1 means lower/upper bound, return a vector of length
`dimension` """
sign_ = 2 * ib - 1
assert sign_**2 == 1
if self.bounds is None or self.bounds[ib] is None:
return np.array(dimension * [sign_ * np.inf])
res = []
for i in range(dimension):
res.append(self.bounds[ib][min([i, len(self.bounds[ib]) - 1])])
if res[-1] is None:
res[-1] = sign_ * np.inf
return np.array(res)
def has_bounds(self, which='both'):
"""return `True` if any variable is bounded"""
valid_whichs = (None, 'both', 'lower', 'upper')
if which not in valid_whichs:
raise ValueError("`which` parameter must be in {0} but was ={1}"
.format(valid_whichs, which))
bounds = self.bounds
if bounds is None or bounds in (False, [], ()) or (
bounds[0] is None and bounds[1] is None):
return False
for ib, bound in enumerate(bounds):
if bound is None or (
ib == 0 and which == 'upper') or (
ib == 1 and which == 'lower'):
continue
sign_ = 2 * ib - 1
for bound_i in bound:
if bound_i is not None and sign_ * bound_i < np.inf:
return True
return False
def is_in_bounds(self, x):
"""not yet tested"""
if self.bounds is None:
return True
for ib in [0, 1]:
if self.bounds[ib] is None:
continue
for i in rglen(x):
idx = min([i, len(self.bounds[ib]) - 1])
if self.bounds[ib][idx] is None:
continue
if ((ib == 0 and x[i] < self.bounds[ib][idx]) or (
ib == 1 and x[i] > self.bounds[ib][idx])):
return False
return True
def idx_out_of_bounds(self, x):
"""return index list of out-of-bound values in `x`.
``if bounds.idx_out_of_bounds`` evaluates to `True` if and only if
`x` is out of bounds.
"""
if self.bounds is None:
return []
idxs = []
for ib in [0, 1]:
if self.bounds[ib] is None:
continue
for i in rglen(x):
idx = min([i, len(self.bounds[ib]) - 1])
if self.bounds[ib][idx] is None:
continue
if ((ib == 0 and x[i] < self.bounds[ib][idx]) or (
ib == 1 and x[i] > self.bounds[ib][idx])):
idxs += [i]
return sorted(idxs)
def get_bound(self, index):
"""return lower and upper bound of variable with index `index`"""
if self.bounds is None:
return [-np.inf, np.inf]
res = []
for ib in [0, 1]:
if self.bounds[ib] is None or len(self.bounds[ib]) == 0:
b = None
else:
b = self.bounds[ib][min((index, len(self.bounds[ib]) - 1))]
res.append([-np.inf, np.inf][ib] if b is None else b)
return res
def to_dim_times_two(self, bounds):
"""return boundaries in format ``[[lb0, ub0], [lb1, ub1], ...]``,
as used by ``BoxConstraints...`` class.
"""
if not bounds:
b = [[None, None]]
else:
l = [None, None] # figure out lenths
for i in [0, 1]:
try:
l[i] = len(bounds[i])
except TypeError:
bounds[i] = [bounds[i]]
l[i] = 1
if l[0] != l[1] and 1 not in l and None not in (
bounds[0][-1], bounds[1][-1]): # disallow different lengths
raise ValueError(
"lower and upper bounds must have the same length\n"
"or length one or `None` as last element (the last"
" element is always recycled).\n"
"Lengths were %s"
% str(l))
b = [] # bounds in different format
try:
for i in range(max(l)):
b.append([bounds[0][min((i, l[0] - 1))],
bounds[1][min((i, l[1] - 1))]])
except (TypeError, IndexError):
print("boundaries must be provided in the form " +
"[scalar_of_vector, scalar_or_vector]")
raise
return b
class BoundNone(BoundaryHandlerBase):
"""no boundaries"""
def __init__(self, bounds=None):
if bounds is not None:
raise ValueError()
# BoundaryHandlerBase.__init__(self, None)
super(BoundNone, self).__init__(None)
def is_in_bounds(self, x):
return True
class BoundTransform(BoundaryHandlerBase):
"""Handle boundaries by a smooth, piecewise linear and quadratic
transformation into the feasible domain.
>>> import numpy as np
>>> import cma
>>> from cma.constraints_handler import BoundTransform
>>> from cma import fitness_transformations as ft
>>> veq = cma.utilities.math.Mh.vequals_approximately
>>> b = BoundTransform([0, None])
>>> assert b.bounds == [[0], [None]]
>>> assert veq(b.repair([-0.1, 0, 1, 1.2]), np.array([0.0125, 0.0125, 1, 1.2])), b.repair([-0.1, 0, 1, 1.2])
>>> assert b.is_in_bounds([0, 0.5, 1])
>>> assert veq(b.transform([-1, 0, 1, 2]), [0.9, 0.0125, 1, 2 ]), b.transform([-1, 0, 1, 2])
>>> bounded_sphere = ft.ComposedFunction([
... cma.ff.sphere,
... BoundTransform([[], 5 * [-1] + [np.inf]]).transform
... ])
>>> o1 = cma.fmin(bounded_sphere, 6 * [-2], 0.5) # doctest: +ELLIPSIS
(4_w,9)-aCMA-ES (mu_w=2.8,w_1=49%) in dimension 6 (seed=...
>>> o2 = cma.fmin(cma.ff.sphere, 6 * [-2], 0.5, options={
... 'BoundaryHandler': cma.s.ch.BoundTransform,
... 'bounds': [[], 5 * [-1] + [np.inf]] }) # doctest: +ELLIPSIS
(4_w,9)-aCMA-ES (mu_w=2.8,w_1=49%) in dimension 6 (seed=...
>>> assert o1[1] < 5 + 1e-8 and o2[1] < 5 + 1e-8
>>> b = BoundTransform([-np.random.rand(120), np.random.rand(120)])
>>> for i in range(0, 100, 9):
... x = (-i-1) * np.random.rand(120) + i * np.random.randn(120)
... x_to_b = b.repair(x)
... x2 = b.inverse(x_to_b)
... x2_to_b = b.repair(x2)
... x3 = b.inverse(x2_to_b)
... x3_to_b = b.repair(x3)
... assert veq(x_to_b, x2_to_b)
... assert veq(x2, x3)
... assert veq(x2_to_b, x3_to_b)
>>> for _ in range(5):
... lb = np.random.randn(4)
... ub = lb + 1e-7 + np.random.rand(4)
... b = BoundTransform([lb, ub])
... for x in [np.random.randn(4) / np.sqrt(np.random.rand(4)) for _ in range(22)]:
... assert all(lb <= b.transform(x)), (lb, ub, b.__dict__)
... assert all(b.transform(x) <= ub), (lb, ub, b.__dict__)
Details: this class uses ``class BoxConstraintsLinQuadTransformation``
"""
def __init__(self, bounds=None):
"""Argument bounds can be `None` or ``bounds[0]`` and ``bounds[1]``
are lower and upper domain boundaries, each is either `None` or
a scalar or a list or array of appropriate size.
"""
# BoundaryHandlerBase.__init__(self, bounds)
super(BoundTransform, self).__init__(bounds)
self.bounds_tf = BoxConstraintsLinQuadTransformation(self.to_dim_times_two(bounds))
def repair(self, x, copy_if_changed=True):
"""transforms ``x`` into the bounded domain.
"""
copy = copy_if_changed
if self.bounds is None or (self.bounds[0] is None and
self.bounds[1] is None):
return x
return np.asarray(self.bounds_tf(x, copy))
def transform(self, x):
return self.repair(x)
def inverse(self, x, copy_if_changed=True):
"""inverse transform of ``x`` from the bounded domain.
"""
if self.bounds is None or (self.bounds[0] is None and
self.bounds[1] is None):
return x
return np.asarray(self.bounds_tf.inverse(x, copy_if_changed)) # this doesn't exist
class BoundPenalty(BoundaryHandlerBase):
"""Compute a bound penalty and update coordinate-wise penalty weights.
An instance must be updated each iteration using the `update` method.
Details:
- The penalty computes like ``sum(w[i] * (x[i]-xfeas[i])**2)``,
where ``xfeas`` is the closest feasible (in-bounds) solution from
``x``. The weight ``w[i]`` should be updated during each iteration
using the update method.
Example how this boundary handler is used with `cma.fmin` via the
options (`CMAOptions`) of the class `cma.CMAEvolutionStrategy`:
>>> import cma
>>> res = cma.fmin(cma.ff.elli, 6 * [0.9], 0.1,
... {'BoundaryHandler': cma.BoundPenalty,
... 'bounds': [-1, 1],
... 'tolflatfitness': 10,
... 'fixed_variables': {0: 0.012, 2:0.234}
... }) # doctest: +ELLIPSIS
(4_w,8)-aCMA-ES (mu_w=2.6,w_1=52%) in dimension 4 (seed=...
>>> if res[1] >= 13.76: print(res) # should never happen
Reference: Hansen et al 2009, A Method for Handling Uncertainty...
IEEE TEC, with addendum, see
https://ieeexplore.ieee.org/abstract/document/4634579
https://hal.inria.fr/inria-00276216/file/TEC2008.pdf
**todo**: implement a more generic interface, where this becomes a
fitness wrapper which adds the desired penalty and the `update`
method is used as callback argument for `fmin` like::
f = cma.BoundPenalty(cma.ff.elli, bounds=[-1, 1])
res = cma.fmin(f, 6 * [1], callback=f.update)
where callback functions should receive the same arguments as
`tell`, namely an `CMAEvolutionStrategy` instance, an array of the
current solutions and their respective f-values. Such change is
relatively involved. Consider also that bounds are related with the
geno- to phenotype transformation.
"""
def __init__(self, bounds=None):
"""Argument bounds can be `None` or ``bounds[0]`` and ``bounds[1]``
are lower and upper domain boundaries, each is either `None` or
a scalar or a `list` or `np.array` of appropriate size.
"""
# #
# bounds attribute reminds the domain boundary values
# BoundaryHandlerBase.__init__(self, bounds)
super(BoundPenalty, self).__init__(bounds)
self.gamma = 1 # a very crude assumption
self.weights_initialized = False # gamma becomes a vector after initialization
self.hist = [] # delta-f history
def repair(self, x, copy_if_changed=True):
"""sets out-of-bounds components of ``x`` on the bounds.
"""
# TODO (old data): CPU(N,lam,iter=20,200,100): 3.3s of 8s for two bounds, 1.8s of 6.5s for one bound
# remark: np.max([bounds[0], x]) is about 40 times slower than max((bounds[0], x))
copy = copy_if_changed
if self.has_bounds():
bounds = self.bounds
if copy:
x = np.array(x, copy=True)
if bounds[0] is not None:
if np.isscalar(bounds[0]):
for i in rglen(x):
x[i] = max((bounds[0], x[i]))
else:
for i in rglen(x):
j = min([i, len(bounds[0]) - 1])
if bounds[0][j] is not None:
x[i] = max((bounds[0][j], x[i]))
if bounds[1] is not None:
if np.isscalar(bounds[1]):
for i in rglen(x):
x[i] = min((bounds[1], x[i]))
else:
for i in rglen(x):
j = min((i, len(bounds[1]) - 1))
if bounds[1][j] is not None:
x[i] = min((bounds[1][j], x[i]))
return x
# ____________________________________________________________
#
def __call__(self, x, archive, gp):
"""returns the boundary violation penalty for `x`,
where `x` is a single solution or a list or np.array of solutions.
"""
# if x in (None, (), []): # breaks when x is a nparray
# return x
if not self.has_bounds():
return 0.0 if np.isscalar(x[0]) else [0.0] * len(x) # no penalty
x_is_single_vector = np.isscalar(x[0])
if x_is_single_vector:
x = [x]
# add fixed variables to self.gamma
try:
gamma = list(self.gamma) # fails if self.gamma is a scalar
for i in sorted(gp.fixed_values): # fails if fixed_values is None
gamma.insert(i, 0.0)
gamma = np.asarray(gamma)
except TypeError:
gamma = self.gamma
pen = []
for xi in x:
# CAVE: this does not work with already repaired values!!
# CPU(N,lam,iter=20,200,100)?: 3s of 10s, np.array(xi): 1s
# remark: one deep copy can be prevented by xold = xi first
xpheno = xi if gp.isidentity else gp.pheno(archive[xi]['geno'])
# necessary, because xi was repaired to be in bounds
xinbounds = self.repair(xpheno)
# could be omitted (with unpredictable effect in case of external repair)
fac = 1 # exp(0.1 * (log(self.scal) - np.mean(self.scal)))
pen.append(sum(gamma * ((xinbounds - xpheno) / fac)**2) / len(xi))
return pen[0] if x_is_single_vector else pen
# ____________________________________________________________
#
def feasible_ratio(self, solutions):
"""counts for each coordinate the number of feasible values in
``solutions`` and returns an `np.array` of length
``len(solutions[0])`` with the ratios.
"""
raise NotImplementedError
# ____________________________________________________________
#
def update(self, function_values, es):
"""updates the weights for computing a boundary penalty.
Arguments
=========
``function_values``:
all function values of recent population of solutions
``es``:
`CMAEvolutionStrategy` object instance, in particular
mean and variances and the methods from the attribute
`gp` of type `GenoPheno` are used.
"""
if self.bounds is None or (self.bounds[0] is None and
self.bounds[1] is None):
return self
N = es.N
# ## prepare
# compute varis = sigma**2 * C_ii
if 11 < 3: # old
varis = es.sigma**2 * np.array(N * [es.C] if np.isscalar(es.C) else (# scalar case
es.C if np.isscalar(es.C[0]) else # diagonal matrix case
[es.C[i][i] for i in range(N)])) # full matrix case
else:
varis = es.sigma**2 * es.sm.variances
# relative violation in geno-space
dmean = (es.mean - es.gp.geno(self.repair(es.gp.pheno(es.mean)))) / varis**0.5
# ## Store/update a history of delta fitness value
fvals = sorted(function_values)
_l = 1 + len(fvals)
val = fvals[3 * _l // 4] - fvals[_l // 4] # exact interquartile range apart interpolation
val = val / np.mean(varis) # new: val is normalized with sigma of the same iteration
# insert val in history
if np.isfinite(val) and val > 0:
self.hist.insert(0, val)
elif val == np.inf and len(self.hist) > 1:
self.hist.insert(0, max(self.hist))
else:
pass # ignore 0 or nan values
if len(self.hist) > 20 + (3 * N) / es.popsize:
self.hist.pop()
# ## prepare
dfit = np.median(self.hist) # median interquartile range
damp = min(1, es.sp.weights.mueff / 10. / N)
# ## set/update weights
# Throw initialization error
if len(self.hist) == 0:
raise ValueError('wrongful initialization, no feasible solution sampled. ' +
'Reasons can be mistakenly set bounds (lower bound not smaller than upper bound) or a too large initial sigma0 or... ' +
'See description of argument func in help(cma.fmin) or an example handling infeasible solutions in help(cma.CMAEvolutionStrategy). ')
# initialize weights
if dmean.any() and (not self.weights_initialized or es.countiter == 2): # TODO
self.gamma = np.array(N * [2 * dfit]) ## BUGBUGzzzz: N should be phenotypic (bounds are in phenotype), but is genotypic
self.weights_initialized = True
# update weights gamma
if self.weights_initialized:
edist = np.array(abs(dmean) - 3 * max(1, N**0.5 / es.sp.weights.mueff))
if 1 < 3: # this is better, around a factor of two
# increase single weights possibly with a faster rate than they can decrease
# value unit of edst is std dev, 3==random walk of 9 steps
self.gamma *= np.exp((edist > 0) * np.tanh(edist / 3) / 2.)**damp
# decrease all weights up to the same level to avoid single extremely small weights
# use a constant factor for pseudo-keeping invariance
self.gamma[self.gamma > 5 * dfit] *= np.exp(-1. / 3)**damp
# self.gamma[idx] *= exp(5*dfit/self.gamma[idx] - 1)**(damp/3)
elif 1 < 3 and (edist > 0).any(): # previous method
# CAVE: min was max in TEC 2009
self.gamma[edist > 0] *= 1.1**min(1, es.sp.weights.mueff / 10. / N)
# max fails on cigtab(N=12,bounds=[0.1,None]):
# self.gamma[edist>0] *= 1.1**max(1, es.sp.weights.mueff/10./N) # this was a bug!?
# self.gamma *= exp((edist>0) * np.tanh(edist))**min(1, es.sp.weights.mueff/10./N)
else: # alternative version, but not better
solutions = es.pop # this has not been checked
r = self.feasible_ratio(solutions) # has to be the averaged over N iterations
self.gamma *= np.exp(np.max([N * [0], 0.3 - r], axis=0))**min(1, es.sp.weights.mueff / 10 / N)
es.more_to_write += list(self.gamma) if self.weights_initialized else N * [1.0]
# ## return penalty
# es.more_to_write = self.gamma if not np.isscalar(self.gamma) else N*[1]
return self # bound penalty values
def _g_pos_max(gvals):
return max(gi if gi > 0 else 0 for gi in gvals)
def _g_pos_sum(gvals):
return sum(gi for gi in gvals if gi > 0)
def _g_pos_squared_sum(gvals):
return sum(gi**2 for gi in gvals if gi > 0)
class ConstrainedSolutionsArchive(object):
"""Biobjective Pareto archive to store some Pareto optimal solutions
for constrained optimization.
The user can define the aggregator for the constraints values which
is by default the sum of the positive parts.
The Pareto archive is maintained in the `archive` attribute and the
Pareto optimal solutions can be recovered in `archive.infos`.
"""
def __init__(self, aggregator=_g_pos_sum):
self.aggregator = aggregator
self.archive = None
self.count = 0
self.maxlen = 10
try:
import moarchiving
except ImportError:
m = ("``import moarchiving`` failed, hence convergence tracking "
"is disabled. \n 'pip install moarchiving' should fix this.")
_warnings.warn(m)
return
self.archive = moarchiving.BiobjectiveNondominatedSortedList()
def update(self, f, g, info=None):
self.count += 1
if self.archive is not None:
gagg = self.aggregator(g)
try:
self.archive.add([f, gagg], info=info)
except TypeError:
self.archive.add([f, gagg]) # previous interface
while len(self.archive) > self.maxlen:
if self.archive[1][1] > 0: # keep at least one infeasible solution
self.archive.remove(self.archive[0])
else:
self.archive.remove(self.archive[-1])
class PopulationEvaluator(object):
"""evaluate and store f- and g-values of a population in attributes F and G.
If the g-function (`constraints`) has an `insert` method, `x`-values
are "inserted" first.
If the `constraints` function has a `true_g` attribute (assigned during
the call) and ``offset_from_true_g is True``, population constraints
values are corrected by an offset to guaranty that ``g >= 0`` if
``true_g > 0``. Named solutions are not offset.
"""
def __init__(self, objective, constraints, insert=True, offset_from_true_g=False):
self.objective = objective
self.constraints = constraints
self.offset_from_true_g = offset_from_true_g
self.insert = insert
def __call__(self, X, **kwargs):
"""`kwargs` are named solutions resulting in
::
self.name['x'] = kwargs[name]
self.name['f'] = self.objective(kwargs[name])
self.name['g'] = self.constraints(kwargs[name])
Store result in attributes `F` and `G`.
"""
self.X = X
self.F = [self.objective(x) for x in X]
if self.insert:
try: # use constraints.insert method if available
for x in X:
self.constraints.insert(x)
for name, x in kwargs.items():
self.constraints.insert(x)
except (AttributeError, TypeError):
pass
try: # use constraints.true_g attribute if available
if not hasattr(self.constraints, 'true_g'):
raise AttributeError # avoid to repeat one evaluation in next line
self.G_all = [(self.constraints(x), self.constraints.true_g) for x in X]
except AttributeError: # "regular" execution path
self.G = [self.constraints(x) for x in X]
else: # process constraints.true_g attribute values
self.G = [G[0] for G in self.G_all]
self.G_true = [G[1] for G in self.G_all]
# offset g values for which g < 0 < true_g, TODO: avoid the double loop?
if self.offset_from_true_g: # process true g-values and offset g-values if necessary
for j in range(len(self.G[0])): # for each constraint
offset = 0
for i in range(len(self.G)): # compute offset from all candidates
if self.G_true[i][j] > 0 and self.G[i][j] + offset < 0:
offset = -self.G[i][j] # use smallest negative value of infeasible solution
assert offset >= 0
for i in range(len(self.G)): # add offset on each infeasible candidate
if self.G_true[i][j] > 0:
self.G[i][j] += offset
assert np.all([np.all(np.asarray(self.G[i])[np.asarray(self.G_true[i]) > 0] >= 0)
for i in range(len(self.G))])
for name, x in kwargs.items():
setattr(self, name, {'x': x,
'f': self.objective(x),
'g': self.constraints(x)})
return self
@property
def feasibility_ratios(self):
"""or bias for equality constraints"""
return np.mean(np.asarray(self.G) <= 0, axis=0)
# return [np.mean(g <= 0) for g in np.asarray(self.G).T]
class DequeCDF(_collections.deque):
"""a queue with (in case) element-wise cdf computation.
The `deque` is here used like a `list` with maximum length functionality,
the (inherited) constructor takes `maxlen` as keyword argument (since Python 2.6).
>>> import cma
>>> d = cma.constraints_handler.DequeCDF(maxlen=22)
>>> for i in range(5):
... d.append([i])
>>> ' '.join(['{0:.2}'.format(x) for x in
... [d.cdf(0, 0), d.cdf(0, 2), d.cdf(0, 2.1), d.cdf(0, 22.1), d.cdf(0, 4, 2)]])
'0.1 0.5 0.6 1.0 0.75'
"""
def cdf(self, i, val=0, len_=None):
"""return ecdf(`val`) from the `i`-th element of the last `len_`
values in self,
in other words, the ratio of values in ``self[-len_:][i]`` to
be smaller than `val`.
"""
j0 = int(min((len_ or len(self), len(self))))
data = np.asarray([self[j][i] for j in range(-j0, 0)])
return np.mean((data < val) + 0.5 * (data == val))
def cdf1(self, val=0, len_=None):
"""return ECDF at `val` in case this is a `deque` of scalars"""
j0 = int(min((len_ or len(self), len(self))))
data = np.asarray([self[j] for j in range(-j0, 0)])
return np.mean((data < val) + 0.5 * (data == val))
class LoggerList(list):
"""list of loggers with plot method"""
def plot(self, moving_window_width=7):
"""versatile plot method, argument list positions may change"""
import matplotlib
from matplotlib import pyplot as plt
# _, axes = plt.gcf().subplots(2, 2) # gcf().subplots return array of subplots, plt.subplots returns array of axes
# axes = list(axes[0]) + list(axes[1])
for i, logger in enumerate(self):
# plt.sca(axes[i])
with _warnings.catch_warnings(): # catch misfiring add_subplot warning
_warnings.filterwarnings('ignore', message='Adding an axes using the same arguments')
plt.subplot(2, 2, i + 1)
logger.plot()
if len(logger.data.shape) > 1 and logger.data.shape[1] > 1:
for j, d in enumerate(logger.data.T):
if j < len(logger.data.T) - 1: # variable number annotation
plt.text(len(d), d[-1], str(j))
if i == 1: # plot rolling average
plt.plot(moving_average(d, min((moving_window_width, len(d)))),
color='r', linewidth=0.15)
if i == 0:
v = np.abs(logger.data)
min_val = max((1e-9, np.min(v[v>0])))
if matplotlib.__version__[:3] < '3.3':
# a terrible interface change that swallows the new/old parameter and breaks code
plt.yscale('symlog', linthreshy=min_val) # see matplotlib.scale.SymmetricalLogScale
else:
plt.yscale('symlog', linthresh=min_val)
def _log_g(s):
return s.g + [0]
def _log_feas_events(s):
return [i + 0.5 * (gi > 0) - 0.25 + 0.2 * np.tanh(gi) for i, gi in enumerate(s.g)
] + [len(s.g) + np.any(np.asarray(s.g) > 0)]
def _log_lam(s):
"""for active constraints, lam is generally positive because Dg and Df are opposed"""
v = np.log10(np.maximum(1e-9, np.abs(s.lam - (0 if s.lam_opt is None else s.lam_opt))))
return np.hstack([v, 0]) # add column to get same colors as _log_feas_events
def _log_mu(s):
v = np.log10(np.maximum(s.mu, 1e-9))
return np.hstack([v, 0]) # add column to get same colors as _log_feas_events
class CountLastSameChanges(object):
"""An array/list of successive same-sign counts.
``.same_changes[i]`` counts how often the same sign was successively
observed during ``update(..., i, change)``. When the sign flips,
``.same_changes[i]`` is reset to 1 or -1.
`init` needs to be called before the class instance can be used.
A valid use case is ``lsc = CountLastSameChanges().init(dimension)``.
TODO: parameters may need to depend on population size?
"""
def __init__(self, parent=None):
self.same_changes = None
self.parent = parent # get easy access for hacks, not in use
" count of the number of changes with the same sign, where"
" the count sign signifies the sign"
self.chi_exponent_threshold = None # waiting and ramp up time
self.chi_exponent_factor = None # steepness of ramp up
" increase chi exponent up to given threshold, can make up for dimension dependent chi"
def init(self, dimension, force=False):
"""do not overwrite user-set values unless `force`"""
if force or self.same_changes is None:
self.same_changes = dimension * [0]
if force or self.chi_exponent_threshold is None: # threshold for number of consecutive changes
# self.chi_exponent_threshold = 5 + self.dimension**0.5 # previous value, probably too small in >- 20-D
# self.chi_exponent_threshold = 5 + self.dimension**0.75 # sweeps in aug-lag4-mu-update.ipynv
self.chi_exponent_threshold = 2 + self.dimension
if force or self.chi_exponent_factor is None: # factor used in shape_exponent
# self.chi_exponent_factor = 3 / dimension**0.5 # previous value
# self.chi_exponent_factor = 1 * dimension**0.25 / self.chi_exponent_threshold # steepness of ramp up
self.chi_exponent_factor = 4 / self.chi_exponent_threshold # steepness of ramp up
return self
@property
def dimension(self):
return len(self.same_changes)
def update(self, i, change):
"""update ``same_changes[i]`` count based on the sign of `change`"""
if change * self.same_changes[i] < 0:
self.same_changes[i] = 0
self.same_changes[i] += change
return self
def shape_exponent(self, i):
"""return value to be added to exponent from change count.
return a positive/negative value (or zero) when the last change was
respectively postive/negative.
The value is zero for the first `chi_exponent_threshold` same changes
and increases linearly for the next `chi_exponent_threshold` same changes
and then stays at the threshold value as long as the change does not
flip sign.
"""
d = self.same_changes[i]
s = np.sign(d)
d -= s * self.chi_exponent_threshold # wait until increase
d *= d * s > 0 # sign must not have changed
return self.chi_exponent_factor * s * min((self.chi_exponent_threshold, s * d)) # bound change
def constraints_info_dict(count, x, f, g, g_al):
"""return dictionary with arg values, mainly there to unify names"""
return {'x': x, # caveat: x is not a copy
'f': f, 'g': g, 'f_al': f + sum(g_al), 'g_al': g_al,
'count': count}
class AugmentedLagrangian(object):
"""Augmented Lagrangian with adaptation of the coefficients
for minimization, implemented after Atamna et al FOGA 2017,
Algorithm 1 and Sect 8.2, https://hal.inria.fr/hal-01455379/document.
`cma.ConstrainedFitnessAL` provides a (more) user-friendly interface to
the `AugmentedLagrangian` class.
Input `dimension` is the search space dimension, boolean `equality`
may be an iterable of length of number of constraints indicating the
type for each constraint.
Below, the objective function value is denoted as ``f = f(x)``, the
constraints values as ``g = g(x) <= 0``, the penalties compute to
``penalties(x) = self(g(x)) = lam g + mu g^2 / 2`` (if g > -lam / mu) for
each element of g, as returned by calling the instance with g as argument.
The penalized "fitness" value ``f + sum(self(g))`` shall be minimized.
lam and mu are the Lagrange multipliers or coefficients.
An additional method, `set_coefficients` allows to initialize the
Lagrange multipliers from data.
A short example (and doctest):
>>> import cma
>>> from cma.constraints_handler import AugmentedLagrangian, PopulationEvaluator
>>> m = 2 # number of constraints
>>> def objective(x):
... return sum(x[m:]**2) + sum((x[:m] - 1)**2) - m
>>> def constraints(x):
... return x[:m]
>>> es = cma.CMAEvolutionStrategy(3 * [1], 1, {
... 'termination_callback': lambda es: sum(es.mean**2) < 1e-8}) #doctest: +ELLIPSIS
(3_w,7)-aCMA-ES...
>>> al = AugmentedLagrangian(es.N) # lam and mu still need to be set
>>> # al.chi_domega = 1.15 # is the new default, which seems to give better results than the original value
>>> # al.lam, al.mu = ... # we could set the initial Lagrange coefficients here
>>> while not es.stop():
... eva = PopulationEvaluator(objective, constraints)(es.ask(), m=es.mean)
... al.set_coefficients(eva.F, eva.G) # set lam and mu, not part of the original algorithm
... al.update(eva.m['f'], eva.m['g'])
... es.tell(eva.X, [f + sum(al(g)) for f, g in zip(eva.F, eva.G)])
>>> if es.result.evaluations > 3100:
... print("evaluations %d !< 3100. 1500 is normal, 2700 happens rarely" % es.result.evaluations)
>>> assert 'callback' in es.stop()
>>> assert len(eva.feasibility_ratios) == m
>>> assert sum(eva.feasibility_ratios < 0) == sum(eva.feasibility_ratios > 1) == 0
Details: the input `dimension` is needed to compute the default change
rate `chi_domega` (if ``chi_domega is None``), to compute initial
coefficients and to compare between h and g to update mu. The default
dependency of `chi_domega` on the dimension seems to be however
suboptimal. Setting ``self.chi_domega = 1.15`` as is the current
default seems to give better results than the original setting.
More testing based on the simpler `ConstrainedFitnessAL` interface:
>>> import cma
>>> for algorithm, evals in zip((0, 1, 2, 3), (2000, 2200, 1500, 1800)):
... alf = cma.ConstrainedFitnessAL(cma.ff.sphere, lambda x: [x[0] + 1], 3,
... find_feasible_first=True)
... _ = alf.al.set_algorithm(algorithm)
... alf.al.logging = False
... x, es = cma.fmin2(alf, 3 * [1], 0.1, {'verbose':-9, 'seed':algorithm}, # verbosity+seed for doctest only
... callback=alf.update)
... assert sum((es.mean - [-1, 0, 0])**2) < 1e-9, (algorithm, es.mean)
... assert es.countevals < evals, (algorithm, es.countevals)
... assert alf.best_feas.f < 10, (algorithm, str(alf.best_feas))
... # print(algorithm, es.countevals, ) #alf.best_feas.__dict__)
"""
def __init__(self, dimension, equality=False):
"""use `set_algorithm()` and ``set_...()`` methods to change defaults"""
self.algorithm = 0 # 1 = original, < 1 is now default
self.dimension = dimension # maybe not desperately needed
self.lam, self.mu = None, None # will become np arrays
self._initialized = np.array(False) # only used for setting, not for update
self._equality = np.array(equality, dtype=bool)
self.k2 = 5
self.dgamma = 5 # damping for lambda change
self.mucdf3_horizon = int(5 + self.dimension**1)
" number of data to compute cdf for mu adaptation"
self.f, self.g = 2 * [None] # store previous values
self.count = 0 # number of actual updates after any mu > 0 was set
self.count_g_in_penalized_domain = 0 # will be an array
" number of times g induced a penality in __call__ since last update"
self.count_mu_last_same_changes = CountLastSameChanges()
" number of same changes of mu, -3 means it decreased in all last three iterations"
self.counts = _collections.defaultdict(int)
self.g_history = DequeCDF(maxlen=self.dimension + 20)
" g-values from calling update"
self.g_all = _collections.defaultdict(_functools.partial(DequeCDF, maxlen=2 * self.dimension + 20))
" all g-values from calling the class, recorded but not in use"
self.count_calls = 0
self.lam_opt = None # only for display in logger
self.logging = 1
self._set_parameters()
self._init_()
def set_atamna2017(self):
"""Atamna et al 2017 parameter settings"""
self.k1 = 3
self.chi_domega = 2**(1. / 5 / self.dimension) # factor for mu change, 5 == domega
return self
def set_dufosse2020(self):
"""Dufosse & Hansen 2020 parameter settings"""
self.k1 = 10
self.chi_domega = 2**(1. / self.dimension**0.5)
return self
def _set_parameters(self):
"""set parameters based on the value of `self.algorithm`"""
if self.algorithm <= 0: # default is 3
self.set_atamna2017()
elif self.algorithm in (1, 3, 4):
self.set_atamna2017()
elif self.algorithm == 2:
self.set_dufosse2020()
else:
raise ValueError("Algorithm id {0} is not recognized".format(self.algorithm))
def set_algorithm(self, algorithm=None):
"""if algorithm not `None`, set it and return self,
otherwise return current algorithm value which should be an integer.
Values < 1 are the (new) default, 1 == Atamna et al 2017, 2 == modified 1.
"""
if algorithm is None: # like get_algorithm
return self.algorithm
elif algorithm != self.algorithm:
# modify parameters, if necessary
self.algorithm = algorithm
self._set_parameters()
return self
def _init_(self):
"""allow to reset the logger with a single call"""
self.loggers = LoggerList()
if self.logging > 0:
self.loggers.append(_Logger(self, callables=[_log_g],
labels=['constraint values'],
name='outauglagg'))
self.loggers.append(_Logger(self, callables=[_log_feas_events],
labels=['sign(gi) / 2 + i', 'overall feasibility'],
name='outauglagfeas'))
self.loggers.append(_Logger(self, callables=[_log_lam],
labels=['lg(abs(lambda))' if self.lam_opt is None
else 'lg(abs(lambda-lam_opt))'],
name='outauglaglam'))
self.loggers.append(_Logger(self, callables=[_log_mu],
labels=['lg(mu)'], name='outauglagmu'))
self.logger_mu_conditions = None
if self.algorithm in (1, 2):
self.logger_mu_conditions = _Logger("mu_conditions", labels=[
r'$\mu$ increases',
r'$\mu g^2 < %.0f |\Delta h| / n$' % self.k1,
r'$|\Delta g| < |g| / %.0f$' % self.k2])
@property
def m(self):
"""number of constraints, raise `TypeError` if not set yet"""
return len(self.lam)
@property
def is_initialized(self):
try:
return all(self._initialized)
except TypeError:
return bool(self._initialized)
@property
def count_initialized(self):
"""number of constraints with initialized coefficients"""
return 0 if self.mu is None else sum(self._initialized * (self.mu > 0))
@property
def feasibility_ratios(self):
"""or bias for equality constraints, versatile interface may change"""
try: