Skip to content

Instantly share code, notes, and snippets.

@zibon
Created July 6, 2013 11:23
Show Gist options
  • Save zibon/5939625 to your computer and use it in GitHub Desktop.
Save zibon/5939625 to your computer and use it in GitHub Desktop.
// Structure for Point, Geo coordinates can be represented with it
typedef struct
{
double X, Y;
}Point;
// Structure to keep cost, distance and traveltime. If distance/ traveltime is missing, there may be a negative flag
typedef struct
{
double cost, distance, traveltime;
}CostPack;
// Class for holding vehicle information which consist of capacity, and cost_per_km
// For first version we will use homogeneous cost
class CVehicleInfo
{
public:
CVehicleInfo();
~CVehicleInfo();
bool init();
bool loadUnit(int lUnit);
bool unloadUnit(int lUnit);
int getCapacity();
void setCapacity(int capacity);
int getId();
void setId(int id);
double getCostPerKM();
void setCostPerKM(double cost);
CVehicleInfo( CVehicleInfo const& );
CVehicleInfo& operator = (const CVehicleInfo& vehicleInfo);
private:
int m_iCapacity;
int m_iCurrentLoad;
int m_iVehicleId;
double m_dCostPerKM;
};
// Class to represent Orders. Each order is consist of open_time, close_time and sevice_time, its location and number of units ordered.
class COrderInfo
{
public:
COrderInfo();
~COrderInfo();
int getOpenTime();
void setOpenTime(int openTime);
int getCloseTime();
void setCloseTime(int closeTime);
int getServiceTime();
void setServiceTime(int serviceTime);
int getOrderUnit();
void setOrderUnit(int orderUnit);
Point getOrderLocation();
void setOrderLocation(Point location);
COrderInfo( COrderInfo const& );
COrderInfo& operator = (const COrderInfo& solution);
private:
int m_iOrderOpenTime;
int m_iOrderCloseTime;
int m_iOrderServiceTime;
int m_iOrderUnitCount;
int m_iOrderId;
Point m_ptOrderLocation;
};
// Class to represent Depot information. Each depot will have it's Open_Time and Close_Time. The Depot that will open earliest will have open time 0
// and all other time will be normalized with respect to it. For the first version there will be only one depot
class CDepotInfo
{
public:
CDepotInfo();
~CDepotInfo();
int getOpenTime();
void setOpenTime(int openTime);
int getCloseTime();
void setCloseTime(int closeTime);
int getDepotId();
void setDepotId(int id);
Point getDepotLocation();
void setDepotLocation(Point location);
CDepotInfo( CDepotInfo const& );
CDepotInfo& operator = (const CDepotInfo& solution);
private:
int m_iDepotOpenTime;
int m_iDepotClosTime;
int m_iDepotId;
Point m_ptDepotLocation;
};
// Class to represent information of a Tour. A Tour starts from a depot and ends in a depot. On the way it serves several orders.
// Each Tour has a vehicle ID and the list of Orders it serves in appropriate order. It also has the total Distance, Cost and Time assciated.
class CTourInfo
{
public:
CTourInfo();
~CTourInfo();
init();
int getVehicleId();
void setVehicleId(int vehicleId);
int getStartDepot();
void setStartDepot(int depotId);
int getEndDepot();
void setEndDepot(int depotId);
bool addCustomer(int customerId);
std::vector<int> getOrderVecotor();
CTourInfo( CTourInfo const& );
CTourInfo& operator = (const CTourInfo& solution);
private:
int m_iVehicleId;
int m_iStartDepotId;
int m_iEndDepotId;
int m_iOrdersServed;
std::vector<int> m_viOrderIds;
double m_dTotalCost;
double m_dTotalDistance;
double m_dTotalTraveltime;
};
// This class will represent a solution of a VRP problem. A solution will be consist of multiple tour.
// It also contains the number of vehicle used, number of orders served and total cost, distance and traveltime.
class CSolutionInfo
{
public:
CSolutionInfo();
~CSolutionInfo();
int getVehicleUsed();
bool addTour(CTourInfo& tour);
double getTotalCost();
double getTotalDistance();
double getTotalTravelTime();
std::vector<CToutInfo> getTourInfoVector();
CTourInfo( CTourInfo const& );
CTourInfo& operator = (const CTourInfo& solution);
private:
std::vector<CTourInfo> m_vtourAll;
int m_iVehicleUsed;
int m_iOrdersServed;
int m_iTotalOrders;
double m_dTotalCost;
double m_dTotalDistance;
double m_dTotalTraveltime;
}
// This is the main class that will solve the VRP problem. It will use the previous classes to represent the problem and the solution.
// It will also have pre generated point to point distance/ cost/ traveltime information in maps.
class CVRPSolver
{
public:
CVRPSolver();
~CVRPSolver();
bool init();
bool addDepot(CDepotInfo depotInfo);
bool addOrder(COrderInfo orderInfo);
bool addVehicle(CVehicleInfo vehicleInfo);
CostPack getOrderToOrderCost(int firstOrder, int secondOrder);
CostPack getDepotToOrderCost(int depotId, int orderId);
CostPack getOrderToOrderCost(int depotId, int orderId);
bool addOrderToOrderCost(int firstOrder, int secondOrder, CostPack cost);
bool addDepotToOrderCost(int depotId, int orderId, CostPack cost);
bool addOrderToOrderCost(int depotId, int orderId, CostPack cost);
bool solveVRP(std::string& strError);
bool getSolution(CSolution *solution, std::string& strError);
private:
bool m_bIsReadyToSolve;
std::vector<CVehicleInfo> m_vVehicleInfos;
std::vector<COrderInfo> m_vOrderInfos;
std::vector<CDepotInfo> m_vDepotInfos;
std::map<int, int> m_mapCustomerIdToIndex;
std::map<int, int> m_mapVehicleIdToIndex;
std::map<int, int> m_mapDepotIdToIndex;
std::map<pair<int, int>, CostPack> m_mapOrderToOrderCost;
std::map<pair<int, int>, CostPack> m_mapDepotToOrderrCost;
std::map<pair<int, int>, CostPack> m_mapOrderToDepotCost;
bool m_bIsSoultionReady;
CSolutionInfo m_solutionFinal;
}
@woodbri
Copy link

woodbri commented Jul 6, 2013

Razequl, This looks fine but it is not obvious (to me) how these will get used. I'm thinking that you might want to document in another gist or your wiki page or in comments what a typical high level use would be. Something like:

CVRPSolver vrp;
vrp.init();

CDepotInfo depot;
...

vrp.addDepot(depot);

vrp.addOrder(order1);
vrp.addOrder(order2);
vrp.addOrder(order3);

vrp.addVehicle(vehicle1);
vrp.addVehicle(vehicle2);

if (vrp.solveVRP(errStr)) {
   if (vrp.getSolution(...)) {
      // print solution
   }
   else {
      // print error message
   }
}
else {
   // print error message
}

// clean up
...

This seems to make sense and is a little more obvious now that I wrote out the code above.

@zibon
Copy link
Author

zibon commented Jul 7, 2013

Hi Steve,
Your idea of implementation is perfect. I have the same idea for implementation. Besides depot, order and vehicle, we also need to populate the costs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment