Skip to content

Commit 8a4970d

Browse files
author
andrewtliew
committed
- docstrings and tidying.
1 parent 04be792 commit 8a4970d

File tree

8 files changed

+67
-47
lines changed

8 files changed

+67
-47
lines changed

src/compas/numerical/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
descent
3939
devo_numpy
4040
ga
41-
lma
42-
mma
4341
moga
4442
4543

src/compas/numerical/solvers/descent.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from __future__ import print_function
21
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
34

45
import sys
56

@@ -25,10 +26,10 @@
2526
e = sqrt(eps)
2627

2728

28-
__author__ = ['Andrew Liew <[email protected]>']
29-
__copyright__ = 'Copyright 2017, BLOCK Research Group - ETH Zurich'
30-
__license__ = 'MIT License'
31-
__email__ = '[email protected]'
29+
__author__ = ['Andrew Liew <[email protected]>']
30+
__copyright__ = 'Copyright 2017, BLOCK Research Group - ETH Zurich'
31+
__license__ = 'MIT License'
32+
__email__ = '[email protected]'
3233

3334

3435
__all__ = [
@@ -39,7 +40,8 @@
3940
def descent(x0, fn, iterations=1000, gtol=10**(-6), bounds=None, limit=0, args=()):
4041
"""A gradient descent optimisation solver.
4142
42-
Parameters:
43+
Parameters
44+
----------
4345
x0 (array, list): n x 1 starting guess of x.
4446
fn (obj): The objective function to minimise.
4547
iterations (int): Maximum number of iterations.
@@ -48,21 +50,24 @@ def descent(x0, fn, iterations=1000, gtol=10**(-6), bounds=None, limit=0, args=(
4850
limit (float): Value of the objective function for which to terminate optimisation.
4951
args (tuple): Additional parameters needed for fn.
5052
51-
Returns:
53+
Returns
54+
-------
5255
float: Final value of the objective function.
5356
array: Values of x at the found local minimum.
5457
"""
5558
r = 0.5
5659
c = 0.0001
5760
n = len(x0)
5861
x0 = reshape(array(x0), (n, 1))
62+
5963
if bounds:
6064
bounds = array(bounds)
6165
lb = bounds[:, 0][:, newaxis]
6266
ub = bounds[:, 1][:, newaxis]
6367
else:
6468
lb = ones((n, 1)) * -10**20
6569
ub = ones((n, 1)) * +10**20
70+
6671
zn = zeros((n, 1))
6772
g = zeros((n, 1))
6873
v = eye(n) * e
@@ -74,26 +79,32 @@ def phi(x, mu, *args):
7479
i = 0
7580
mu = 1
7681
while i < iterations:
82+
7783
p0 = phi(x0, mu, *args)
7884
for j in range(n):
7985
vj = v[:, j][:, newaxis]
8086
g[j, 0] = (phi(x0 + vj, mu, *args) - p0) / e
8187
D = sum(-g * g)
88+
8289
a = 1
8390
x1 = x0 - a * g
8491
while phi(x1, mu, *args) > p0 + c * a * D:
8592
a *= r
8693
x1 = x0 - a * g
8794
x0 -= a * g
95+
8896
mu *= 10
8997
res = mean(abs(g))
9098
i += 1
9199
f1 = phi(x0, mu, *args)
92-
print('Iteration: {0} fopt: {1:.3g} gres: {2:.3g} step: {3}'.format(i, f1, res, a))
100+
93101
if f1 < limit:
94102
break
95103
if res < gtol:
96104
break
105+
106+
print('Iteration: {0} fopt: {1:.3g} gres: {2:.3g} step: {3}'.format(i, f1, res, a))
107+
97108
return f1, x0
98109

99110

src/compas/numerical/solvers/devo_numpy.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from __future__ import print_function
21
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
34

45
import sys
56

@@ -28,10 +29,10 @@
2829
import json
2930

3031

31-
__author__ = ['Andrew Liew <[email protected]>']
32-
__copyright__ = 'Copyright 2017, BLOCK Research Group - ETH Zurich'
33-
__license__ = 'MIT License'
34-
__email__ = '[email protected]'
32+
__author__ = ['Andrew Liew <[email protected]>']
33+
__copyright__ = 'Copyright 2017, BLOCK Research Group - ETH Zurich'
34+
__license__ = 'MIT License'
35+
__email__ = '[email protected]'
3536

3637

3738
__all__ = [
@@ -43,10 +44,8 @@ def devo_numpy(fn, bounds, population, generations, limit=0, results=None, vecto
4344
args=(), callback=None, **kwargs):
4445
"""Call the Differential Evolution solver.
4546
46-
Note:
47-
fn must return vectorised output for input (k, population) if vectored is True.
48-
49-
Parameters:
47+
Parameters
48+
----------
5049
fn (obj): The function to evaluate and minimise.
5150
bounds (list): Lower and upper bounds for each DoF [[lb, ub], ...].
5251
population (int): Number of agents in the population.
@@ -60,11 +59,15 @@ def devo_numpy(fn, bounds, population, generations, limit=0, results=None, vecto
6059
args (seq): Sequence of optional arguments to pass to fn.
6160
callback (obj): Callback function for each generation.
6261
63-
Returns:
62+
Returns
63+
-------
6464
float: Optimal value of objective function.
6565
list: Values that give the optimum (minimised) function.
66-
"""
6766
67+
Note
68+
----
69+
fn must return vectorised output for input (k, population) if vectored is True.
70+
"""
6871
tic = time()
6972

7073
# Heading

src/compas/numerical/solvers/ga.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
from __future__ import print_function
21
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
34

45
import re
56
import random
67
import json
78
import copy
89

910

10-
__author__ = ['Tomas Mendez Echenagucia <[email protected]>']
11-
__copyright__ = 'Copyright 2017, Block Research Group - ETH Zurich'
12-
__license__ = 'MIT License'
13-
__email__ = '[email protected]'
11+
__author__ = ['Tomas Mendez Echenagucia <[email protected]>']
12+
__copyright__ = 'Copyright 2017, Block Research Group - ETH Zurich'
13+
__license__ = 'MIT License'
14+
__email__ = '[email protected]'
1415

1516

1617
__all__ = [
@@ -60,7 +61,6 @@ def ga(fit_function,
6061
output_path=None,
6162
input_path=None):
6263

63-
ga_ = GA()
6464
"""Genetic Algorithm optimisation (Holland 1975).
6565
6666
Parameters
@@ -181,6 +181,7 @@ def foo(X):
181181
================================================================================
182182
183183
"""
184+
ga_ = GA()
184185

185186
ga_.fit_name = fit_name or fit_function.__name__
186187
ga_.fit_type = fit_type
@@ -448,7 +449,6 @@ def scale_population(self, decoded_pop):
448449
scaled_pop: list
449450
The scaled ppopulation list.
450451
"""
451-
452452
scaled_pop = [[[]] * self.num_var for i in range(self.num_pop)]
453453
for j in range(self.num_pop):
454454
for i in range(self.num_var):
@@ -496,7 +496,6 @@ def select_elite_pop(self, pop, num_elite=None):
496496
elite_pop: dict
497497
The elite population dictionary.
498498
"""
499-
500499
if self.fit_type == 'min':
501500
sorted_ind = self.get_sorting_indices(self.current_pop['fit_value'])
502501
elif self.fit_type == 'max':
@@ -565,7 +564,6 @@ def simple_crossover(self):
565564
combined with individuals in ``GA.mating_pool_b`` using a single, randomly selected
566565
crossover point.
567566
"""
568-
569567
self.current_pop = {'binary': [], 'decoded': [], 'scaled': [], 'fit_value': []}
570568
self.current_pop['binary'] = [[[]] * self.num_var for i in range(self.num_pop)]
571569
for j in range((self.num_pop - self.num_elite) / 2):
@@ -603,7 +601,7 @@ def code_decoded(self, decoded_pop):
603601
Parameters
604602
----------
605603
decoded_pop: dict
606-
The decoded population dictionary to be coded
604+
The decoded population dictionary to be coded
607605
608606
Returns
609607
-------
@@ -844,6 +842,10 @@ def get_best_individual_index(self):
844842
self.best_individual_index = indices[0]
845843

846844

845+
# ==============================================================================
846+
# Main
847+
# ==============================================================================
848+
847849
if __name__ == '__main__':
848850

849851
import os

src/compas/numerical/solvers/lma.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
""""""
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
25

36
__author__ = ['Tom Van Mele', ]
4-
__copyright__ = 'Copyright 2016 - Block Research Group, ETH Zurich'
7+
__copyright__ = 'Copyright 2017 - Block Research Group, ETH Zurich'
58
__license__ = 'MIT License'
69
__email__ = '[email protected]'
710

src/compas/numerical/solvers/mma.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
16
__author__ = ['Tom Van Mele', ]
2-
__copyright__ = 'Copyright 2016 - Block Research Group, ETH Zurich'
7+
__copyright__ = 'Copyright 2017 - Block Research Group, ETH Zurich'
38
__license__ = 'MIT License'
49
__email__ = '[email protected]'
510

src/compas/numerical/solvers/moga.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
from __future__ import print_function
21
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
34

45
import re
56
import random
67
import json
78

89

9-
__author__ = ['Tomas Mendez Echenagucia <[email protected]>']
10-
__copyright__ = 'Copyright 2016, Block Research Group - ETH Zurich'
11-
__license__ = 'MIT License'
12-
__email__ = '[email protected]'
10+
__author__ = ['Tomas Mendez Echenagucia <[email protected]>']
11+
__copyright__ = 'Copyright 2017, Block Research Group - ETH Zurich'
12+
__license__ = 'MIT License'
13+
__email__ = '[email protected]'
1314

1415

1516
__all__ = [
@@ -54,11 +55,10 @@ def moga(fit_functions,
5455
fkwargs=None,
5556
output_path=None):
5657

57-
"""Genetic Algorithm optimisation [deb2001]_.
58+
"""Multi-objective Genetic Algorithm optimisation [deb2001]_.
5859
5960
Parameters
6061
----------
61-
6262
fit_functions : list
6363
List of functions to be used by the :class'MOGA' to determine the fitness values.
6464
The function must have as a first argument a list of variables that determine the
@@ -471,7 +471,6 @@ def decode_binary_pop(self, bin_pop):
471471
return decoded_pop
472472

473473
def scale_population(self, decoded_pop):
474-
475474
"""Scales the decoded population, variable values are scaled according to each
476475
of their bounds contained in ``GA.boundaries``.
477476
@@ -485,7 +484,6 @@ def scale_population(self, decoded_pop):
485484
scaled_pop: list
486485
The scaled ppopulation list.
487486
"""
488-
489487
scaled_pop = [[[]] * self.num_var for i in range(self.num_pop)]
490488
for j in range(self.num_pop):
491489
for i in range(self.num_var):
@@ -802,7 +800,6 @@ def get_pop_from_pf_file(self):
802800
lines = pf_file.readlines()
803801
pf_file.close()
804802

805-
806803
file_pop['scaled'] = [[[]] * self.num_var for i in range(self.num_pop)]
807804
file_pop['scaled'] = [[[]] * self.num_fit_func for i in range(self.num_pop)]
808805
for i in range(self.num_pop):
@@ -923,7 +920,6 @@ def make_pop_pf_dict(self):
923920
-------
924921
pf_dict: dict
925922
The dictionary containing the pareto front level of all individuals.
926-
927923
"""
928924
pf_dict = {}
929925
for j in range(len(self.pareto_front_indices) - 1):

src/compas/utilities/animation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from __future__ import division
44

55
import os
6-
import imageio
7-
6+
try:
7+
import imageio
8+
except:
9+
pass
810

911
__author__ = ['Matthias Rippmann', ]
1012
__copyright__ = 'Copyright 2016 - Block Research Group, ETH Zurich'

0 commit comments

Comments
 (0)