Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit a3890c9

Browse files
author
Tanguy Damart
committed
Syntax fixes
1 parent caa28dc commit a3890c9

File tree

9 files changed

+333
-190
lines changed

9 files changed

+333
-190
lines changed

bluepyopt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import bluepyopt.stoppingCriteria
3434
import bluepyopt.deapext.optimisations
3535
import bluepyopt.deapext.optimisationsCMA
36+
import bluepyopt.deapext.hype
3637

3738
# Add some backward compatibility for the time when DEAPoptimisation not in
3839
# deapext yet

bluepyopt/deapext/CMA_MO.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Multi Objective CMA-es class"""
22

33
"""
4-
Copyright (c) 2016, EPFL/Blue Brain Project
4+
Copyright (c) 2016-2020, EPFL/Blue Brain Project
55
66
This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt>
77
@@ -34,13 +34,13 @@
3434
from . import utils
3535
from . import hype
3636

37-
logger = logging.getLogger('__main__')
37+
logger = logging.getLogger("__main__")
3838

3939

4040
def get_hyped(pop):
4141
# Cap the obj at 250
4242
points = numpy.array([ind.fitness.values for ind in pop])
43-
points[points > 250.] = 250.
43+
points[points > 250.0] = 250.0
4444
lbounds = numpy.min(points, axis=0)
4545
ubounds = numpy.max(points, axis=0)
4646

@@ -55,28 +55,29 @@ def get_hyped(pop):
5555

5656
# Rescale the objective space
5757
points = (points - lbounds) / numpy.max(ubounds.flatten())
58-
ubounds = numpy.max(points, axis=0) + 2.
58+
ubounds = numpy.max(points, axis=0) + 2.0
5959

60-
hv = hype.hypeIndicatorSampled(points=points,
61-
bounds=ubounds,
62-
k=5,
63-
nrOfSamples=200000)
60+
hv = hype.hypeIndicatorSampled(
61+
points=points, bounds=ubounds, k=5, nrOfSamples=200000
62+
)
6463
return hv
6564

6665

6766
class CMA_MO(cma.StrategyMultiObjective):
6867
"""Multiple objective covariance matrix adaption"""
6968

70-
def __init__(self,
71-
centroids,
72-
offspring_size,
73-
sigma,
74-
max_ngen,
75-
IndCreator,
76-
RandIndCreator,
77-
weight_hv=0.5,
78-
map_function=None,
79-
use_scoop=False):
69+
def __init__(
70+
self,
71+
centroids,
72+
offspring_size,
73+
sigma,
74+
max_ngen,
75+
IndCreator,
76+
RandIndCreator,
77+
weight_hv=0.5,
78+
map_function=None,
79+
use_scoop=False,
80+
):
8081
"""Constructor
8182
8283
Args:
@@ -85,8 +86,8 @@ def __init__(self,
8586
sigma (float): initial standard deviation of the distribution
8687
max_ngen (int): total number of generation to run
8788
IndCreator (fcn): function returning an individual of the pop
88-
weight_hv (float): between 0 and 1. Weight given to the
89-
hypervolume contribution when computing the score of an
89+
weight_hv (float): between 0 and 1. Weight given to the
90+
hypervolume contribution when computing the score of an
9091
individual in MO-CMA. The weight of the fitness contribution
9192
is computed as 1 - weight_hv.
9293
"""
@@ -101,18 +102,19 @@ def __init__(self,
101102
else:
102103
if len(centroids) != lambda_:
103104
from itertools import cycle
105+
104106
generator = cycle(centroids)
105107
starters = [next(generator) for i in range(lambda_)]
106108
else:
107109
starters = centroids
108110

109-
cma.StrategyMultiObjective.__init__(self, starters, sigma,
110-
mu=int(lambda_ * 0.5),
111-
lambda_=lambda_)
111+
cma.StrategyMultiObjective.__init__(
112+
self, starters, sigma, mu=int(lambda_ * 0.5), lambda_=lambda_
113+
)
112114

113115
self.population = []
114116
self.problem_size = len(starters[0])
115-
117+
116118
self.weight_hv = weight_hv
117119

118120
self.map_function = map_function
@@ -126,16 +128,19 @@ def __init__(self,
126128
if self.use_scoop:
127129
if self.map_function:
128130
raise Exception(
129-
'Impossible to use scoop is providing self defined map '
130-
'function: %s' % self.map_function)
131+
"Impossible to use scoop is providing self defined map "
132+
"function: %s" % self.map_function
133+
)
131134
from scoop import futures
135+
132136
self.map_function = futures.map
133137

134138
# Set termination conditions
135139
self.active = True
136140
if max_ngen <= 0:
137141
max_ngen = 100 + 50 * (self.problem_size + 3) ** 2 / numpy.sqrt(
138-
self.lambda_)
142+
self.lambda_
143+
)
139144

