PaGMO  1.1.5
gtoc_2.cpp
1 /*****************************************************************************
2 * Copyright (C) 2004-2009 The PaGMO development team, *
3 * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4 * http://apps.sourceforge.net/mediawiki/pagmo *
5 * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Developers *
6 * http://apps.sourceforge.net/mediawiki/pagmo/index.php?title=Credits *
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 <algorithm>
26 #include <functional>
27 #include <vector>
28 #include <string>
29 #include <sstream>
30 #include <keplerian_toolbox/astro_constants.h>
31 
32 #include "gtoc_2.h"
33 #include "../exceptions.h"
34 #include "../types.h"
35 #include "base.h"
36 
37 
38 using namespace kep_toolbox;
39 using namespace kep_toolbox::sims_flanagan;
40 
41 namespace pagmo { namespace problem {
42 
44 
60 gtoc_2::gtoc_2(int ast1, int ast2, int ast3, int ast4, int n_seg, objective obj):
61  base(12 * n_seg + 15,0,1,7 * 4 + n_seg * 4 + 1, n_seg * 4 + 1, 1E-3),
62  m_n_seg(n_seg),m_spacecraft(1500.,.1,4000.),m_obj(obj)
63 {
64  if (n_seg <= 0) {
65  pagmo_throw(value_error,"invalid number of segments");
66  }
67  m_asteroids.push_back(planet::gtoc2(910));
68  m_asteroids.push_back(planet::gtoc2(ast1));
69  m_asteroids.push_back(planet::gtoc2(ast2));
70  m_asteroids.push_back(planet::gtoc2(ast3));
71  m_asteroids.push_back(planet::gtoc2(ast4));
72  // Build legs.
73  for (int i = 0; i < 4; ++i) {
74  m_legs.push_back(leg());
75  m_legs.back().set_spacecraft(m_spacecraft);
76  m_legs.back().set_mu(1.32712440018e20);
77  m_legs.back().set_throttles_size(n_seg);
78  }
79  // Check that input asteroids belong to different groups.
80  int asteroid_groups[4] = {m_asteroids[1].get_group(), m_asteroids[2].get_group(),
81  m_asteroids[3].get_group(), m_asteroids[4].get_group()};
82  std::sort(asteroid_groups,asteroid_groups + 4);
83  if (std::unique(asteroid_groups,asteroid_groups + 4) - asteroid_groups != 4) {
84  pagmo_throw(value_error,"asteroids must belong to different groups");
85  }
86  if (std::find(asteroid_groups,asteroid_groups + 4,5) != asteroid_groups + 4) {
87  pagmo_throw(value_error,"the Earth cannot appear in the list of asteroids");
88  }
89  decision_vector lb_v, ub_v;
90  // Launch date.
91  lb_v.push_back(57023.5);
92  ub_v.push_back(64693.5);
93  // Flight and waiting times.
94  for (int i = 0; i < 3; ++i) {
95  // Flight time.
96  lb_v.push_back(5);
97  ub_v.push_back(3600);
98  // Waiting time.
99  lb_v.push_back(90.0);
100  ub_v.push_back(90.0000001);
101  }
102  // Final transfer time.
103  lb_v.push_back(5);
104  ub_v.push_back(3600);
105  // Masses.
106  for (int i = 0; i < 4; ++i) {
107  lb_v.push_back(100);
108  ub_v.push_back(1500);
109  }
110  // Vinf cartesian km/s.
111  for (int i = 0; i < 3; ++i) {
112  lb_v.push_back(-3.5);
113  ub_v.push_back(3.5);
114  }
115  // Throttles.
116  for (int i = 0; i < 12 * n_seg; ++i) {
117  lb_v.push_back(-1);
118  ub_v.push_back(1);
119  }
120  set_bounds(lb_v,ub_v);
121 }
122 
124 {
125  return base_ptr(new gtoc_2(*this));
126 }
127 
129 {
130  // Objective function is m_f / t_f.
131  switch (m_obj){
132  case MASS:
133  f[0] = - x[11];
134  break;
135  case TIME:
136  f[0] = (std::accumulate(x.begin() + 1,x.begin() + 8,0.) * ASTRO_DAY2YEAR);
137  break;
138  case MASS_TIME:
139  f[0] = - x[11] / (std::accumulate(x.begin() + 1,x.begin() + 8,0.) * ASTRO_DAY2YEAR);
140  }
141 }
142 
144 {
145  // Cached values.
146  array3D r, v;
147  double initial_mass = m_spacecraft.get_mass();
148  double final_mass = x[8];
149  // Build legs.
150  for (int i = 0; i < 4; ++i) {
151  // Calculate start-end leg epochs.
152  epoch start(std::accumulate(x.begin(),x.begin() + 2 * i + 1, 0.),epoch::MJD),
153  end(std::accumulate(x.begin(),x.begin() + 2 * i + 2, 0.),epoch::MJD);
154  // Set leg's start-end epochs.
155  m_legs[i].set_t_i(start);
156  m_legs[i].set_t_f(end);
157  // Initial state.
158  m_asteroids[i].eph(start,r,v);
159  // First leg: Vinf is added to the state.
160  if (i == 0) {
161  for (int j = 0; j < 3; ++j) {
162  v[j] += x[12 + j] * 1000;
163  }
164  }
165  m_legs[i].set_x_i(sc_state(r,v,initial_mass));
166  // Throttles.
167  for (int j = 0; j < m_n_seg; ++j) {
168  m_legs[i].set_throttles(j,get_nth_throttle(j,x.begin() + 15 + 3 * i * m_n_seg,start,end));
169  }
170  // Final state.
171  m_asteroids[i + 1].eph(end,r,v);
172  m_legs[i].set_x_f(sc_state(r,v,final_mass));
173  // Update masses.
174  initial_mass = final_mass;
175  // Do not update mass if final iteration (useless).
176  if (i != 3) {
177  final_mass = x[9 + i];
178  }
179  }
180  // Load state mismatches into constraints vector.
181  for (int i = 0; i < 4; ++i) {
182  m_legs[i].get_mismatch_con(c.begin() + 7 * i, c.begin() + 7 * (i + 1));
183  // Passing non-dimensional units to the solver.
184  for (int j = 0; j < 3; ++j) {
185  c[7 * i + j] /= ASTRO_AU;
186  c[7 * i + j + 3] /= ASTRO_EARTH_VELOCITY;
187  }
188  c[7*i+6] /=1500;
189  }
190  // Throttles constraints.
191  for (int i = 0; i < 4; ++i) {
192  m_legs[i].get_throttles_con(c.begin() + 28 + i * m_n_seg, c.begin() + 28 + (i + 1) * m_n_seg);
193  }
194  // Vinf constraint.
195  c.back() = (x[12] * x[12] + x[13] * x[13] + x[14] * x[14] - 3.5 * 3.5) / (ASTRO_EARTH_VELOCITY * ASTRO_EARTH_VELOCITY) * 1000000;
196 }
197 
199 //void gtoc_2::set_sparsity(int &lenG, std::vector<int> &iGfun, std::vector<int> &jGvar) const
200 // {
201 // //Numerical procedure
202 // estimate_sparsity(lenG, iGfun, jGvar);
203 // }
204 
205 std::string gtoc_2::get_name() const
206 {
207  return "GTOC_2";
208 }
209 
210 std::string gtoc_2::human_readable_extra() const {
211  std::ostringstream oss;
212  oss << "\n\tAsteroid Sequence:\t\t\t";
213  for (int i =1 ; i< 5; ++i ) oss << m_asteroids[i].get_name() << " ";
214  oss << std::endl;
215  return oss.str();
216 }
217 
219 
222 std::string gtoc_2::pretty(const std::vector<double> &x) const
223 {
224  std::ostringstream s;
225  //We start by filling up the m_legs with the correct information
227  this->compute_constraints_impl(c,x);
228  s << "Final Mass: " << x[11] << " Kg" << std::endl;
229  s << "Total Time: " << std::accumulate(x.begin() + 1,x.begin() + 8,0.) * ASTRO_DAY2YEAR << " Years" << std::endl;
230  s << "Objective Function: " << x[11] / ( std::accumulate(x.begin() + 1,x.begin() + 8,0.) * ASTRO_DAY2YEAR ) << " Kg/Years" << std::endl;
231  for (int i=0; i<4;++i) s << this->m_legs[i] << std::endl;
232  s << std::endl;
233 
234  return s.str();
235 }
236 
237 }} // namespaces
238 
239 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::gtoc_2)
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 problem class.
Definition: problem/base.h:148
std::string get_name() const
Implementation of the sparsity structure: automated detection.
Definition: gtoc_2.cpp:205
void objfun_impl(fitness_vector &, const decision_vector &) const
Objective function implementation.
Definition: gtoc_2.cpp:128
GTOC_2 Low-Thrust Multiple Asteroid Randezvous Problem.
Definition: gtoc_2.h:53
objective
The objective function can be defined as final mass, final time or mass/time.
Definition: gtoc_2.h:57
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
c_size_type get_c_dimension() const
Return global constraints dimension.
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
base_ptr clone() const
Clone method.
Definition: gtoc_2.cpp:123
gtoc_2(int=815, int=300, int=110, int=47, int=10, objective=MASS_TIME)
Constructor.
Definition: gtoc_2.cpp:60
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.
std::string pretty(const std::vector< double > &x) const
A pretty description of the chromosome.
Definition: gtoc_2.cpp:222
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
Implementation of constraint computation.
Definition: gtoc_2.cpp:143
std::string human_readable_extra() const
Extra information in human readable format.
Definition: gtoc_2.cpp:210