def _compute_constraints_impl(self,x): from PyKEP import epoch, AU, EARTH_VELOCITY, fb_con from PyKEP.sims_flanagan import leg, sc_state from numpy.linalg import norm from math import sqrt, asin, acos #Ephemerides t_E = epoch(x[0]) t_V = epoch(x[0] + x[1]) t_M = epoch(x[0] + x[1] + x[9]) rE, vE = self.__earth.eph(t_E) rV, vV = self.__venus.eph(t_V) rM, vM = self.__mercury.eph(t_M) #First Leg v = [a+b for a,b in zip(vE,x[3:6])] x0 = sc_state(rE,v,self.__sc.mass) v = [a+b for a,b in zip(vV,x[6:9])] xe = sc_state(rV, v ,x[2]) self.__leg1.set(t_E,x0,x[-3 * (self.__nseg1 + self.__nseg2):-self.__nseg2 * 3],t_V,xe) #Second leg v = [a+b for a,b in zip(vV,x[11:14])] x0 = sc_state(rV,v,x[2]) v = [a+b for a,b in zip(vM,x[14:17])] xe = sc_state(rM, v ,x[10]) self.__leg2.set(t_E,x0,x[(-3 * self.__nseg2):],t_V,xe) #Defining the costraints #departure v_dep_con = (x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - self.__Vinf_dep * self.__Vinf_dep) / (EARTH_VELOCITY * EARTH_VELOCITY) #arrival v_arr_con = (x[14] * x[14] + x[15] * x[15] + x[16] * x[16] - self.__Vinf_arr * self.__Vinf_arr) / (EARTH_VELOCITY * EARTH_VELOCITY) #fly-by at Venus DV_eq, alpha_ineq = fb_con(x[6:9],x[11:14],self.__venus) #Assembling the constraints retval = list(self.__leg1.mismatch_constraints() + self.__leg2.mismatch_constraints()) + [DV_eq] + list(self.__leg1.throttles_constraints() + self.__leg2.throttles_constraints()) + [v_dep_con] + [v_arr_con] + [alpha_ineq] #We then scale all constraints to non dimensional values #leg 1 retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass #leg 2 retval[7] /= AU retval[8] /= AU retval[9] /= AU retval[10] /= EARTH_VELOCITY retval[11] /= EARTH_VELOCITY retval[12] /= EARTH_VELOCITY retval[13] /= self.__sc.mass #fly-by at Venus retval[14] /= (EARTH_VELOCITY*EARTH_VELOCITY) return retval
def _compute_constraints_impl(self, x): start = epoch(x[0]) end = epoch(x[0] + x[1]) r, v = self.earth.eph(start) v = [a + b for a, b in zip(v, x[3:6])] x0 = sc_state(r, v, self.sc.mass) r, v = self.mars.eph(end) xe = sc_state(r, v, x[2]) self.leg.set(start, x0, x[-3 * self.nseg:], end, xe) v_inf_con = (x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - self.Vinf * self.Vinf) / (EARTH_VELOCITY * EARTH_VELOCITY) retval = list(self.leg.mismatch_constraints() + self.leg.throttles_constraints()) + [v_inf_con] # We then scale all constraints to non-dimensional values retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.sc.mass return retval
def _compute_constraints_impl(self, x): from PyKEP import epoch, AU, EARTH_VELOCITY from PyKEP.sims_flanagan import leg, sc_state start = epoch(x[0]) end = epoch(x[0] + x[1]) r, v = self.__earth.eph(start) v = [a + b for a, b in zip(v, x[3:6])] x0 = sc_state(r, v, self.__sc.mass) r, v = self.__mars.eph(end) xe = sc_state(r, v, x[2]) self.__leg.set(start, x0, x[-3 * self.__nseg:], end, xe) v_inf_con = (x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - self.__Vinf * self.__Vinf) / (EARTH_VELOCITY * EARTH_VELOCITY) retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) + [v_inf_con] #We then scale all constraints to non dimensiona values retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass return retval
def _compute_constraints_impl(self,x): from PyKEP import epoch, AU, EARTH_VELOCITY from PyKEP.sims_flanagan import leg, sc_state start = epoch(x[0]) end = epoch(x[0] + x[1]) r,v = self.__earth.eph(start) v = [a+b for a,b in zip(v,x[3:6])] x0 = sc_state(r,v,self.__sc.mass) r,v = self.__mars.eph(end) xe = sc_state(r, v ,x[2]) self.__leg.set(start,x0,x[-3 * self.__nseg:],end,xe) v_inf_con = (x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - self.__Vinf * self.__Vinf) / (EARTH_VELOCITY * EARTH_VELOCITY) retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) + [v_inf_con] #We then scale all constraints to non dimensiona values retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass return retval
def _compute_constraints_impl(self, x): from PyKEP import epoch, AU, EARTH_VELOCITY, DAY2SEC from PyKEP.sims_flanagan import sc_state start = epoch(x[0]) end = epoch(x[0] + x[1]) r, v = self.__earth.eph(start) v = [a + b for a, b in zip(v, x[4:7])] x0 = sc_state(r, v, self.__sc.mass) r, v = self.__mars.eph(end) xe = sc_state(r, v, x[3]) self.__leg.set(start, x0, x[-3 * self.__nseg :], end, xe, x[2] * DAY2SEC) v_inf_con = (x[4] * x[4] + x[5] * x[5] + x[6] * x[6] - self.__Vinf * self.__Vinf) / ( EARTH_VELOCITY * EARTH_VELOCITY ) try: retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) + [v_inf_con] except: print "warning: CANNOT EVALUATE constraints .... possible problem in the taylor integration in the Sundmann variable" return (1e14,) * (8 + 1 + self.__nseg + 2) # We then scale all constraints to non-dimensional values retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass retval[7] /= 365.25 * DAY2SEC return retval
def plot(self, x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg t_E = epoch(x[0]) t_V = epoch(x[0] + x[1]) t_M = epoch(x[0] + x[1] + x[9]) rE, vE = self.__earth.eph(t_E) rV, vV = self.__venus.eph(t_V) rM, vM = self.__mercury.eph(t_M) #First Leg v = [a + b for a, b in zip(vE, x[3:6])] x0 = sc_state(rE, v, self.__sc.mass) v = [a + b for a, b in zip(vV, x[6:9])] xe = sc_state(rV, v, x[2]) self.__leg1.set( t_E, x0, x[-3 * (self.__nseg1 + self.__nseg2):-self.__nseg2 * 3], t_V, xe) #Second leg v = [a + b for a, b in zip(vV, x[11:14])] x0 = sc_state(rV, v, x[2]) v = [a + b for a, b in zip(vM, x[14:17])] xe = sc_state(rM, v, x[10]) self.__leg2.set(t_E, x0, x[(-3 * self.__nseg2):], t_V, xe) fig = plt.figure() axis = fig.gca(projection='3d') #The Sun axis.scatter([0], [0], [0], color='y') #The legs plot_sf_leg(self.__leg1, units=AU, N=10, ax=axis) plot_sf_leg(self.__leg2, units=AU, N=10, ax=axis) #The planets plot_planet(self.__earth, t_E, units=AU, legend=True, color=(0.7, 0.7, 1), ax=axis) plot_planet(self.__venus, t_V, units=AU, legend=True, color=(0.7, 0.7, 1), ax=axis) plot_planet(self.__mercury, t_M, units=AU, legend=True, color=(0.7, 0.7, 1), ax=axis) plt.show()
def plot(self,x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg start = epoch(x[0]) end = epoch(x[0] + x[1]) r,v = self.__earth.eph(start) v = [a+b for a,b in zip(v,x[3:6])] x0 = sc_state(r,v,self.__sc.mass) r,v = self.__mars.eph(end) xe = sc_state(r, v ,x[2]) self.__leg.set(start,x0,x[-3 * self.__nseg:],end,xe) fig = plt.figure() ax = fig.gca(projection='3d') #The Sun ax.scatter([0],[0],[0], color='y') #The leg plot_sf_leg(ax, self.__leg, units=AU,N=10) #The planets plot_planet(ax, self.__earth, start, units=AU, legend = True,color=(0.8,0.8,1)) plot_planet(ax, self.__mars, end, units=AU, legend = True,color=(0.8,0.8,1)) plt.show()
def _compute_constraints_impl(self,x): from PyKEP import epoch, AU, EARTH_VELOCITY, DAY2SEC from PyKEP.sims_flanagan import sc_state start = epoch(x[0]) end = epoch(x[0] + x[1]) r,v = self.__earth.eph(start) v = [a+b for a,b in zip(v,x[4:7])] x0 = sc_state(r,v,self.__sc.mass) r,v = self.__mars.eph(end) xe = sc_state(r, v ,x[3]) self.__leg.set(start,x0,x[-3 * self.__nseg:],end,xe, x[2] * DAY2SEC) v_inf_con = (x[4] * x[4] + x[5] * x[5] + x[6] * x[6] - self.__Vinf * self.__Vinf) / (EARTH_VELOCITY * EARTH_VELOCITY) try: retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) + [v_inf_con] except: print "warning: CANNOT EVALUATE constraints .... possible problem in the Taylor integration in the Sundmann variable" return (1e14,)*(8+1+self.__nseg+2) #We then scale all constraints to non-dimensional values retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass retval[7] /= 365.25 * DAY2SEC return retval
def plot(self,x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_sf_leg mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0,0,0, color='y') # 1 - We decode the chromosome extracting the time of flights T = list([0]*(self.__n_legs)) for i in range(self.__n_legs): T[i] = x[2+i*8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs+1)) r_P = list([None] * (self.__n_legs+1)) v_P = list([None] * (self.__n_legs+1)) for i,planet in enumerate(self.seq): t_P[i+1] = epoch(self.tf - sum(T[i+1:])) r_P[i+1],v_P[i+1] = self.seq[i].eph(t_P[i+1]) plot_planet(ax, self.seq[i], t_P[i+1], units=JR, legend = True,color='k') #And we insert a fake planet simulating the starting position t_P[0] = epoch(self.tf - sum(T)) theta = x[0] phi = x[1] r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] r_P[0] = r v_P[0] = x[4:7] #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): if i!=0: #First Leg v = [a+b for a,b in zip(v_P[i],x[(4 + i * 8):(7 + i * 8)])] else: v = v_P[i] x0 = sc_state(r_P[i],v,m0) v = [a+b for a,b in zip(v_P[i+1],x[(7 + i * 8):(10 + i * 8)])] if (i==self.__n_legs-1): #Last leg v = [a+b for a,b in zip(v_P[i+1],self.vf)] xe = sc_state(r_P[i+1], v ,x[3+8*i]) throttles = x[(8*self.__n_legs-1 + 3*sum(self.__n_seg[:i])):(8*self.__n_legs-1 + 3*sum(self.__n_seg[:i])+3*self.__n_seg[i])] self.__leg.set(t_P[i],x0,throttles,t_P[i+1],xe) plot_sf_leg(ax, self.__leg, units=JR,N=50) #update mass! m0 = x[3+8*i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #raise Exception plt.show() return ax
def plot(self,x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU, DAY2SEC from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg start = epoch(x[0]) end = epoch(x[0] + x[1]) r,v = self.__earth.eph(start) v = [a+b for a,b in zip(v,x[4:7])] x0 = sc_state(r,v,self.__sc.mass) r,v = self.__mars.eph(end) xe = sc_state(r, v ,x[3]) self.__leg.set(start,x0,x[-3 * self.__nseg:],end,xe, x[2] * DAY2SEC) fig = plt.figure() axis = fig.gca(projection='3d') #The Sun axis.scatter([0],[0],[0], color='y') #The leg plot_sf_leg(self.__leg, units=AU,N=10, ax = axis) #The planets plot_planet(self.__earth, start, units=AU, legend = True,color=(0.8,0.8,1), ax = axis) plot_planet(self.__mars, end, units=AU, legend = True,color=(0.8,0.8,1), ax = axis) plt.show()
def plot(self,x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg fig = plt.figure() ax = fig.gca(projection='3d') #Plotting the Sun ........ ax.scatter([0],[0],[0], color='y') #Plotting the legs ....... # 1 - We decode the chromosome extracting the time of flights T = list([0]*(self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1+i*8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs+1)) r_P = list([None] * (self.__n_legs+1)) v_P = list([None] * (self.__n_legs+1)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): #First Leg v = [a+b for a,b in zip(v_P[i],x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i],v,m0) v = [a+b for a,b in zip(v_P[i+1],x[(6 + i * 8):(11 + i * 8)])] xe = sc_state(r_P[i+1], v ,x[2 + i * 8]) throttles = x[(1 +8 * self.__n_legs + 3*sum(self.__n_seg[:i])):(1 +8 * self.__n_legs + 3*sum(self.__n_seg[:i])+3*self.__n_seg[i])] self.__leg.set(t_P[i],x0,throttles,t_P[i+1],xe) #update mass! m0 = x[2+8*i] plot_sf_leg(ax, self.__leg, units=AU,N=10) #Plotting planets for i,planet in enumerate(self.seq): plot_planet(ax, planet, t_P[i], units=AU, legend = True,color=(0.7,0.7,1)) plt.show()
def plot(self, x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg t_E = epoch(x[0]) t_V = epoch(x[0] + x[1]) t_M = epoch(x[0] + x[1] + x[9]) rE, vE = self.__earth.eph(t_E) rV, vV = self.__venus.eph(t_V) rM, vM = self.__mercury.eph(t_M) # First Leg v = [a + b for a, b in zip(vE, x[3:6])] x0 = sc_state(rE, v, self.__sc.mass) v = [a + b for a, b in zip(vV, x[6:9])] xe = sc_state(rV, v, x[2]) self.__leg1.set( t_E, x0, x[-3 * (self.__nseg1 + self.__nseg2):-self.__nseg2 * 3], t_V, xe) # Second leg v = [a + b for a, b in zip(vV, x[11:14])] x0 = sc_state(rV, v, x[2]) v = [a + b for a, b in zip(vM, x[14:17])] xe = sc_state(rM, v, x[10]) self.__leg2.set(t_E, x0, x[(-3 * self.__nseg2):], t_V, xe) fig = plt.figure() axis = fig.gca(projection='3d') # The Sun axis.scatter([0], [0], [0], color='y') # The legs plot_sf_leg(self.__leg1, units=AU, N=10, ax=axis) plot_sf_leg(self.__leg2, units=AU, N=10, ax=axis) # The planets plot_planet( self.__earth, t_E, units=AU, legend=True, color=(0.7, 0.7, 1), ax = axis) plot_planet( self.__venus, t_V, units=AU, legend=True, color=(0.7, 0.7, 1), ax = axis) plot_planet( self.__mercury, t_M, units=AU, legend=True, color=(0.7, 0.7, 1), ax = axis) plt.show()
def _compute_constraints_impl(self,x): x0 = sc_state(self.__r[0],self.__v[0],self.__sc.mass) xf = sc_state(self.__r[1],self.__v[1],x[0]) throttles = x[1:] self.__leg.set(self.__t[0],x0,throttles,self.__t[1], xf) #leg = self.__leg #raise Exception retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) retval[0] = retval[0] / (JR) retval[1] = retval[1] / (JR) retval[2] = retval[2] / (JR) #using Europa velocity as unit retval[3] = retval[3] / 13000 retval[4] = retval[4] / 13000 retval[5] = retval[5] / 13000 retval[6] = retval[6] / 1000 return retval
def _compute_constraints_impl(self, x): x0 = sc_state(self.__r[0], self.__v[0], self.__sc.mass) xf = sc_state(self.__r[1], self.__v[1], x[0]) throttles = x[1:] self.__leg.set(self.__t[0], x0, throttles, self.__t[1], xf) #leg = self.__leg #raise Exception retval = list(self.__leg.mismatch_constraints() + self.__leg.throttles_constraints()) retval[0] = retval[0] / (JR) retval[1] = retval[1] / (JR) retval[2] = retval[2] / (JR) #using Europa velocity as unit retval[3] = retval[3] / 13000 retval[4] = retval[4] / 13000 retval[5] = retval[5] / 13000 retval[6] = retval[6] / 1000 return retval
def _pl2pl_fixed_time_plot(self, x): from PyGMO import problem, algorithm, population from PyKEP import sims_flanagan, AU from PyKEP.orbit_plots import plot_planet, plot_sf_leg from PyKEP import phasing import matplotlib.pyplot as plt import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') ast1, ast2 = self.get_sequence() leg = self.get_leg() t_i = self.get_t0() t_f = self.get_t1() # Get initial sc state sc = leg.get_spacecraft() r, v = ast1.eph(t_i) xi = sims_flanagan.sc_state(r, v, sc.mass) # Get final sc state r, v = ast2.eph(t_f) xf = sims_flanagan.sc_state(r, v, x[0]) # Update leg with final sc state and throttles leg.set(t_i, xi, x[1:], t_f, xf) # Plot asteroid orbits plot_planet(ast1, t0=t_i, color=(0.8, 0.6, 0.4), legend=True, units=AU, ax=ax) plot_planet(ast2, t0=t_f, color=(0.8, 0.6, 0.8), legend=True, units=AU, ax=ax) # Plot Sims-Flanagan leg plot_sf_leg(leg, units=AU, N=50, ax=ax) return ax
def _compute_constraints_impl(self,x): # 1 - We decode the chromosome extracting the time of flights T = list([0]*(self.__n_legs)) for i in range(self.__n_legs): T[i] = x[2+i*8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs+1)) r_P = list([None] * (self.__n_legs+1)) v_P = list([None] * (self.__n_legs+1)) for i,planet in enumerate(self.seq): t_P[i+1] = epoch(self.tf - sum(T[i+1:])) r_P[i+1],v_P[i+1] = self.seq[i].eph(t_P[i+1]) #And we insert a fake planet simulating the starting position t_P[0] = epoch(self.tf - sum(T)) theta = x[0] phi = x[1] r = [cos(phi)*sin(theta), cos(phi)*cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR*1000*d for d in r] r_P[0] = r v_P[0] = x[4:7] #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): if i!=0: #First Leg v = [a+b for a,b in zip(v_P[i],x[(4 + i * 8):(7 + i * 8)])] else: v = v_P[i] x0 = sc_state(r_P[i],v,m0) v = [a+b for a,b in zip(v_P[i+1],x[(7 + i * 8):(10 + i * 8)])] if (i==self.__n_legs-1): #Last leg v = [a+b for a,b in zip(v_P[i+1],self.vf)] xe = sc_state(r_P[i+1], v ,x[3+8*i]) throttles = x[(8*self.__n_legs-1 + 3*sum(self.__n_seg[:i])):(8*self.__n_legs-1 + 3*sum(self.__n_seg[:i])+3*self.__n_seg[i])] self.__leg.set(t_P[i],x0,throttles,t_P[i+1],xe) #update mass! m0 = x[3+8*i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #raise Exception #Adding the boundary constraints #departure v_dep_con = (x[4] ** 2 + x[5] ** 2 + x[6] **2 - 3400.0 ** 2) / (3400.0**2) #arrival ceq.append(v_dep_con) #We add the fly-by constraints for i in range(self.__n_legs-1): DV_eq, alpha_ineq = fb_con(x[7 + i*8:10 + i*8],x[12+ i * 8:15+ i * 8],self.seq[i]) ceq.append(DV_eq / (3400.0**2)) cineq.append(alpha_ineq) #Making the mismatches non dimensional for i in range(self.__n_legs): ceq[0+i*7] /= JR*1000 ceq[1+i*7] /= JR*1000 ceq[2+i*7] /= JR*1000 ceq[3+i*7] /= 3400 ceq[4+i*7] /= 3400 ceq[5+i*7] /= 3400 ceq[6+i*7] /= 1000 #We assemble the constraint vector retval = list() retval.extend(ceq) retval.extend(cineq) return retval
def plot(self, x, ax=None): """ ax = prob.plot(x, ax=None) - x: encoded trajectory - ax: matplotlib axis where to plot. If None figure and axis will be created - [out] ax: matplotlib axis where to plot Plots the trajectory represented by a decision vector x on the 3d axis ax Example:: ax = prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg # Creating the axis if necessary if ax is None: mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() axis = fig.gca(projection='3d') else: axis = ax # Plotting the Sun ........ axis.scatter([0], [0], [0], color='y') # Plotting the legs ....... # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1 + i * 8] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) # 3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): # First Leg v = [a + b for a, b in zip(v_P[i], x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i], v, m0) v = [ a + b for a, b in zip(v_P[i + 1], x[(6 + i * 8):(11 + i * 8)]) ] xe = sc_state(r_P[i + 1], v, x[2 + i * 8]) throttles = x[(1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i])):( 1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) # update mass! m0 = x[2 + 8 * i] plot_sf_leg(self.__leg, units=AU, N=10, ax=axis) # Plotting planets for i, planet in enumerate(self.seq): plot_planet(planet, t_P[i], units=AU, legend=True, color=(0.7, 0.7, 1), ax=axis) plt.show() return axis
def _compute_constraints_impl(self, x): from PyKEP import epoch, AU, EARTH_VELOCITY, fb_con from PyKEP.sims_flanagan import leg, sc_state from numpy.linalg import norm from math import sqrt, asin, acos #Ephemerides t_E = epoch(x[0]) t_V = epoch(x[0] + x[1]) t_M = epoch(x[0] + x[1] + x[9]) rE, vE = self.__earth.eph(t_E) rV, vV = self.__venus.eph(t_V) rM, vM = self.__mercury.eph(t_M) #First Leg v = [a + b for a, b in zip(vE, x[3:6])] x0 = sc_state(rE, v, self.__sc.mass) v = [a + b for a, b in zip(vV, x[6:9])] xe = sc_state(rV, v, x[2]) self.__leg1.set( t_E, x0, x[-3 * (self.__nseg1 + self.__nseg2):-self.__nseg2 * 3], t_V, xe) #Second leg v = [a + b for a, b in zip(vV, x[11:14])] x0 = sc_state(rV, v, x[2]) v = [a + b for a, b in zip(vM, x[14:17])] xe = sc_state(rM, v, x[10]) self.__leg2.set(t_E, x0, x[(-3 * self.__nseg2):], t_V, xe) #Defining the constraints #departure v_dep_con = (x[3] * x[3] + x[4] * x[4] + x[5] * x[5] - self.__Vinf_dep * self.__Vinf_dep) / (EARTH_VELOCITY * EARTH_VELOCITY) #arrival v_arr_con = (x[14] * x[14] + x[15] * x[15] + x[16] * x[16] - self.__Vinf_arr * self.__Vinf_arr) / (EARTH_VELOCITY * EARTH_VELOCITY) #fly-by at Venus DV_eq, alpha_ineq = fb_con(x[6:9], x[11:14], self.__venus) #Assembling the constraints retval = list(self.__leg1.mismatch_constraints() + self.__leg2.mismatch_constraints()) + [ DV_eq ] + list(self.__leg1.throttles_constraints() + self.__leg2.throttles_constraints()) + [ v_dep_con ] + [v_arr_con] + [alpha_ineq] #We then scale all constraints to non-dimensional values #leg 1 retval[0] /= AU retval[1] /= AU retval[2] /= AU retval[3] /= EARTH_VELOCITY retval[4] /= EARTH_VELOCITY retval[5] /= EARTH_VELOCITY retval[6] /= self.__sc.mass #leg 2 retval[7] /= AU retval[8] /= AU retval[9] /= AU retval[10] /= EARTH_VELOCITY retval[11] /= EARTH_VELOCITY retval[12] /= EARTH_VELOCITY retval[13] /= self.__sc.mass #fly-by at Venus retval[14] /= (EARTH_VELOCITY * EARTH_VELOCITY) return retval
def plot(self, x, ax=None): """ ax = prob.plot(x, ax=None) - x: encoded trajectory - ax: matplotlib axis where to plot. If None figure and axis will be created - [out] ax: matplotlib axis where to plot Plots the trajectory represented by a decision vector x on the 3d axis ax Example:: ax = prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg # Creating the axis if necessary if ax is None: mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() axis = fig.gca(projection='3d') else: axis = ax # Plotting the Sun ........ axis.scatter([0], [0], [0], color='y') # Plotting the legs ....... # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1 + i * 8] # 2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) # 3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): # First Leg v = [a + b for a, b in zip(v_P[i], x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i], v, m0) v = [a + b for a, b in zip(v_P[i + 1], x[(6 + i * 8):(11 + i * 8)])] xe = sc_state(r_P[i + 1], v, x[2 + i * 8]) throttles = x[(1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i])):(1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) # update mass! m0 = x[2 + 8 * i] plot_sf_leg(self.__leg, units=AU, N=10, ax=axis) # Plotting planets for i, planet in enumerate(self.seq): plot_planet(planet, t_P[i], units=AU, legend=True, color=(0.7, 0.7, 1), ax = axis) plt.show() return axis
def _compute_constraints_impl(self, x): # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[2 + i * 8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i + 1] = epoch(self.tf - sum(T[i + 1:])) r_P[i + 1], v_P[i + 1] = self.seq[i].eph(t_P[i + 1]) #And we insert a fake planet simulating the starting position t_P[0] = epoch(self.tf - sum(T)) theta = x[0] phi = x[1] r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] r_P[0] = r v_P[0] = x[4:7] #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): if i != 0: #First Leg v = [a + b for a, b in zip(v_P[i], x[(4 + i * 8):(7 + i * 8)])] else: v = v_P[i] x0 = sc_state(r_P[i], v, m0) v = [ a + b for a, b in zip(v_P[i + 1], x[(7 + i * 8):(10 + i * 8)]) ] if (i == self.__n_legs - 1): #Last leg v = [a + b for a, b in zip(v_P[i + 1], self.vf)] xe = sc_state(r_P[i + 1], v, x[3 + 8 * i]) throttles = x[(8 * self.__n_legs - 1 + 3 * sum(self.__n_seg[:i])):( 8 * self.__n_legs - 1 + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) #update mass! m0 = x[3 + 8 * i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #raise Exception #Adding the boundary constraints #departure v_dep_con = (x[4]**2 + x[5]**2 + x[6]**2 - 3400.0**2) / (3400.0**2) #arrival ceq.append(v_dep_con) #We add the fly-by constraints for i in range(self.__n_legs - 1): DV_eq, alpha_ineq = fb_con(x[7 + i * 8:10 + i * 8], x[12 + i * 8:15 + i * 8], self.seq[i]) ceq.append(DV_eq / (3400.0**2)) cineq.append(alpha_ineq) #Making the mismatches non dimensional for i in range(self.__n_legs): ceq[0 + i * 7] /= JR * 1000 ceq[1 + i * 7] /= JR * 1000 ceq[2 + i * 7] /= JR * 1000 ceq[3 + i * 7] /= 3400 ceq[4 + i * 7] /= 3400 ceq[5 + i * 7] /= 3400 ceq[6 + i * 7] /= 1000 #We assemble the constraint vector retval = list() retval.extend(ceq) retval.extend(cineq) return retval
def plot(self, x): import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP.orbit_plots import plot_planet, plot_sf_leg mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(0, 0, 0, color='y') # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[2 + i * 8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i + 1] = epoch(self.tf - sum(T[i + 1:])) r_P[i + 1], v_P[i + 1] = self.seq[i].eph(t_P[i + 1]) plot_planet(ax, self.seq[i], t_P[i + 1], units=JR, legend=True, color='k') #And we insert a fake planet simulating the starting position t_P[0] = epoch(self.tf - sum(T)) theta = x[0] phi = x[1] r = [cos(phi) * sin(theta), cos(phi) * cos(theta), sin(phi)] #phi close to zero is in the moon orbit plane injection r = [JR * 1000 * d for d in r] r_P[0] = r v_P[0] = x[4:7] #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): if i != 0: #First Leg v = [a + b for a, b in zip(v_P[i], x[(4 + i * 8):(7 + i * 8)])] else: v = v_P[i] x0 = sc_state(r_P[i], v, m0) v = [ a + b for a, b in zip(v_P[i + 1], x[(7 + i * 8):(10 + i * 8)]) ] if (i == self.__n_legs - 1): #Last leg v = [a + b for a, b in zip(v_P[i + 1], self.vf)] xe = sc_state(r_P[i + 1], v, x[3 + 8 * i]) throttles = x[(8 * self.__n_legs - 1 + 3 * sum(self.__n_seg[:i])):( 8 * self.__n_legs - 1 + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) plot_sf_leg(ax, self.__leg, units=JR, N=50) #update mass! m0 = x[3 + 8 * i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #raise Exception plt.show() return ax
def _compute_constraints_impl(self, x): # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1 + i * 8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): #First Leg v = [a + b for a, b in zip(v_P[i], x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i], v, m0) v = [a + b for a, b in zip(v_P[i + 1], x[(6 + i * 8):(9 + i * 8)])] xe = sc_state(r_P[i + 1], v, x[2 + i * 8]) throttles = x[(1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i])):( 1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) #update mass! m0 = x[2 + 8 * i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #Adding the boundary constraints #departure v_dep_con = (x[3]**2 + x[4]**2 + x[5]**2 - self.__vinf_dep**2) / (EARTH_VELOCITY**2) #arrival v_arr_con = (x[6 + (self.__n_legs - 1) * 8]**2 + x[7 + (self.__n_legs - 1) * 8] **2 + x[8 + (self.__n_legs - 1) * 8]**2 - self.__vinf_arr**2) / (EARTH_VELOCITY**2) cineq.append(v_dep_con * 100) cineq.append(v_arr_con * 100) #We add the fly-by constraints for i in range(self.__n_legs - 1): DV_eq, alpha_ineq = fb_con(x[6 + i * 8:9 + i * 8], x[11 + i * 8:14 + i * 8], self.seq[i + 1]) ceq.append(DV_eq / (EARTH_VELOCITY**2)) cineq.append(alpha_ineq) #Making the mismatches non dimensional for i in range(self.__n_legs): ceq[0 + i * 7] /= AU ceq[1 + i * 7] /= AU ceq[2 + i * 7] /= AU ceq[3 + i * 7] /= EARTH_VELOCITY ceq[4 + i * 7] /= EARTH_VELOCITY ceq[5 + i * 7] /= EARTH_VELOCITY ceq[6 + i * 7] /= self.__sc.mass #We assemble the constraint vector retval = list() retval.extend(ceq) retval.extend(cineq) return retval
def plot(self, x): """ Plots the trajectory represented by the decision vector x Example:: prob.plot(x) """ import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from PyKEP import epoch, AU from PyKEP.sims_flanagan import sc_state from PyKEP.orbit_plots import plot_planet, plot_sf_leg fig = plt.figure() ax = fig.gca(projection='3d') #Plotting the Sun ........ ax.scatter([0], [0], [0], color='y') #Plotting the legs ....... # 1 - We decode the chromosome extracting the time of flights T = list([0] * (self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1 + i * 8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs + 1)) r_P = list([None] * (self.__n_legs + 1)) v_P = list([None] * (self.__n_legs + 1)) for i, planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i], v_P[i] = self.seq[i].eph(t_P[i]) #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): #First Leg v = [a + b for a, b in zip(v_P[i], x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i], v, m0) v = [ a + b for a, b in zip(v_P[i + 1], x[(6 + i * 8):(11 + i * 8)]) ] xe = sc_state(r_P[i + 1], v, x[2 + i * 8]) throttles = x[(1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i])):( 1 + 8 * self.__n_legs + 3 * sum(self.__n_seg[:i]) + 3 * self.__n_seg[i])] self.__leg.set(t_P[i], x0, throttles, t_P[i + 1], xe) #update mass! m0 = x[2 + 8 * i] plot_sf_leg(ax, self.__leg, units=AU, N=10) #Plotting planets for i, planet in enumerate(self.seq): plot_planet(ax, planet, t_P[i], units=AU, legend=True, color=(0.7, 0.7, 1)) plt.show()
def _compute_constraints_impl(self,x): # 1 - We decode the chromosome extracting the time of flights T = list([0]*(self.__n_legs)) for i in range(self.__n_legs): T[i] = x[1+i*8] #2 - We compute the epochs and ephemerides of the planetary encounters t_P = list([None] * (self.__n_legs+1)) r_P = list([None] * (self.__n_legs+1)) v_P = list([None] * (self.__n_legs+1)) for i,planet in enumerate(self.seq): t_P[i] = epoch(x[0] + sum(T[0:i])) r_P[i],v_P[i] = self.seq[i].eph(t_P[i]) #3 - We iterate through legs to compute mismatches and throttles constraints ceq = list() cineq = list() m0 = self.__sc.mass for i in range(self.__n_legs): #First Leg v = [a+b for a,b in zip(v_P[i],x[(3 + i * 8):(6 + i * 8)])] x0 = sc_state(r_P[i],v,m0) v = [a+b for a,b in zip(v_P[i+1],x[(6 + i * 8):(9 + i * 8)])] xe = sc_state(r_P[i+1], v ,x[2 + i * 8]) throttles = x[(1 + 8*self.__n_legs + 3*sum(self.__n_seg[:i])):(1 + 8*self.__n_legs + 3*sum(self.__n_seg[:i])+3*self.__n_seg[i])] self.__leg.set(t_P[i],x0,throttles,t_P[i+1],xe) #update mass! m0 = x[2+8*i] ceq.extend(self.__leg.mismatch_constraints()) cineq.extend(self.__leg.throttles_constraints()) #Adding the boundary constraints #departure v_dep_con = (x[3] ** 2 + x[4] ** 2 + x[5] **2 - self.__vinf_dep ** 2) / (EARTH_VELOCITY**2) #arrival v_arr_con = (x[6 + (self.__n_legs-1)*8]**2 + x[7+ (self.__n_legs-1)*8]**2 + x[8+ (self.__n_legs-1)*8]**2 - self.__vinf_arr ** 2) / (EARTH_VELOCITY**2) cineq.append(v_dep_con*100) cineq.append(v_arr_con*100) #We add the fly-by constraints for i in range(self.__n_legs-1): DV_eq, alpha_ineq = fb_con(x[6 + i*8:9 + i*8],x[11+ i * 8:14+ i * 8],self.seq[i+1]) ceq.append(DV_eq / (EARTH_VELOCITY**2)) cineq.append(alpha_ineq) #Making the mismatches non dimensional for i in range(self.__n_legs): ceq[0+i*7] /= AU ceq[1+i*7] /= AU ceq[2+i*7] /= AU ceq[3+i*7] /= EARTH_VELOCITY ceq[4+i*7] /= EARTH_VELOCITY ceq[5+i*7] /= EARTH_VELOCITY ceq[6+i*7] /= self.__sc.mass #We assemble the constraint vector retval = list() retval.extend(ceq) retval.extend(cineq) return retval