PaGMO  1.1.5
gtoc5_rendezvous.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_rendezvous.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_rendezvous::gtoc5_rendezvous(int segments, int source, int target, const double &lb_epoch, const double &initial_mass, const double &ctol):
50  base(segments * 3 + 3, 0, 1, 7 + segments, segments,ctol),
51  m_n_segments(segments),m_source(source),m_target(target),m_lb_epoch(lb_epoch),m_initial_mass(initial_mass)
52 {
53  std::vector<double> lb_v(get_dimension());
54  std::vector<double> ub_v(get_dimension());
55 
56  // Start (MJD).
57  lb_v[0] = m_lb_epoch;
58  ub_v[0] = m_lb_epoch + 356.25 * 10;
59 
60  // Leg duration.
61  lb_v[1] = 10;
62  ub_v[1] = 365.25 * 4;
63 
64  // Final mass.
65  lb_v[2] = 500;
66  ub_v[2] = m_initial_mass;
67 
68  // I Throttles
69  for (int i = 3; i < segments * 3 + 3; ++i)
70  {
71  lb_v[i] = -1;
72  ub_v[i] = 1;
73  }
74 
75  //Copying the lb,ub vector into the problems bounds
76  set_bounds(lb_v,ub_v);
77 
78  // Set spacecraft.
79  m_leg.set_spacecraft(kep_toolbox::sims_flanagan::spacecraft(m_initial_mass,0.3,3000));
80 }
81 
84 {
85  return base_ptr(new gtoc5_rendezvous(*this));
86 }
87 
90 {
91  f[0] = -x[2];
92 }
93 
96 {
97  using namespace kep_toolbox;
98  // We set the leg.
99  const epoch epoch_i(x[0],epoch::MJD), epoch_f(x[1] + x[0],epoch::MJD);
100  array3D v0, r0, vf, rf;
101  m_source.eph(epoch_i,r0,v0);
102  m_target.eph(epoch_f,rf,vf);
103  m_leg.set_leg(epoch_i,sc_state(r0,v0,m_leg.get_spacecraft().get_mass()),x.begin() + 3, x.end(),epoch_f,sc_state(rf,vf,x[2]),ASTRO_MU_SUN);
104 
105  // We evaluate the state mismatch at the mid-point. And we use astronomical units to scale them
106  m_leg.get_mismatch_con(c.begin(), c.begin() + 7);
107  for (int i=0; i<3; ++i) c[i]/=ASTRO_AU;
108  for (int i=3; i<6; ++i) c[i]/=ASTRO_EARTH_VELOCITY;
109  c[6] /= m_leg.get_spacecraft().get_mass();
110  // We evaluate the constraints on the throttles writing on the 7th mismatch constrant (mass is off)
111  m_leg.get_throttles_con(c.begin() + 7, c.begin() + 7 + m_n_segments);
112 }
113 
115 void gtoc5_rendezvous::set_sparsity(int &lenG, std::vector<int> &iGfun, std::vector<int> &jGvar) const
116 {
117  //Initial point
119  for (pagmo::decision_vector::size_type i = 0; i<x0.size(); ++i)
120  {
121  x0[i] = get_lb()[i] + (get_ub()[i] - get_lb()[i]) / 3.12345;
122  }
123  //Numerical procedure
124  estimate_sparsity(x0, lenG, iGfun, jGvar);
125 }
126 
127 std::string gtoc5_rendezvous::get_name() const
128 {
129  return "GTOC5 Rendezvous phase";
130 }
131 
132 std::string gtoc5_rendezvous::pretty(const decision_vector &x) const
133 {
134  using namespace kep_toolbox;
135  // We set the leg.
136  const epoch epoch_i(x[0],epoch::MJD), epoch_f(x[1] + x[0],epoch::MJD);
137  array3D v0, r0, vf, rf;
138  m_source.eph(epoch_i,r0,v0);
139  m_target.eph(epoch_f,rf,vf);
140  m_leg.set_leg(epoch_i,sc_state(r0,v0,m_leg.get_spacecraft().get_mass()),x.begin() + 3, x.end(),epoch_f,sc_state(rf,vf,x[2]),ASTRO_MU_SUN);
141 
142  std::ostringstream oss;
143  oss << m_leg << '\n' << m_source << '\n' << m_target << '\n';
144  return oss.str();
145 }
146 
147 }}
148 
149 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::gtoc5_rendezvous);
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
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
base_ptr clone() const
Clone method.
gtoc5_rendezvous(int=10, int=1, int=2, const double &=57023, const double &=4000, const double &=1E-5)
Constructor.
Base problem class.
Definition: problem/base.h:148
void set_sparsity(int &, std::vector< int > &, std::vector< int > &) const
Implementation of the sparsity structure: automated detection.
size_type get_dimension() const
Return global dimension.
std::string pretty(const decision_vector &) const
A nice string representation of a chromosome.
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Implementation of the constraint function.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
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.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
std::string get_name() const
Get problem's name.