-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackBox.py
165 lines (144 loc) · 6.31 KB
/
blackBox.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from PyGMO.problem import base
import rebound
import numpy as np
import math
from simulator import SpaceSim
MAX_DV= 0.00045512099
filename = "blackBoxLow.bin"
class earthToMars(base):
def __init__(self, dim=12*3,c_dim=2):
#USAGE: super(derived_class_name,self).__init__(dim, i_dim, n_obj, c_dim, c_ineq_dim, c_tol)
#dim: Total dimension of the decision vector
#i_dim: dimension of the integer part of decision vector (the integer part is placed at the end of the decision vector). Defaults to 0
#n_obj: number of objectives. Defaults to 1
#c_dim: total dimension of the constraint vector. dDefaults to 0
#c_ineq_dim: dimension of the inequality part of the constraint vector (inequality const. are placed at the end of the decision vector). Defaults to 0
#c_tol: constraint tolerance. When comparing individuals, this tolerance is used to decide whether a constraint is considered satisfied.
super(earthToMars,self).__init__(dim, 0, 1, c_dim, 0, 1e-5)
self.set_bounds(-MAX_DV, MAX_DV)
self.c_dim=c_dim
self.dim=dim
def _objfun_impl(self, x):
sim=SpaceSim()
dimention =self.dimension/3
decisions=[]
de = [0,0,0]
for i in range(dimention):
de[0]=x[i*3]
de[1]=x[i*3+1]
de[2]=x[i*3+2]
decisions.append(de)
f = sim.simulate(decisions)
return (-f.particles[3].m,)
def _compute_constraints_impl(self, x):
sim=SpaceSim()
decisions=self._convertToList(x)
sim2 = sim.simulate(decisions)
dvx,dvy,dvz = self.getRelativeVelocity(sim2)
distance = self.getDistance(sim2)
ceq = list([0]*self.c_dim)
ceq[0]=distance-8e-6 # distance-0.01<=0
ceq[1]=(dvx**2+dvy**2+dvz**2)**0.5-0.0004
return ceq # distance-0.01<=0
def _convertToList(self,x):
dimention =self.dimension/3
decisions=[]
for i in range(dimention):
de=[]
de.append(x[i*3])
de.append(x[i*3+1])
de.append(x[i*3+2])
decisions.append(de)
return decisions
def getDistance(self, sim2):
rocketCord = (sim2.particles[3].x,sim2.particles[3].y,sim2.particles[3].z)
marsCord = (sim2.particles[2].x,sim2.particles[2].y,sim2.particles[2].z)
distance = ((rocketCord[0]-marsCord[0])**2 +(rocketCord[1]-marsCord[1])**2 +
(rocketCord[2]-marsCord[2])**2)**0.5
return distance
def getRelativeVelocity(self, sim2):
rocketCord = (sim2.particles[3].vx,sim2.particles[3].vy,sim2.particles[3].vz)
marsCord = (sim2.particles[2].vx,sim2.particles[2].vy,sim2.particles[2].vz)
return rocketCord[0]-marsCord[0],rocketCord[1]-marsCord[1],rocketCord[2]-marsCord[2]
def human_readable_extra(self):
return "\n\t Problem dimension: " + str(self.__dim)
class earthToMars1(base):
def __init__(self, dim=12*3,c_dim=2):
#USAGE: super(derived_class_name,self).__init__(dim, i_dim, n_obj, c_dim, c_ineq_dim, c_tol)
#dim: Total dimension of the decision vector
#i_dim: dimension of the integer part of decision vector (the integer part is placed at the end of the decision vector). Defaults to 0
#n_obj: number of objectives. Defaults to 1
#c_dim: total dimension of the constraint vector. dDefaults to 0
#c_ineq_dim: dimension of the inequality part of the constraint vector (inequality const. are placed at the end of the decision vector). Defaults to 0
#c_tol: constraint tolerance. When comparing individuals, this tolerance is used to decide whether a constraint is considered satisfied.
super(earthToMars1,self).__init__(dim, 0, 1, c_dim, 0, 1e-5)
self.set_bounds(-MAX_DV, MAX_DV)
self.c_dim=c_dim
self.dim=dim
def _objfun_impl(self, x):
sim=SpaceSim()
dimention =self.dimension/3
decisions=[]
de = [0,0,0]
for i in range(dimention):
de[0]=x[i*3]
de[1]=x[i*3+1]
de[2]=x[i*3+2]
decisions.append(de)
f = sim.simulate(decisions)
return (-f.particles[3].m,)
def _compute_constraints_impl(self, x):
sim=SpaceSim()
decisions=self._convertToList(x)
sim2 = sim.simulate(decisions)
dvx,dvy,dvz = self.getRelativeVelocity(sim2)
distance = self.getDistance(sim2)
ceq = list([0]*self.c_dim)
ceq[0]=distance-8e-6 # distance-0.01<=0
ceq[1]=(dvx**2+dvy**2+dvz**2)**0.5-0.0004
return ceq # distance-0.01<=0
def _convertToList(self,x):
dimention =self.dimension/3
decisions=[]
for i in range(dimention):
de=[]
de.append(x[i*3])
de.append(x[i*3+1])
de.append(x[i*3+2])
decisions.append(de)
return decisions
def getDistance(self, sim2):
rocketCord = (sim2.particles[3].x,sim2.particles[3].y,sim2.particles[3].z)
marsCord = (sim2.particles[2].x,sim2.particles[2].y,sim2.particles[2].z)
distance = ((rocketCord[0]-marsCord[0])**2 +(rocketCord[1]-marsCord[1])**2 +
(rocketCord[2]-marsCord[2])**2)**0.5
return distance
def getRelativeVelocity(self, sim2):
rocketCord = (sim2.particles[3].vx,sim2.particles[3].vy,sim2.particles[3].vz)
marsCord = (sim2.particles[2].vx,sim2.particles[2].vy,sim2.particles[2].vz)
return rocketCord[0]-marsCord[0],rocketCord[1]-marsCord[1],rocketCord[2]-marsCord[2]
def human_readable_extra(self):
return "\n\t Problem dimension: " + str(self.__dim)
def run_example1(n_restarts=20):
from PyGMO import algorithm, island
prob = earthToMars()
algo = algorithm.scipy_slsqp(max_iter=500, acc=1e-5)
algo2 = algorithm.mbh(algo, n_restarts, 0.005)
algo2.screen_output = True
isl = island(algo2, prob, 1)
print("Running Monotonic Basin Hopping .... this will take a while.")
isl.evolve(1)
isl.join()
print("Is the solution found a feasible trajectory? " +
str(prob.feasibility_x(isl.population.champion.x)))
return isl.population.champion.x
x=earthToMars()
vector=run_example1()
deci=x._convertToList(vector)
print deci
# sim2=SpaceSim()
# sim=sim2.simulateScatter(deci)
# import rebound
# import matplotlib.pyplot as plt
# fig = rebound.OrbitPlot(sim)
# plt.show()