140145
self.stopping_conditions = [MaxNGen(max_ngen)]
141146

@@ -146,29 +151,30 @@ def _select(self, candidates):
146151
absolute fitness and hyper-volume contribution.
147152
"""
148153

149-
if self.weight_hv == 0.:
154+
if self.weight_hv == 0.0:
150155
fit = [numpy.sum(ind.fitness.values) for ind in candidates]
151156
idx_fit = list(numpy.argsort(fit))
152157
idx_scores = idx_fit[:]
153158

154-
elif self.weight_hv == 1.:
159+
elif self.weight_hv == 1.0:
155160
hv = get_hyped(candidates)
156-
idx_hv = list(numpy.argsort(hv))[::-1]
157-
idx_scores = idx_hv[:]
161+
idx_hv = list(numpy.argsort(hv))[::-1]
162+
idx_scores = idx_hv[:]
158163

159164
else:
160165
hv = get_hyped(candidates)
161-
idx_hv = list(numpy.argsort(hv))[::-1]
166+
idx_hv = list(numpy.argsort(hv))[::-1]
162167
fit = [numpy.sum(ind.fitness.values) for ind in candidates]
163168
idx_fit = list(numpy.argsort(fit))
164169
scores = []
165170
for i in range(len(candidates)):
166-
score = (self.weight_hv * idx_hv.index(i)) + \
167-
((1.-self.weight_hv) * idx_fit.index(i))
171+
score = (self.weight_hv * idx_hv.index(i)) + (
172+
(1.0 - self.weight_hv) * idx_fit.index(i)
173+
)
168174
scores.append(score)
169175
idx_scores = list(numpy.argsort(scores))
170176

171-
chosen = [candidates[i] for i in idx_scores[:self.mu]]
177+
chosen = [candidates[i] for i in idx_scores[: self.mu]]
172178
not_chosen = [candidates[i] for i in idx_scores[self.mu:]]
173179
return chosen, not_chosen
174180

@@ -213,6 +219,8 @@ def check_termination(self, gen):
213219
[c.check(stopping_params) for c in self.stopping_conditions]
214220
for c in self.stopping_conditions:
215221
if c.criteria_met:
216-
logger.info('CMA stopped because of termination criteria: ' +
217-
' '.join(c.name))
222+
logger.info(
223+
"CMA stopped because of termination criteria: " +
224+
"" + " ".join(c.name)
225+
)
218226
self.active = False

bluepyopt/deapext/CMA_SO.py

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Single Objective CMA-es class"""
22

33
"""
4-
Copyright (c) 2016, EPFL/Blue Brain Project
4+
Copyright (c) 2016-2020, EPFL/Blue Brain Project
55
66
This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt>
77
@@ -29,26 +29,37 @@
2929
from deap import base
3030
from deap import cma
3131

32-
from .stoppingCriteria import MaxNGen, Stagnation, TolHistFun, EqualFunVals, \
33-
NoEffectAxis, TolUpSigma, TolX, ConditionCov, NoEffectCoor
32+
from .stoppingCriteria import (
33+
MaxNGen,
34+
Stagnation,
35+
TolHistFun,
36+
EqualFunVals,
37+
NoEffectAxis,
38+
TolUpSigma,
39+
TolX,
40+
ConditionCov,
41+
NoEffectCoor,
42+
)
3443

3544
from . import utils
3645

37-
logger = logging.getLogger('__main__')
46+
logger = logging.getLogger("__main__")
3847

3948

4049
class CMA_SO(cma.Strategy):
4150
"""Single objective covariance matrix adaption"""
4251

43-
def __init__(self,
44-
centroids,
45-
offspring_size,
46-
sigma,
47-
max_ngen,
48-
IndCreator,
49-
RandIndCreator,
50-
map_function=None,
51-
use_scoop=False):
52+
def __init__(
53+
self,
54+
centroids,
55+
offspring_size,
56+
sigma,
57+
max_ngen,
58+
IndCreator,
59+
RandIndCreator,
60+
map_function=None,
61+
use_scoop=False,
62+
):
5263
"""Constructor
5364
5465
Args:
@@ -86,7 +97,8 @@ def __init__(self,
8697
self.active = True
8798
if max_ngen <= 0:
8899
max_ngen = 100 + 50 * (self.problem_size + 3) ** 2 / numpy.sqrt(
89-
self.lambda_)
100+
self.lambda_
101+
)
90102

