eoVRPUtils.h

00001 /*
00002  * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00003  * (C) OPAC Team, LIFL, 2002-2007
00004  *
00005  * (c) Antonio LaTorre <atorre@fi.upm.es>, 2007
00006  *
00007  * This software is governed by the CeCILL license under French law and
00008  * abiding by the rules of distribution of free software.  You can  use,
00009  * modify and/ or redistribute the software under the terms of the CeCILL
00010  * license as circulated by CEA, CNRS and INRIA at the following URL
00011  * "http://www.cecill.info".
00012  *
00013  * As a counterpart to the access to the source code and  rights to copy,
00014  * modify and redistribute granted by the license, users are provided only
00015  * with a limited warranty  and the software's author,  the holder of the
00016  * economic rights,  and the successive licensors  have only  limited liability.
00017  *
00018  * In this respect, the user's attention is drawn to the risks associated
00019  * with loading,  using,  modifying and/or developing or reproducing the
00020  * software by the user in light of its specific status of free software,
00021  * that may mean  that it is complicated to manipulate,  and  that  also
00022  * therefore means  that it is reserved for developers  and  experienced
00023  * professionals having in-depth computer knowledge. Users are therefore
00024  * encouraged to load and test the software's suitability as regards their
00025  * requirements in conditions enabling the security of their systems and/or
00026  * data to be ensured and,  more generally, to use and operate it in the
00027  * same conditions as regards security.
00028  * The fact that you are presently reading this means that you have had
00029  * knowledge of the CeCILL license and that you accept its terms.
00030  *
00031  * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00032  * Contact: paradiseo-help@lists.gforge.inria.fr
00033  *
00034  */
00035 
00036 #ifndef eoVRPUtils_h
00037 #define eoVRPUtils_h
00038 
00039 // General includes
00040 #include <vector>
00041 #include <utility>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <sstream>
00045 #include <math.h>
00046 
00052 #define PI                   3.14159265
00053 
00060 #define VEHICLE_CAPACITY   200
00061 
00062 
00063 typedef std::vector<int> Route;
00064 typedef std::vector< Route > Routes;
00065 
00066 
00072 namespace eoVRPUtils {
00073 
00086 typedef struct ClientData {
00087 
00088     unsigned id;            
00089     double   x;             
00090     double   y;             
00091     double   demand;        
00092     double   readyTime;     
00093     double   dueTime;       
00094     double   serviceTime;   
00096 } ClientDataT;
00097 
00098 
00099 static std::vector <ClientDataT> clients;             
00100 static std::vector <std::vector <double> > dist;      
00108 void computeDistances () {
00109 
00110     unsigned numClients = clients.size ();
00111 
00112     dist.resize (numClients) ;
00113 
00114     for (unsigned i = 0; i < dist.size (); i ++)
00115         dist [i].resize (numClients);
00116 
00117     // Distances computation
00118     for (unsigned i = 0; i < dist.size (); i ++)
00119         for (unsigned j = i + 1 ; j < dist.size (); j ++) {
00120 
00121             double distX = clients [i].x - clients [j].x;
00122             double distY = clients [i].y - clients [j].y;
00123 
00124             dist [i][j] = dist [j][i] = sqrt (distX * distX + distY * distY);
00125 
00126         }
00127 
00128 }
00129 
00130 
00139 void getTimeWindow (unsigned _client, double& _readyTime, double& _dueTime, double& _serviceTime) {
00140 
00141     assert (_client >= 0 && _client < clients.size ());
00142 
00143     _readyTime = clients [_client].readyTime;
00144     _dueTime = clients [_client].dueTime;
00145     _serviceTime = clients [_client].serviceTime;
00146 
00147 }
00148 
00149 
00157 float distance (unsigned _from,  unsigned _to) {
00158 
00159     assert (_from >= 0 && _from < clients.size ());
00160     assert (_to   >= 0 && _to   < clients.size ());
00161 
00162     return dist [_from][_to];
00163 
00164 }
00165 
00166 
00174 float polarAngle (unsigned _from, unsigned _to) {
00175 
00176     assert (_from >= 0 && _from < clients.size ());
00177     assert (_to   >= 0 && _to   < clients.size ());
00178 
00179     double angle = atan2 (clients [_from].y - clients [_to].y,
00180                           clients [_from].x - clients [_to].x);
00181 
00182     // To convert it from radians to degrees
00183     angle *= 180 / PI;
00184 
00185     if (angle < 0)
00186         angle *= -1;
00187 
00188     return angle;
00189 
00190 }
00191 
00192 
00199 void load (const char* _fileName) {
00200 
00201     std::ifstream f (_fileName);
00202 
00203     if (f) {
00204 
00205         while (!f.eof ()) {
00206 
00207             ClientDataT client;
00208 
00209             f >> client.id;
00210             f >> client.x;
00211             f >> client.y;
00212             f >> client.demand;
00213             f >> client.readyTime;
00214             f >> client.dueTime;
00215             f >> client.serviceTime;
00216 
00217             clients.push_back (client);
00218 
00219         }
00220 
00221         f.close ();
00222 
00223         computeDistances ();
00224 
00225     }
00226     else {
00227 
00228         std::cerr << "Error: the file: " << _fileName << " doesn't exist !!!" << std::endl ;
00229         exit (1);
00230 
00231     }
00232 
00233 }
00234 
00235 
00241 void printRoute (const Route& _route) {
00242 
00243     std::cout << "[";
00244 
00245     for (unsigned i = 0; i < _route.size (); i++) {
00246 
00247         std::cout << _route [i];
00248 
00249         if (i != _route.size () -1)
00250             std::cout << ", ";
00251 
00252     }
00253 
00254     std::cout << "]";
00255 
00256 }
00257 
00258 
00264 void printRoutes (Routes& _routes) {
00265 
00266     std::cout << "[";
00267 
00268     for (unsigned i = 0; i < _routes.size (); i++) {
00269 
00270         std::cout << "[";
00271 
00272         for (unsigned j = 0; j < _routes [i].size (); j++) {
00273 
00274             std::cout << _routes [i][j];
00275 
00276             if (j != _routes [i].size () -1)
00277                 std::cout << ", ";
00278 
00279         }
00280 
00281         if (i == _routes.size () -1)
00282             std::cout << "]";
00283         else
00284             std::cout << "]," << std::endl;
00285     }
00286 
00287     std::cout << "]";
00288 
00289 }
00290 
00291 
00292 };
00293 
00294 #endif

Generated on Fri Dec 7 16:57:19 2007 for CVRP-TW by  doxygen 1.4.7