PaGMO  1.1.5
earth_planet.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/numeric/conversion/bounds.hpp>
29 #include <boost/numeric/conversion/cast.hpp>
30 #include <cmath>
31 #include <stdexcept>
32 #include <vector>
33 #include <keplerian_toolbox/sims_flanagan/codings.h>
34 #include <keplerian_toolbox/planet/jpl_low_precision.h>
35 
36 #include "../exceptions.h"
37 #include "../types.h"
38 #include "base.h"
39 #include "earth_planet.h"
40 
41 using namespace kep_toolbox;
42 using namespace kep_toolbox::sims_flanagan;
43 
44 namespace pagmo { namespace problem {
46 
47 earth_planet::earth_planet(int segments, std::string target, const double &ctol) : base(base_format(1,segments,1000).size(), 0, 1, 6 + segments + 1 +1, segments+2,ctol),
48  encoding(1,segments,1000), vmax(3000),n_segments(segments)
49 {
50  std::vector<double> lb_v(get_dimension());
51  std::vector<double> ub_v(get_dimension());
52 
53  //Start
54  lb_v[encoding.leg_start_epoch_i(0)[0]] = 0;
55  ub_v[encoding.leg_start_epoch_i(0)[0]] = 1000;
56 
57  //End
58  lb_v[encoding.leg_end_epoch_i(0)[0]] = 500;
59  ub_v[encoding.leg_end_epoch_i(0)[0]] = 1500;
60 
61  //Start Velocity
62  std::vector<int> tmp = encoding.leg_start_velocity_i(0);
63  for (std::vector<int>::size_type i = 0; i < tmp.size() ; ++i)
64  {
65  lb_v[tmp[i]] = -3;
66  ub_v[tmp[i]] = 3;
67  }
68 
69  //End Velocity
70  tmp = encoding.leg_end_velocity_i(0);
71  for (std::vector<int>::size_type i = 0; i < tmp.size() ; ++i)
72  {
73  lb_v[tmp[i]] = 0;
74  ub_v[tmp[i]] = 0;
75  }
76 
77  //I Throttles
78  for (int j = 0; j<encoding.n_segments(0); ++j)
79  {
80  tmp = encoding.segment_thrust_i(0,j);
81  for (std::vector<int>::size_type i = 0; i < tmp.size() ; ++i)
82  {
83  lb_v[tmp[i]] = -1;
84  ub_v[tmp[i]] = 1;
85  }
86  }
87 
88  set_bounds(lb_v,ub_v);
89 
90  //traj_fb constructor
91  std::vector<planet_ptr> sequence;
92  sequence.push_back(planet_ptr(new jpl_lp("earth")));
93  sequence.push_back(planet_ptr(new jpl_lp(target)));
94  trajectory = fb_traj(sequence,segments,1000,0.05,boost::numeric::bounds<double>::highest());
95 }
96 
99 {
100  return base_ptr(new earth_planet(*this));
101 }
102 
105 {
106  trajectory.init_from_full_vector(x.begin(),x.end(),encoding);
107  f[0] = trajectory.get_leg(0).evaluate_dv() / 1000;
108 }
109 
112 {
113  // We decode the decision vector into a multiple fly-by trajectory
114  trajectory.init_from_full_vector(x.begin(),x.end(),encoding);
115 
116  // We evaluate the state mismatch at the mid-point. And we use astronomical units to scale them
117  trajectory.evaluate_all_mismatch_con(c.begin(), c.begin() + 7);
118  for (int i=0; i<3; ++i) c[i]/=ASTRO_AU;
119  for (int i=3; i<6; ++i) c[i]/=ASTRO_EARTH_VELOCITY;
120 
121  // We evaluate the constraints on the throttles writing on the 7th mismatch constrant (mass is off)
122  trajectory.get_leg(0).get_throttles_con(c.begin() + 6, c.begin() + 6 + n_segments);
123 
124  // We evaluate the constraint on the initial launch velocity
125  c[6 + n_segments] = (trajectory.evaluate_leg_vinf2_i(0) - vmax*vmax) / ASTRO_EARTH_VELOCITY / ASTRO_EARTH_VELOCITY;
126 
127  // We evaluate the linear constraint on the epochs (tf > ti)
128  c[7 + n_segments] = trajectory.get_leg(0).get_t_i().mjd2000() - trajectory.get_leg(0).get_t_f().mjd2000();
129 }
130 
132 void earth_planet::set_sparsity(int &lenG, std::vector<int> &iGfun, std::vector<int> &jGvar) const
133 {
134  //Initial point
136  for (pagmo::decision_vector::size_type i = 0; i<x0.size(); ++i)
137  {
138  x0[i] = get_lb()[i] + (get_ub()[i] - get_lb()[i]) / 3.12345;
139  }
140  //Numerical procedure
141  estimate_sparsity(x0, lenG, iGfun, jGvar);
142 }
143 
144 std::string earth_planet::get_name() const
145 {
146  return "Earth-Planet";
147 }
148 
149 }}
150 
151 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::earth_planet)
Test problem kep tool.
Definition: earth_planet.h:47
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
void set_sparsity(int &, std::vector< int > &, std::vector< int > &) const
Implementation of the sparsity structure: automated detection.
Base problem class.
Definition: problem/base.h:148
size_type get_dimension() const
Return global dimension.
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.
std::string get_name() const
Get problem's name.
void objfun_impl(fitness_vector &, const decision_vector &) const
Implementation of the objective function.
const decision_vector & get_lb() const
Lower bounds getter.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
base_ptr clone() const
Clone method.
earth_planet(int=10, std::string="mars", const double &=1E-9)
Constructor.