/timm's /charming /python /tricks
Download
models.py.
Read more on How to be Charming (in Python).
01: import sys 02: sys.dont_write_bytecode=True 03: from model import * 04: 05: class Schaffer(Model): 06: "A very simple model." 07: def has(i): 08: return [Num("$x1",lo=-10,hi=10), 09: Num("<f1"), 10: Num("<f2")] 11: def score(i,l): 12: x = i.get(l,"x1") 13: i.put(l, "f1", x**2) 14: i.put(l, "f2", (x-2)**2) 15: 16: class Kursawe(Model): 17: "A model with 8 decisions and 2 objectives." 18: def has(i): 19: return [Num("$x1",lo = -5,hi = 5), 20: Num("$x2",lo = -5,hi = 5), 21: Num("$x3",lo = -5,hi = 5), 22: Num("$x4",lo = -5,hi = 5), 23: Num("$x5",lo = -5,hi = 5), 24: Num("$x6",lo = -5,hi = 5), 25: Num("$x7",lo = -5,hi = 5), 26: Num("$x8",lo = -5,hi = 5), 27: Num("<f1"), 28: Num("<f2")] 29: def score(i,l): 30: f1 = f2 = 0.0 31: for n,one in enumerate(l[:7]): 32: two = l[n+1] 33: f1 += -10*e**(-0.2*(one**2 + two**2)**0.5) 34: for one in l[:8]: 35: f2 += abs(one)**0.8 + 5*math.sin(one**3) 36: i.put(l,"f1",f1) 37: i.put(l,"f2",f2) 38: 39: def run(what=Schaffer,repeats=10): 40: """Note that the high-level loop is generic 41: to all models.""" 42: x=what() 43: for _ in range(repeats): 44: print x.any() 45: for col in x.t.columns: 46: print col 47: 48: def schafferd(): run(Schaffer) 49: def kursawed() : run(Kursawe) 50:
This file is part of Timm's charming Python tricks.
© 2014, Tim Menzies:
tim.menzies@gmail.com,
http://menzies.us.
Timm's charming Python tricks are free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Timm's charming Python tricks are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser Public License along with Foobar. If not, see http://www.gnu.org/licenses.