PaGMO  1.1.5
gtoc5_self_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_self_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_self_flyby::gtoc5_self_flyby(int segments, int ast_id, const double &mjd, const double &initial_mass, const double &ctol):
50  base(segments * 3 + 5, 0, 1, 7 + segments + 1, segments + 1,ctol),
51  m_n_segments(segments),m_ast(ast_id),m_mjd(mjd),m_initial_mass(initial_mass)
52 {
53  std::vector<double> lb_v(get_dimension());
54  std::vector<double> ub_v(get_dimension());
55 
56  // Flyby (MJD) transfer time in days
57  lb_v[0] = 5;
58  ub_v[0] = 200;
59 
60  // Mass at fly-by
61  lb_v[1] = 100;
62  ub_v[1] = m_initial_mass;
63 
64  // Velocity at fly-by
65  for (int i = 2; i < 5; ++i) {
66  lb_v[i] = -1500;
67  ub_v[i] = 1500;
68  }
69 
70  // I Throttles
71  for (int i = 5; i < segments * 3 + 5; ++i)
72  {
73  lb_v[i] = -1;
74  ub_v[i] = 1;
75  }
76 
77  set_bounds(lb_v,ub_v);
78 
79  // Set spacecraft.
80  m_leg.set_spacecraft(kep_toolbox::sims_flanagan::spacecraft(m_initial_mass,0.3,3000));
81 }
82 
85 {
86  return base_ptr(new gtoc5_self_flyby(*this));
87 }
88 
91 {
92  f[0] = x[0]; //Time of Flight
93 }
94 
97 {
98  using namespace kep_toolbox;
99  // We set the leg.
100  const epoch epoch_source(m_mjd,epoch::MJD), epoch_target(m_mjd + x[0],epoch::MJD);
101  array3D v_source, r_source, v_target, r_target;
102  m_ast.eph(epoch_source,r_source,v_source);
103  m_ast.eph(epoch_target,r_target,v_target);
104  v_target[0] += x[2];
105  v_target[1] += x[3];
106  v_target[2] += x[4];
107  m_leg.set_leg(epoch_source,sc_state(r_source,v_source,m_leg.get_spacecraft().get_mass()),x.begin() + 5, x.begin() + 5 + m_n_segments * 3,
108  epoch_target,sc_state(r_target,v_target,x[1]),ASTRO_MU_SUN);
109 
110 
111  // We evaluate the state mismatch at the mid-point. And we use astronomical units to scale them
112  m_leg.get_mismatch_con(c.begin(), c.begin() + 7);
113  for (int i=0; i<3; ++i) c[i]/=ASTRO_AU;
114  for (int i=3; i<6; ++i) c[i]/=ASTRO_EARTH_VELOCITY;
115  c[6] /= m_leg.get_spacecraft().get_mass();
116 
117  // We evaluate the constraints on the throttles writing on the 7th mismatch constrant (mass is off)
118  m_leg.get_throttles_con(c.begin() + 7, c.begin() + 7 + m_n_segments);
119 
120  // Minimum flyby speed = 0.4 km/s.
121  c[7 + m_n_segments] = (160000. - (x[2] * x[2] + x[3] * x[3] + x[4] * x[4]));
122 }
123 
125 void gtoc5_self_flyby::set_sparsity(int &lenG, std::vector<int> &iGfun, std::vector<int> &jGvar) const
126 {
127  //Initial point
129  for (pagmo::decision_vector::size_type i = 0; i<x0.size(); ++i)
130  {
131  x0[i] = get_lb()[i] + (get_ub()[i] - get_lb()[i]) / 3.12345;
132  }
133  //Numerical procedure
134  estimate_sparsity(x0, lenG, iGfun, jGvar);
135 }
136 
137 std::string gtoc5_self_flyby::get_name() const
138 {
139  return "GTOC5 Flyby phase";
140 }
141 
142 std::string gtoc5_self_flyby::pretty(const decision_vector &x) const
143 {
144  using namespace kep_toolbox;
145  // We set the leg.
146  const epoch epoch_source(m_mjd,epoch::MJD), epoch_target(m_mjd + x[0],epoch::MJD);
147  array3D v_source, r_source, v_target, r_target;
148  m_ast.eph(epoch_source,r_source,v_source);
149  m_ast.eph(epoch_target,r_target,v_target);
150  v_target[0] += x[2];
151  v_target[1] += x[3];
152  v_target[2] += x[4];
153  m_leg.set_leg(epoch_source,sc_state(r_source,v_source,m_leg.get_spacecraft().get_mass()),x.begin() + 5, x.begin() + 5 + m_n_segments * 3,
154  epoch_target,sc_state(r_target,v_target,x[1]),ASTRO_MU_SUN);
155 
156  std::ostringstream oss;
157  oss << m_leg << '\n' << m_ast << '\n';
158  return oss.str();
159 }
160 
161 }}
162 
163 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::gtoc5_self_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
std::string pretty(const decision_vector &) const
A nice string representation of a chromosome.
Base problem class.
Definition: problem/base.h:148
size_type get_dimension() const
Return global dimension.
std::string get_name() const
Get problem's name.
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
void set_sparsity(int &, std::vector< int > &, std::vector< int > &) const
Implementation of the sparsity structure: automated detection.
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.
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Implementation of the constraint function.
const decision_vector & get_lb() const
Lower bounds getter.
base_ptr clone() const
Clone method.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
gtoc5_self_flyby(int=5, int=1, const double &=57023, const double &mass=4000, const double &=1E-5)
Constructor.