91103
self.stopping_conditions = [
92104
MaxNGen(max_ngen),
@@ -97,7 +109,7 @@ def __init__(self,
97109
TolUpSigma(float(self.sigma)),
98110
TolX(),
99111
ConditionCov(),
100-
NoEffectCoor()
112+
NoEffectCoor(),
101113
]
102114

103115
def update(self, population):
@@ -113,29 +125,47 @@ def update(self, population):
113125
c_diff = self.centroid - old_centroid
114126

115127
# Cumulation : update evolution path
116-
self.ps = (1 - self.cs) * self.ps + sqrt(self.cs * (2 - self.cs) *
117-
self.mueff) / self.sigma * numpy.dot(self.B, (1. /
118-
self.diagD) * numpy.dot(self.B.T, c_diff)) # noqa
119-
120-
hsig = float((numpy.linalg.norm(self.ps) / sqrt(1. - (1. - self.cs)
121-
** (2. * (self.update_count + 1.))) / self.chiN <
122-
(1.4 + 2. / (self.dim + 1.)))) # noqa
128+
self.ps = (1 - self.cs) * self.ps + sqrt(
129+
self.cs * (2 - self.cs) * self.mueff
130+
) / self.sigma * numpy.dot(
131+
self.B, (1.0 / self.diagD) * numpy.dot(self.B.T, c_diff)
132+
) # noqa
133+
134+
hsig = float(
135+
(
136+
numpy.linalg.norm(self.ps)
137+
/ sqrt(1.0 - (1.0 - self.cs) **
138+
(2.0 * (self.update_count + 1.0)))
139+
/ self.chiN
140+
< (1.4 + 2.0 / (self.dim + 1.0))
141+
)
142+
)
123143

124144
self.update_count += 1
125145

126-
self.pc = (1 - self.cc) * self.pc + hsig * sqrt(self.cc * (2 - self.cc)
127-
* self.mueff) / self.sigma * c_diff # noqa
146+
self.pc = (1 - self.cc) * self.pc + hsig * sqrt(
147+
self.cc * (2 - self.cc) * self.mueff
148+
) / self.sigma * c_diff
128149

129150
# Update covariance matrix
130151
artmp = population[0:self.mu] - old_centroid
131-
self.C = (1 - self.ccov1 - self.ccovmu + (1 - hsig) *
132-
self.ccov1 * self.cc * (2 - self.cc)) * self.C \
133-
+ self.ccov1 * numpy.outer(self.pc, self.pc) \
134-
+ self.ccovmu * numpy.dot((self.weights * artmp.T), artmp) \
135-
/ self.sigma ** 2 # noqa
136-
137-
self.sigma *= numpy.exp((numpy.linalg.norm(self.ps) / self.chiN - 1.) *
138-
self.cs / self.damps) # noqa
152+
self.C = (
153+
(
154+
1
155+
- self.ccov1
156+
- self.ccovmu
157+
+ (1 - hsig) * self.ccov1 * self.cc * (2 - self.cc)
158+
)
159+
* self.C
160+
+ self.ccov1 * numpy.outer(self.pc, self.pc)
161+
+ self.ccovmu * numpy.dot((self.weights * artmp.T), artmp)
162+
/ self.sigma ** 2
163+
)
164+
165+
self.sigma *= numpy.exp(
166+
(numpy.linalg.norm(self.ps) / self.chiN - 1.0) * self.cs
167+
/ self.damps
168+
)
139169

140170
self.diagD, self.B = numpy.linalg.eigh(self.C)
141171
indx = numpy.argsort(self.diagD)
@@ -182,6 +212,8 @@ def check_termination(self, gen):
182212
[c.check(stopping_params) for c in self.stopping_conditions]
183213
for c in self.stopping_conditions:
184214
if c.criteria_met:
185-
logger.info('CMA stopped because of termination criteria: ' +
186-
' '.join(c.name))
215+
logger.info(
216+
"CMA stopped because of termination criteria: " +
217+
"" + " ".join(c.name)
218+
)
187219
self.active = False

0 commit comments

Comments
 (0)