/* * Copyright (C) DOLPHIN Project-Team, Lille Nord-Europe, 2007-2008 * (C) OPAC Team, LIFL, 2002-2008 * * (c) Mostepha Redouane Khouadjia , 2008 * * This software is governed by the CeCILL license under French law and * abiding by the rules of distribution of free software. You can use, * modify and/ or redistribute the software under the terms of the CeCILL * license as circulated by CEA, CNRS and INRIA at the following URL * "http://www.cecill.info". * * As a counterpart to the access to the source code and rights to copy, * modify and redistribute granted by the license, users are provided only * with a limited warranty and the software's author, the holder of the * economic rights, and the successive licensors have only limited liability. * * In this respect, the user's attention is drawn to the risks associated * with loading, using, modifying and/or developing or reproducing the * software by the user in light of its specific status of free software, * that may mean that it is complicated to manipulate, and that also * therefore means that it is reserved for developers and experienced * professionals having in-depth computer knowledge. Users are therefore * encouraged to load and test the software's suitability as regards their * requirements in conditions enabling the security of their systems and/or * data to be ensured and, more generally, to use and operate it in the * same conditions as regards security. * The fact that you are presently reading this means that you have had * knowledge of the CeCILL license and that you accept its terms. * * ParadisEO WebSite : http://paradiseo.gforge.inria.fr * Contact: paradiseo-help@lists.gforge.inria.fr * */ #ifndef EOEASYPSODVRP_H_ #define EOEASYPSODVRP_H_ #include #include "eoPsoDVRPflight.h" #include "eoPsoDVRPvelocity.h" #include "eoPsoDVRPEvalFunc.h" #include "eoPsoDVRPEncodeSwarm.h" #include "eoDVRPSecondsElapsedContinue.h" #include "eoPsoDVRPInit.h" #include #include "eoGlobal.h" template class eoPeoEasyPsoDVRP:public eoPSO < POT > { public : /* eoPeoEasyPsoDVRP(eoPsoDVRPEncodeSwarm & _encoding, // eoEvalFunc< POT > &_eval, eoPsoDVRPEvalFunc &_evalDvrp, eoPsoDVRPvelocity < POT > &_velocity, eoFlight < POT > &_move, eoContinue &_continuator, eoParticleBestInit &_initBest ): encoding(_encoding), eval (_eval), loopEval(_eval), popEval(loopEval), velocity (_velocity), move (_move), continuator(_continuator), initBest(_initBest) {} */ eoPeoEasyPsoDVRP(eoPsoDVRPEncodeSwarm & _encoding, eoEvalFunc < POT > &_eval, eoPopEvalFunc < POT > &_popEval, eoPsoDVRPvelocity < POT > &_velocity, eoFlight < POT > &_move, eoContinue &_continuator, eoGenContinue &_genContinuator, eoParticleBestInit &_initBest ): encoding(_encoding), eval (_eval), loopEval(_eval), popEval(loopEval), velocity (_velocity), move (_move), continuator(_continuator), genContinuator(_genContinuator), initBest(_initBest) {} void operator()(eoPop < POT> &_pop) { double _TIME_STEP = TIME_STEP; try { eoPop empty_pop; do{ encoding.initParticleToItsBest(_pop); if(_TIME_STEP < TIME_CUTOFF * TIME_DAY){ if(_TIME_STEP==0) loadNewCustomers (TIME_DAY,TIME_CUTOFF * TIME_DAY); else loadNewCustomers(_TIME_STEP, TIME_SLICE); PrintLastCustomers(); encoding(_pop,_TIME_STEP,0); popEval(empty_pop, _pop); initBest.apply(_pop); velocity.getTopology().setup(_pop); } genContinuator.totalGenerations(genContinuator.totalGenerations()); do { velocity.apply(_pop); move.apply(_pop); popEval(empty_pop,_pop); velocity.updateNeighborhood(_pop); } while (genContinuator(_pop)); encoding.commitOrders(_pop,_TIME_STEP,TIME_SLICE); velocity.getTopology().setup(_pop); _TIME_STEP += TIME_SLICE ; cout<<_TIME_STEP<< " "< & encoding; eoEvalFunc < POT > &eval; eoPopLoopEval loopEval; eoPopEvalFunc& popEval; eoPsoDVRPvelocity < POT > &velocity; eoFlight < POT > &move; eoContinue< POT > &continuator; eoGenContinue < POT > & genContinuator; eoParticleBestInit &initBest; class eoDummyEval : public eoEvalFunc { public: void operator()(POT & _po) {} }dummyEval; }; void print(eoPop &pop,std::ostream &_os) { for (size_t i =0, size = pop.size(); i< size ; ++i) { _os < &pop,std::ostream &_os) { for (size_t i =0, size = pop.size(); i< size ; ++i) { _os< &pop, std::ostream &_os) { for (size_t i =0, size = pop.size(); i< size ; ++i) { _os < &topology, unsigned _seed, std::ostream &_os) { _os < &pop, std::ostream &_os) { for (size_t i =0, size = pop.size(); i & _pop) { for(size_t i = 0 , size = _pop.size(); i < size; ++i) closeTours(_pop[i]); } void resetSwarmToBestParticule(eoPop& _pop, eoParticleDVRP _po ) { for(size_t i = 0 , size = _pop.size() ; i < size ; i++) _pop[i] = _po ; } void HillClimbingSearch( eoParticleDVRP & _po, unsigned sizeNeighboor = 1 ) { Routes neighborhoodSolution ; double depotTimeWindow = clients[0].durationService; double dueTime ; bool validNeighborSolution ; neighborhoodSolution = _po.pRoutes ; validNeighborSolution = false; for( size_t tr = 0; tr < neighborhoodSolution.size(); tr++) { if(_po.serviceIsProgressCurrentPosition(tr)) { unsigned lastServedCustomer = _po.IndexLastServedCustomerCurrentPosition(tr) ; unsigned positionLastCustomer = _po.planifiedCustomers[lastServedCustomer].pRouting.routePosition -1; swapCustomers(neighborhoodSolution[tr],positionLastCustomer); dueTime = getTimeOfService(neighborhoodSolution[tr], _po.planifiedCustomers[lastServedCustomer].id,_po.planifiedCustomers[lastServedCustomer].pRouting.serviceTime) + distance(dist, neighborhoodSolution[tr].back(),0); }else { swapCustomers(neighborhoodSolution[tr], 0 ); dueTime = getTimeOfService(neighborhoodSolution[tr], 0 , _po.firstTimeServiceCurrentPosition[tr]) + distance(dist, neighborhoodSolution[tr].back(),0); } if (dueTime <= depotTimeWindow) { validNeighborSolution = true ; break ; } } if(validNeighborSolution) { double neighboorFitness = computTourLength(neighborhoodSolution); // std::cout<< " _po.fitness() " << _po.fitness() << " neighboorFitness "<< neighboorFitness << endl ; if( neighboorFitness < _po.fitness() ) { _po.pRoutes = neighborhoodSolution; _po.reDesign() ; _po.fitness(neighboorFitness); } } } void HillClimbingSearch(eoPop& _pop, unsigned sizeNeighboor = 1 ) { for(size_t i = 0, size = _pop.size(); i < size ; i ++ ) HillClimbingSearch(_pop[i], sizeNeighboor) ; } bool TwoOptimalAlgorithm (eoParticleDVRP & _po) { unsigned lastServedCustomerPosition; bool twoOptFeasible, changehappen = false ; double dueTime, depotTimeWindow = clients[0].durationService; for (size_t tr = 0, size = _po.pRoutes.size() ; tr < size ; tr++) { Route routeOpt = _po.pRoutes[tr]; twoOptFeasible =false; if(_po.serviceIsProgressCurrentPosition(tr)) { unsigned lastServedCustomer = _po.IndexLastServedCustomerCurrentPosition(tr) ; lastServedCustomerPosition = _po.planifiedCustomers[lastServedCustomer].pRouting.routePosition -1; if (twoOptOnRoute(routeOpt,lastServedCustomerPosition)) { dueTime = getTimeOfService(routeOpt, _po.planifiedCustomers[lastServedCustomer].id,_po.planifiedCustomers[lastServedCustomer].pRouting.serviceTime) + distance(dist, routeOpt.back(),0); twoOptFeasible = true; } } else { lastServedCustomerPosition = 0; if (twoOptOnRoute(routeOpt,lastServedCustomerPosition)) { dueTime = getTimeOfService(routeOpt, 0 , _po.firstTimeServiceCurrentPosition[tr]) + distance(dist, routeOpt.back(),0); twoOptFeasible = true; } } if (twoOptFeasible && dueTime <= depotTimeWindow) {_po.pRoutes[tr] = routeOpt; changehappen = true; } } if (changehappen) { _po.reDesign() ; _po.invalidate(); return true; } return false; } void TwoOptimalAlgorithm (eoPop & _pop) { for(size_t i = 0, size = _pop.size(); i < size ; i++) TwoOptimalAlgorithm(_pop[i]); } bool ThreeOptimalAlgorithm (eoParticleDVRP & _po) { unsigned lastServedCustomerPosition; bool threeOptFeasible, changehappen = false ; double dueTime, depotTimeWindow = clients[0].durationService; for (size_t tr = 0, size = _po.pRoutes.size() ; tr < size ; tr++) { Route routeOpt = _po.pRoutes[tr]; threeOptFeasible =false; if(_po.serviceIsProgressCurrentPosition(tr)) { unsigned lastServedCustomer = _po.IndexLastServedCustomerCurrentPosition(tr) ; lastServedCustomerPosition = _po.planifiedCustomers[lastServedCustomer].pRouting.routePosition -1; if (threeOptOnRoute(routeOpt,lastServedCustomerPosition)) { dueTime = getTimeOfService(routeOpt, _po.planifiedCustomers[lastServedCustomer].id,_po.planifiedCustomers[lastServedCustomer].pRouting.serviceTime) + distance(dist, routeOpt.back(),0); threeOptFeasible = true; } } else { lastServedCustomerPosition = 0; if (threeOptOnRoute(routeOpt,lastServedCustomerPosition)) { dueTime = getTimeOfService(routeOpt, 0 , _po.firstTimeServiceCurrentPosition[tr]) + distance(dist, routeOpt.back(),0); threeOptFeasible = true; } } if (threeOptFeasible && dueTime <= depotTimeWindow) {_po.pRoutes[tr] = routeOpt; changehappen = true; } } if (changehappen) { _po.reDesign() ; _po.invalidate(); return true; } return false; } void ThreeOptimalAlgorithm (eoPop & _pop) { for(size_t i = 0, size = _pop.size(); i < size ; i++) ThreeOptimalAlgorithm(_pop[i]); } void VariableNeighborhoodSearch(eoParticleDVRP & _po, unsigned neighborSize) { Routes neighborhoodSolution ; double dueTime, depotTimeWindow = clients[0].durationService; bool validNeighborSolution ; neighborhoodSolution = _po.pRoutes ; validNeighborSolution = false; unsigned neighborhoodIndex = 1; for( size_t tr = 0; tr < neighborhoodSolution.size(); tr++) { if(_po.serviceIsProgressCurrentPosition(tr)) { unsigned lastServedCustomer = _po.IndexLastServedCustomerCurrentPosition(tr) ; unsigned positionLastCustomer = _po.planifiedCustomers[lastServedCustomer].pRouting.routePosition -1; for(size_t k = 0 ; k <= neighborhoodIndex ; k++) swapCustomers(neighborhoodSolution[tr],positionLastCustomer); dueTime = getTimeOfService(neighborhoodSolution[tr], _po.planifiedCustomers[lastServedCustomer].id,_po.planifiedCustomers[lastServedCustomer].pRouting.serviceTime) + distance(dist, neighborhoodSolution[tr].back(),0); }else { for(size_t k = 0 ; k <= neighborhoodIndex ; k++) swapCustomers(neighborhoodSolution[tr], 0); dueTime = getTimeOfService(neighborhoodSolution[tr], 0 , _po.firstTimeServiceCurrentPosition[tr]) + distance(dist, neighborhoodSolution[tr].back(),0); } if (dueTime <= depotTimeWindow) { double neighboorFitness = computTourLength(neighborhoodSolution); if( neighboorFitness < _po.fitness() ) { _po.pRoutes = neighborhoodSolution; _po.reDesign() ; _po.fitness(neighboorFitness); if(TwoOptimalAlgorithm(_po)) { _po.computCurrentTourLength(); _po.fitness(_po.toursLength()); neighborhoodIndex = 1; continue ; } } } neighborhoodIndex = (neighborhoodIndex + 1) % neighborSize ; } } void VariableNeighborhoodSearch(eoPop& _pop, unsigned neighborSize) { for(size_t i = 0, size = _pop.size(); i < size ; i++) VariableNeighborhoodSearch(_pop[i], neighborSize); } double avragePopFitness(eoPop& _pop) { double average = 0.0; for(size_t i = 0, size = _pop.size(); i < size ; i ++) { average += _pop[i].best(); } return (average/_pop.size()); } bool AllCustomersAttributed(eoDVRPStarTopology &topology) { size_t i = 0; while (i < topology.best().size()) { if(!topology.best().planifiedCustomers[i].bestRouting.is_served) return false; i++; } return true; } #endif /*EOEASYPSODVRP_H_*/