| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # Problem definition: |
|---|
| 4 | # A-R Hedar and M Fukushima, "Derivative-Free Filter Simulated Annealing |
|---|
| 5 | # Method for Constrained Continuous Global Optimization", Journal of |
|---|
| 6 | # Global Optimization, 35(4), 521-549 (2006). |
|---|
| 7 | # |
|---|
| 8 | # Original Matlab code written by A. Hedar (Nov. 23, 2005) |
|---|
| 9 | # http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/go.htm |
|---|
| 10 | # and ported to Python by Mike McKerns (December 2014) |
|---|
| 11 | # |
|---|
| 12 | # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) |
|---|
| 13 | # Copyright (c) 1997-2016 California Institute of Technology. |
|---|
| 14 | # License: 3-clause BSD. The full license text is available at: |
|---|
| 15 | # - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE |
|---|
| 16 | |
|---|
| 17 | def objective(x): |
|---|
| 18 | x0,x1,x2,x3,x4,x5,x6,x7 = x |
|---|
| 19 | return x0 + x1 + x2 |
|---|
| 20 | |
|---|
| 21 | bounds = [(100,10000)] + [(1000,10000)]*2 + [(10,1000)]*5 |
|---|
| 22 | # with penalty='penalty' applied, solution is: |
|---|
| 23 | xs = [579.3167, 1359.943, 5110.071, 182.0174, \ |
|---|
| 24 | 295.5985, 217.9799, 286.4162,395.5979] |
|---|
| 25 | ys = 7049.3307 |
|---|
| 26 | |
|---|
| 27 | from mystic.symbolic import generate_constraint, generate_solvers, simplify |
|---|
| 28 | from mystic.symbolic import generate_penalty, generate_conditions |
|---|
| 29 | |
|---|
| 30 | equations = """ |
|---|
| 31 | -1.0 + 0.0025*(x3 + x5) <= 0.0 |
|---|
| 32 | -1.0 + 0.0025*(-x3 + x4 + x6) <= 0.0 |
|---|
| 33 | -1.0 + 0.01*(-x4 + x7) <= 0.0 |
|---|
| 34 | 100.0*x0 - x0*x5 + 833.33252*x3 - 83333.333 <= 0.0 |
|---|
| 35 | x1*x3 - x1*x6 - 1250.0*x3 + 1250.0*x4 <= 0.0 |
|---|
| 36 | x2*x4 - x2*x7 - 2500.0*x4 + 1250000.0 <= 0.0 |
|---|
| 37 | """ |
|---|
| 38 | cf = generate_constraint(generate_solvers(simplify(equations))) |
|---|
| 39 | pf = generate_penalty(generate_conditions(equations), k=1e12) |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if __name__ == '__main__': |
|---|
| 44 | |
|---|
| 45 | from mystic.solvers import diffev2 |
|---|
| 46 | from mystic.math import almostEqual |
|---|
| 47 | |
|---|
| 48 | result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=80, gtol=500, disp=False, full_output=True) |
|---|
| 49 | |
|---|
| 50 | assert almostEqual(result[0], xs, rel=1e-2) |
|---|
| 51 | assert almostEqual(result[1], ys, rel=1e-2) |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | # EOF |
|---|