PaGMO  1.1.5
gtoc5_flyby.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #include <string>
26 
27 #include <boost/integer_traits.hpp>
28 #include <boost/math/constants/constants.hpp>
29 #include <boost/numeric/conversion/bounds.hpp>
30 #include <boost/numeric/conversion/cast.hpp>
31 #include <cmath>
32 #include <sstream>
33 #include <stdexcept>
34 #include <vector>
35 
36 #include "../exceptions.h"
37 #include "../types.h"
38 #include "base.h"
39 #include "gtoc5_flyby.h"
40 #include <keplerian_toolbox/astro_constants.h>
41 #include <keplerian_toolbox/planet/gtoc5.h>
42 
43 using namespace kep_toolbox;
44 using namespace kep_toolbox::sims_flanagan;
45 
46 namespace pagmo { namespace problem {
48 
49 gtoc5_flyby::gtoc5_flyby(int segments, int source, int flyby, int target, const double &lb_epoch, const double &initial_mass, objective obj, const double & tof_ub, const double &ctol):
50  base(segments * 6 + 8, 0, 1, 14 + 2 * segments + 1, 2 * segments + 1,ctol),
51  m_n_segments(segments),m_source(source),m_flyby(flyby),m_target(target),m_lb_epoch(lb_epoch),m_initial_mass(initial_mass),m_obj(obj)
52 {
53  std::vector<double> lb_v(get_dimension());
54  std::vector<double> ub_v(get_dimension());
55 
56  // Source (MJD).
57  lb_v[0] = m_lb_epoch;
58  ub_v[0] = m_lb_epoch + 200;
59 
60  // Flyby days fraction
61  lb_v[1] = 0.001;
62  ub_v[1] = 0.99;
63 
64  // Total transfer time in days
65  lb_v[2] = 10;
66  ub_v[2] = 365.25 * tof_ub;
67 
68  // Mass at fly-by
69  lb_v[3] = 5;
70  ub_v[3] = m_initial_mass;
71 
72  // Mass at target
73  lb_v[4] = 5;
74  ub_v[4] = m_initial_mass;
75 
76  // Velocity at fly-by
77  for (int i = 5; i < 8; ++i) {
78  lb_v[i] = -2000;
79  ub_v[i] = 2000;
80  }
81 
82  // I Throttles
83  for (int i = 8; i < segments * 6 + 8; ++i)
84  {
85  lb_v[i] = -1;
86  ub_v[i] = 1;
87  }
88 
89  set_bounds(lb_v,ub_v);
90 
91  // Set spacecraft.
92  m_leg1.set_spacecraft(kep_toolbox::sims_flanagan::spacecraft(m_initial_mass,0.3,3000));
93  m_leg2.set_spacecraft(kep_toolbox::sims_flanagan::spacecraft(m_initial_mass,0.3,3000));
94 }
95 
98 {
99  return base_ptr(new gtoc5_flyby(*this));
100 }
101 
104 {
105  if (m_obj == MASS) {
106  f[0] = -x[4];
107  } else if (m_obj == TIME) {
108  f[0] = x[2];
109  } else {
110  f[0] = x[0] + x[2];
111  }
112 }
113 
116 {
117  using namespace kep_toolbox;
118  // We set the leg.
119  const epoch epoch_source(x[0],epoch::MJD), epoch_flyby(x[0] + x[1]*x[2],epoch::MJD), epoch_target(x[0] + x[2],epoch::MJD);
120  array3D v_source, r_source, v_flyby, r_flyby, v_target, r_target;
121  m_source.eph(epoch_source,r_source,v_source);
122  m_flyby.eph(epoch_flyby,r_flyby,v_flyby);
123  v_flyby[0] += x[5];
124  v_flyby[1] += x[6];
125  v_flyby[2] += x[7];
126  m_target.eph(epoch_target,r_target,v_target);
127  m_leg1.set_leg(epoch_source,sc_state(r_source,v_source,m_leg1.get_spacecraft().get_mass()),x.begin() + 8, x.begin() + 8 + m_n_segments * 3,
128  epoch_flyby,sc_state(r_flyby,v_flyby,x[3]),ASTRO_MU_SUN);
129  m_leg2.set_leg(epoch_flyby,sc_state(r_flyby,v_flyby,x[3] - 1),x.begin() + 8 + m_n_segments * 3, x.end(),
130  epoch_target,sc_state(r_target,v_target,x[4]),ASTRO_MU_SUN);
131 
132  // We evaluate the state mismatch at the mid-point. And we use astronomical units to scale them
133  m_leg1.get_mismatch_con(c.begin(), c.begin() + 7);
134  for (int i=0; i<3; ++i) c[i]/=ASTRO_AU;
135  for (int i=3; i<6; ++i) c[i]/=ASTRO_EARTH_VELOCITY;
136  c[6] /= m_leg1.get_spacecraft().get_mass();
137 
138  m_leg2.get_mismatch_con(c.begin() + 7, c.begin() + 14);
139  for (int i=7; i<10; ++i) c[i]/=ASTRO_AU;
140  for (int i=10; i<13; ++i) c[i]/=ASTRO_EARTH_VELOCITY;
141  c[13] /= m_leg1.get_spacecraft().get_mass(); // Scaling units for mass are identical throughtout the legs, thus m_leg1.
142 
143  // We evaluate the constraints on the throttles writing on the 7th mismatch constrant (mass is off)
144  m_leg1.get_throttles_con(c.begin() + 14, c.begin() + 14 + m_n_segments);
145  m_leg2.get_throttles_con(c.begin() + 14 + m_n_segments, c.begin() + 14 + 2 * m_n_segments);
146 
147  // Minimum flyby speed = 0.4 km/s.
148  c[14 + 2 * m_n_segments] = (160000. - (x[5] * x[5] + x[6] * x[6] + x[7] * x[7])) / 160000.;
149 }
150 
152 void gtoc5_flyby::set_sparsity(int &lenG, std::vector<int> &iGfun, std::vector<int> &jGvar) const
153 {
154  //Initial point
156  for (pagmo::decision_vector::size_type i = 0; i<x0.size(); ++i)
157  {
158  x0[i] = get_lb()[i] + (get_ub()[i] - get_lb()[i]) / 3.12345;
159  }
160  //Numerical procedure
161  estimate_sparsity(x0, lenG, iGfun, jGvar);
162 }
163 
164 std::string gtoc5_flyby::get_name() const
165 {
166  return "GTOC5 Flyby phase";
167 }
168 
169 std::string gtoc5_flyby::pretty(const decision_vector &x) const
170 {
171  using namespace kep_toolbox;
172  // We set the leg.
173  const epoch epoch_source(x[0],epoch::MJD), epoch_flyby(x[0] + x[1],epoch::MJD), epoch_target(x[0] + x[1] + x[2],epoch::MJD);
174  array3D v_source, r_source, v_flyby, r_flyby, v_target, r_target;
175  m_source.eph(epoch_source,r_source,v_source);
176  m_flyby.eph(epoch_flyby,r_flyby,v_flyby);
177  v_flyby[0] += x[5];
178  v_flyby[1] += x[6];
179  v_flyby[2] += x[7];
180  m_target.eph(epoch_target,r_target,v_target);
181  m_leg1.set_leg(epoch_source,sc_state(r_source,v_source,m_leg1.get_spacecraft().get_mass()),x.begin() + 8, x.begin() + 8 + m_n_segments * 3,
182  epoch_flyby,sc_state(r_flyby,v_flyby,x[3]),ASTRO_MU_SUN);
183  m_leg2.set_leg(epoch_flyby,sc_state(r_flyby,v_flyby,x[3] - 1),x.begin() + 8 + m_n_segments * 3, x.end(),
184  epoch_target,sc_state(r_target,v_target,x[4]),ASTRO_MU_SUN);
185 
186  std::ostringstream oss;
187  oss << m_leg1 << '\n' << m_source << '\n' << m_flyby << '\n';
188  oss << m_leg2 << '\n' << m_flyby << '\n' << m_target << '\n';
189  return oss.str();
190 }
191 
192 }}
193 
194 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::gtoc5_flyby);
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
gtoc5_flyby(int=10, int=1, int=2, int=3, const double &=57023, const double &=4000, objective=MASS, const double &tof_ub=3, const double &=1E-5)
Constructor.
Definition: gtoc5_flyby.cpp:49
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Implementation of the constraint function.
Base problem class.
Definition: problem/base.h:148
size_type get_dimension() const
Return global dimension.
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
Total time of flight.
Definition: gtoc5_flyby.h:52
std::string pretty(const decision_vector &) const
A nice string representation of a chromosome.
void set_sparsity(int &, std::vector< int > &, std::vector< int > &) const
Implementation of the sparsity structure: automated detection.
base_ptr clone() const
Clone method.
Definition: gtoc5_flyby.cpp:97
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::string get_name() const
Get problem's name.
void estimate_sparsity(const decision_vector &, int &lenG, std::vector< int > &iGfun, std::vector< int > &jGvar) const
Heuristics to estimate the sparsity pattern of the problem.
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
const decision_vector & get_ub() const
Upper bounds getter.
const decision_vector & get_lb() const
Lower bounds getter.
Test problem kep tool.
Definition: gtoc5_flyby.h:46
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
objective
Objective function to be minimized.
Definition: gtoc5_flyby.h:50
Propellant mass used.
Definition: gtoc5_flyby.h:51