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, axes=axis) # The planets plot_planet( self.__earth, start, units=AU, legend=True, color=(0.8, 0.8, 1), axes=axis) plot_planet( self.__mars, end, units=AU, legend=True, color=(0.8, 0.8, 1), axes=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, 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): 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 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 fitness(self, x): from pykep import epoch, AU, EARTH_VELOCITY, DAY2SEC from pykep.sims_flanagan import sc_state # This is the objective function objfun = [-x[3]] # And these are the constraints 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: constraints = 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" ) constraints = (1e14, ) * (8 + 1 + self.__nseg + 2) # We then scale all constraints to non-dimensional values constraints[0] /= AU constraints[1] /= AU constraints[2] /= AU constraints[3] /= EARTH_VELOCITY constraints[4] /= EARTH_VELOCITY constraints[5] /= EARTH_VELOCITY constraints[6] /= self.__sc.mass constraints[7] /= 365.25 * DAY2SEC return objfun + constraints
def fitness(self, x): from pykep import epoch, AU, EARTH_VELOCITY, DAY2SEC from pykep.sims_flanagan import sc_state # This is the objective function objfun = [-x[3]] # And these are the constraints 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: constraints = 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") constraints = (1e14,) * (8 + 1 + self.__nseg + 2) # We then scale all constraints to non-dimensional values constraints[0] /= AU constraints[1] /= AU constraints[2] /= AU constraints[3] /= EARTH_VELOCITY constraints[4] /= EARTH_VELOCITY constraints[5] /= EARTH_VELOCITY constraints[6] /= self.__sc.mass constraints[7] /= 365.25 * DAY2SEC return objfun + constraints
def fitness(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 retval = [-x[10]] # 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 constraints = 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 constraints[0] /= AU constraints[1] /= AU constraints[2] /= AU constraints[3] /= EARTH_VELOCITY constraints[4] /= EARTH_VELOCITY constraints[5] /= EARTH_VELOCITY constraints[6] /= self.__sc.mass # leg 2 constraints[7] /= AU constraints[8] /= AU constraints[9] /= AU constraints[10] /= EARTH_VELOCITY constraints[11] /= EARTH_VELOCITY constraints[12] /= EARTH_VELOCITY constraints[13] /= self.__sc.mass # fly-by at Venus constraints[14] /= (EARTH_VELOCITY * EARTH_VELOCITY) return retval + constraints
def plot(self, x, axes=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.orbit_plots import plot_sf_leg, plot_planet # Creating the axis if necessary if axes is None: mpl.rcParams['legend.fontsize'] = 10 fig = plt.figure() ax = fig.gca(projection='3d') else: ax = axes # Plotting the Sun ........ ax.scatter([0], [0], [0], color=['y']) # 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 in range(len(self._seq)): t_P[i] = epoch(x[0] + sum(x[1:i * 8:8])) r_P[i], v_P[i] = self._seq[i].eph(t_P[i]) plot_planet(self._seq[i], t0=t_P[i], units=AU, legend=False, color=(0.7, 0.7, 0.7), s=30, axes=ax) # We assemble the constraints. # 1 - Mismatch Constraints for i in range(self._n_legs): # Departure velocity of the spacecraft in the heliocentric frame v0 = [a + b for a, b in zip(v_P[i], x[3 + 8 * i:6 + 8 * i])] if i == 0: m0 = self._mass[1] else: m0 = x[2 + 8 * (i - 1)] x0 = sc_state(r_P[i], v0, m0) vf = [a + b for a, b in zip(v_P[i + 1], x[6 + 8 * i:9 + 8 * i])] xf = sc_state(r_P[i + 1], vf, x[2 + 8 * i]) idx_start = 1 + 8 * self._n_legs + sum(self._n_seg[:i]) * 3 idx_end = 1 + 8 * self._n_legs + sum(self._n_seg[:i + 1]) * 3 self._leg.set(t_P[i], x0, x[idx_start:idx_end], t_P[i + 1], xf) plot_sf_leg(self._leg, units=AU, N=10, axes=ax, legend=False) return axes
def fitness(self, x): # The final mass is the fitness objfun = [-x[2 + 8 * (self._n_legs - 1)]] eq_c = [] ineq_c = [] if self._multiobjective: objfun = objfun + [sum(x[1:8 * self._n_legs:8])] # 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 in range(len(self._seq)): t_P[i] = epoch(x[0] + sum(x[1:i * 8:8])) r_P[i], v_P[i] = self._seq[i].eph(t_P[i]) # We assemble the constraints. # 1 - Mismatch Constraints for i in range(self._n_legs): # Departure velocity of the spacecraft in the heliocentric frame v0 = [a + b for a, b in zip(v_P[i], x[3 + 8 * i:6 + 8 * i])] if i == 0: m0 = self._mass[1] else: m0 = x[2 + 8 * (i - 1)] x0 = sc_state(r_P[i], v0, m0) vf = [a + b for a, b in zip(v_P[i + 1], x[6 + 8 * i:9 + 8 * i])] xf = sc_state(r_P[i + 1], vf, x[2 + 8 * i]) idx_start = 1 + 8 * self._n_legs + sum(self._n_seg[:i]) * 3 idx_end = 1 + 8 * self._n_legs + sum(self._n_seg[:i + 1]) * 3 self._leg.set(t_P[i], x0, x[idx_start:idx_end], t_P[i + 1], xf) mismatch = list(self._leg.mismatch_constraints()) # Making the mismatch non dimensional (assumes an heliocentric interplanetary trajectory) mismatch[0] /= AU mismatch[1] /= AU mismatch[2] /= AU mismatch[3] /= EARTH_VELOCITY mismatch[4] /= EARTH_VELOCITY mismatch[5] /= EARTH_VELOCITY mismatch[6] /= self._mass[1] eq_c = eq_c + mismatch ineq_c = ineq_c + list(self._leg.throttles_constraints()) # 2 - 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]) eq_c = eq_c + [DV_eq / (EARTH_VELOCITY * EARTH_VELOCITY)] ineq_c = ineq_c + [alpha_ineq] # 3 - Departure and arrival Vinf # 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 n_fb = self._n_legs - 1 v_arr_con = (x[6 + n_fb * 8] * x[6 + n_fb * 8] + x[7 + n_fb * 8] * x[7 + n_fb * 8] + x[8 + n_fb * 8] * x[8 + n_fb * 8] - self._vinf_arr * self._vinf_arr) / (EARTH_VELOCITY * EARTH_VELOCITY) ineq_c = ineq_c + [v_dep_con, v_arr_con] return objfun + eq_c + ineq_c