-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
223 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,47 @@ | ||
#ifndef _ROUTE_H | ||
#define _ROUTE_H | ||
#include <iostream> | ||
#include <list> | ||
#include <vector> | ||
#include "data/prob_input.h" | ||
|
||
class Route { | ||
public: | ||
Route(int d, int v_ind, bool ex, const std::vector<int> o): | ||
day(d), vehicle_index(v_ind), exc_list(ex), orders(o) { } | ||
Route(int oid, int d, int vi, bool ex): | ||
day(d), vehicle(vi), exc_list(ex), orders(1, oid) { } | ||
Route(std::vector<int> ov): | ||
day(0), vehicle(0), exc_list(true), orders(ov) { } | ||
int size() const { return orders.size(); } | ||
int length(cost ProbInput&) const; | ||
unsigned get_day() const { return day; } | ||
unsigned get_vehicle() const { return vehicle; } | ||
bool IsExcList() const { return exc_list; } | ||
void AddOrder(int order_index) { orders.push_back(order_index); } | ||
const int& operator[] (int i) const { return orders[i]; } | ||
int& operator[] (int i) { return orders[i]; } | ||
private: | ||
int day, vehicle_index; | ||
int day, vehicle; | ||
bool exc_list; | ||
std::vector<int> orders; | ||
}; | ||
|
||
class RoutePlan { | ||
public: | ||
RoutePlan(const ProbInput& pi): in(pi) { } | ||
void AddRoute(unsigned d, const Route &r) { | ||
assert(d < plan.size()); | ||
plan[d].push_back(r); | ||
} | ||
RoutePlan(const ProbInput& pi): in(pi) { Allocate(); } | ||
void AddOrder(int , unsigned, unsigned, bool); | ||
void AddRoute(const Route &r) { routes.push_back(r); } | ||
int size() const { return routes.size(); } | ||
int get_est(int route, int order) const { return timetable[route][order]; } | ||
const Route& operator[] (int i) const { return routes[i]; } | ||
Route& operator[] (int i) const { return routes[i]; } | ||
const int& operator() (int veh, int day) const { return plan[veh][day]; } | ||
int& operator() (int veh, int day) { return plan[veh][day]; } | ||
// check whether a route plan is feasible | ||
bool CheckFeasibility(); | ||
private: | ||
void Allocate(); | ||
ProbInput ∈ | ||
std::vector<std::vector<Route>> plan; | ||
std::vector<Route> routes; | ||
std::vector<std::vector<int>> timetable; | ||
std::vector<std::vector<int>> plan; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,78 @@ | ||
#ifndef _BILLING_COST_COMPONENT_H_ | ||
#define _BILLING_COST_COMPONENT_H_ | ||
#include <iostream> | ||
#include <string> | ||
#include "data/prob_input.h" | ||
#include "data/route.h" | ||
|
||
class BillingCostComponent { | ||
public: | ||
BillingCostComponent(const ProbInput &i, int w, string n): | ||
in(i), weight(w), name(n) { } | ||
virtual int ComputeCost(const Route &r) const = 0; | ||
int Cost(const Route &r) const { | ||
return weight * ComputeCost(r, index_route); | ||
} | ||
string Name() const { return name; } | ||
virtual void PrintViolations(const Route &r, unsigned route_index, | ||
std::ostream &os = std::cout); | ||
void SetWeight(const unsigned w) { weight = w; } | ||
int Weight() const { return weight; } | ||
virtual ~BillingCostComponent() {} | ||
protected: | ||
const ProbInput ∈ | ||
const string name; | ||
int weight; | ||
}; | ||
|
||
class DistanceBillingCostComponent: public BillingCostComponent { | ||
public: | ||
DistanceBillingCostComponent(const ProbInput &in, int weight): | ||
BillingCostComponent(in, weight, "DistanceBillingCostComponent") {} | ||
int ComputeCost(const Route &r) const; | ||
}; | ||
|
||
|
||
class LoadFarestClientCostComponent: public BillingCostComponent { | ||
public: | ||
LoadFarestClientCostComponent(const ProbInput &in, int weight): | ||
BillingCostComponent(in, weight, "LoadFarestClientCostComponent") | ||
pair<unsigned, unsigned> MaxRateLoad(const Route &r) const; | ||
int ComputeCost(const Route &r) const; | ||
}; | ||
|
||
|
||
// DistanceLoadBillingCostComponent | ||
// This class compute the cost dependent on the load and on the distance. | ||
class DistanceLoadBillingCostComponent: public BillingCostComponent { | ||
public: | ||
DistanceLoadBillingCostComponent(const ProbInput &in, int weight): | ||
BillingCostComponent(in, weight, "DistanceLoadBillingCostComponent") {} | ||
pair<unsigned, unsigned> MaxRateLoad(const Route &r) const; | ||
bool IsFull(const Route &r) const; | ||
int ComputeCost(const Route &r) const; | ||
}; | ||
|
||
// LoadRangeBillingCostComponent | ||
// This class compute the cost dependent on the load and on the distance. | ||
class LoadRangeBillingCostComponent: public BillingCostComponent { | ||
public: | ||
LoadRangeBillingCostComponent(const ProbInput &in, int weight): | ||
BillingCostComponent(in, weight, "LoadRangeBillingCostComponent") {} | ||
unsigned FindRange(const Route &r, unsigned load) const; | ||
unsigned MaxRate(const Route &r, unsigned range) const; | ||
int ComputeCost(const Route &r) const; | ||
}; | ||
|
||
|
||
// LoadClientDependetBillingCostComponent | ||
// This class compute the cost dependent on the load | ||
// and on the province for each client(load*cost_coef). | ||
class LoadClientDependentCostComponent: public BillingCostComponent { | ||
public: | ||
LoadClientDependentCostComponent(const ProbInput &in, int weight): | ||
BillingCostComponent(in, weight, "LoadClientDependentCostComponent") {} | ||
int ComputeCost(const Route &r) const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,126 @@ | ||
#include <utils/Random.hh> | ||
#include <vector> | ||
#include <string> | ||
#include <utility> | ||
#include <algorithm> | ||
#include "data/order.h" | ||
#include "helpers/vrp_state_manager.h" | ||
#include <utils/Random.hh> | ||
|
||
void VRPStateManager::RandomState(RoutePlan& rp) { | ||
ResetState(rp); | ||
const std::pair &plan_horizon = in.get_plan_horizon(); | ||
bool schduled = false; | ||
int day, vid; | ||
std::vector<int> orders; | ||
std::vectro<int> unschduled_orders; | ||
bool unschduled = false; | ||
assert(plan_horizon.first >= 1); | ||
for (int i = 0; i < in.get_num_order(); ++i) { | ||
for (int i = 0, rnd = 0; i < in.get_num_order(); ++i) { | ||
int day, vid; | ||
do { | ||
day = Random::Int(0, plan_horizon.second - 1); | ||
if (++rnd > in.get_dayspan()) { | ||
unschduled = true; | ||
break; | ||
} | ||
}while(!in.OrderVect(i).IsDayFeasible(day)); | ||
do { | ||
vid = Random::Int(0, in.get_num_vehicle() - 1); | ||
}while(!in.IsReachable(in.VehicleVect(vid), in.OrderVect(i))); | ||
rnd = 0; | ||
if (!unschduled) { | ||
do { | ||
vid = Random::Int(0, in.get_num_vehicle() - 1); | ||
if (++rnd > in.get_num_vehicle()) { | ||
unschduled = true; | ||
break; | ||
} | ||
}while(!in.IsReachable(in.VehicleVect(vid), in.OrderVect(i))); | ||
} | ||
if (unschduled) | ||
rp.AddOrder(i, 0, 0, unschduled); | ||
else | ||
unschduled_orders.push_back(i); | ||
} | ||
rp.AddRoute(Route(unschduled_orders)); | ||
UpdateTimeTable(rp); | ||
} | ||
|
||
void VRPStateManager::ResetState(RoutePlan &rp) { | ||
for (int i = 0; i < in.get_num_vehicle(); ++i) { | ||
for (int j = 0; j < in.get_dayspan(); ++j) { | ||
rp(i, j) = -1; | ||
} | ||
} | ||
} | ||
|
||
void VRPStateManager::UpdateTimeTable(RoutePlan &rp) { | ||
timetable.resize(rp.size()); | ||
for (int i = 0; i < rp.size(); ++i) { | ||
std::string client_from("c0"); | ||
int arrive_time = in.get_depart_time(); | ||
int stop_time = in.get_depart_time(); | ||
int route_size = rp[i].size(); | ||
|
||
timetable[i].resize(route_size + 1, 0); | ||
|
||
for (int j = 0; j <= route_size; ++j) { | ||
std::string client_to("c0"); | ||
if (j < route_size) | ||
client_to = in.OrderVect(rp[i][j]).get_client(); | ||
int ready_time = in.FindClient(client_to).get_ready_time(); | ||
arrive_time += in.FindClient(client_from).get_service_time() | ||
+ in.get_time_dist(client_from, client_to); | ||
if (arrive_time - stop_time > 45 * 360) { // driving rests | ||
arrive_time += 45 * 60; | ||
if (arrive_time < ready_time) | ||
arrive_time = ready_time; | ||
stop_time = arrive_time; | ||
} else if (arrive_time < ready_time) { | ||
if (ready_time - arrive_time >= 45 * 60) | ||
stop_time = ready_time; | ||
arrive_time = ready_time; | ||
} | ||
timetable[i][j] = arrive_time; | ||
} | ||
} | ||
} | ||
|
||
int VRPStateManager::ComputeDateViolationCost(Const RoutePlan& rp, int weight) { | ||
int num_vehicle = in.get_num_vehicle(); | ||
int day_span = in.get_dayspan(), cost = 0; | ||
for (int i = 0; i < num_vehicle; ++i) { | ||
for (int j = 0; j < day_span; ++j) { | ||
int rid = rp(i, j); | ||
if (rid == -1) | ||
continue; | ||
for (int k = 0; k < rp[rid].size(); ++k) { | ||
int oid = rp[rid][k]; | ||
const Order& ord = in.OrderVect(oid); | ||
cost += (1 - ord.IsDayFeasible(j + 1)) * ord.get_demand(); | ||
} | ||
} | ||
} | ||
return (weight * cost); | ||
} | ||
|
||
int VRPStateManager::ComputeTimeViolationCost(Const RoutePlan& rp, int weight) { | ||
int cost = 0; | ||
for (int i = 0; i < rp.size(); ++i) { | ||
for (int j = 0; j < rp[i].size(); ++j) { | ||
std::string client = in.OrderVect(rp[i][j]).get_client(); | ||
int duetime = in.FindClient(client).get_due_time(); | ||
if (rp.get_est(i, j) > duetime) | ||
cost += rp.get_est(i, j) - duetime; | ||
} | ||
} | ||
return (weight * cost); | ||
} | ||
|
||
int VRPStateManager::ComputeOptOrderCost(Const RoutePlan &rp, int weight) { | ||
int cost = 0; | ||
for (int i = 0; i < rp.size(); ++i) { | ||
if (rp[i].IsExcList()) { | ||
for (int j = 0; j < rp[i].size(); ++j) { | ||
const Order& ord = in.OrderVect(rp[i][j]); | ||
if (!ord.IsMandatory()) | ||
cost += ord.get_demand(); | ||
} | ||
} | ||
} | ||
return (weight * cost); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters