-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (70 loc) · 2.14 KB
/
main.py
File metadata and controls
90 lines (70 loc) · 2.14 KB
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
from deap import tools, creator, base, algorithms
import numpy
import random
import matplotlib.pyplot as plt
%matplotlib inline
points = []
mean = 0
def test_regress(coef):
SSres = 0
SStot = 0
for point in points:
x = point[0]
yi = coef[0]*x**2 + coef[1]*x + coef[2]
SSres = SSres + (point[1] - yi)**2
SStot = SStot + (point[1] - mean)**2
err = SSres/SStot
return (err,)
#uncertain here
def mutfloat(individual, indpb):
for i in individual:
if random.uniform(0,1) <= indpb:
i = i + random.uniform(-5,5)
return (individual,)
def model ():
creator.create("bestFit", base.Fitness, weights = (-1.0,))
creator.create("Individual",list,fitness=creator.bestFit)
toolbox = base.Toolbox()
toolbox.register("Coeff",random.uniform,-30,30)
toolbox.register("individual",tools.initRepeat,creator.Individual,toolbox.Coeff, 3)
toolbox.register("population",tools.initRepeat,list,toolbox.individual, n = 50)
toolbox.register("evaluate",test_regress)
toolbox.register("mate",tools.cxTwoPoint)
toolbox.register("mutate",mutfloat,indpb = 0.05)
toolbox.register("select", tools.selTournament,tournsize =3)
toolbox.register("map",map)
cxprob, mutprob, ngens = 0.5,0.2,300
pop = toolbox.population()
ind = toolbox.individual()
hof = tools.HallOfFame(1)
pop = algorithms.eaMuPlusLambda(pop, toolbox,10,50, cxprob, mutprob, ngens,halloffame = hof, verbose = 0)
return hof[0]
random.seed(10)
xi,yi = [],[] #empty lists for plotting
#getting data
f = open('data.txt', 'r')
points = []
for line in f:
point = map(float,line.split())
plt.plot(point[0],point[1],"b"+"o")
points.append(point)
f.close()
#end setting up, begin main
for point in points:
mean = mean + point[1]
mean = mean/len(points)
c = model() #GA
#c = (X^t * X)^-1 * X^t * y
print c
#plotting
for point in points:
xi.append(point[0])
yi.append(c[0] * point[0]**2 + c[1]*point[0] + c[2])
plt.plot(xi, yi, "-")
plt.show()
#theory
#Get new quadratic
#find new min
#plug new min into rosenbrock
#take output and place into points
#repeat from recalc mean