Skip to content

Commit

Permalink
Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
yqf-oo committed Feb 17, 2015
1 parent d528acc commit 62b83e0
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 20 deletions.
3 changes: 3 additions & 0 deletions data/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Client{
id(cid), id_region(rid), serv_time(st * 60), time_window(rt, dt) { }
std::string get_id() const { return id; }
std::string get_region() const { return id_region; }
int get_ready_time() const { return time_window.first * 60; }
int get_due_time() const { return time_window.second * 60; }
int get_service_time() const { return serv_time * 60; }
private:
std::string id, id_region;
int serv_time;
Expand Down
1 change: 1 addition & 0 deletions data/order.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Order {
date_window(rd, dd) { }
std::string get_id() const { return id; }
std::string get_client() const { return id_client; }
unsigned get_demand() const { return quantity; }
bool IsMandatory() const { return mandatory; }
bool IsDayFeasible(int day) const {
return (day >= date_window.first && day <= date_window.second); }
Expand Down
2 changes: 1 addition & 1 deletion data/prob_input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void ProbInput::ReadDataSection(std::fstream &input) {
string region_id;
for (int j = 0; j < num_incompatible_regions; ++j){
intput >> region_id;
int region_ind = region_ind[region_id];
int region_ind = region_imap[region_id];
assert(region_ind < num_region);
site_map[i][region_ind] = false;
}
Expand Down
6 changes: 6 additions & 0 deletions data/prob_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class ProbInput{
assert(i < num_vehicle);
return vehicle_vec(i);
}
int get_dayspan() const {
return plan_horizon.second - plan_horizon.fisrt + 1;
}
int get_depart_time() const {
return client_vec[client_imap[depot_id]].get_ready_time();
}
const std::pair<int, int>& get_plan_horizon() const { return plan_horizon; }
int get_distance(const string&, const string&) const;
int get_time_dist(const string&, const string&) const;
Expand Down
34 changes: 24 additions & 10 deletions data/route.h
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 &in;
std::vector<std::vector<Route>> plan;
std::vector<Route> routes;
std::vector<std::vector<int>> timetable;
std::vector<std::vector<int>> plan;
};
#endif
69 changes: 69 additions & 0 deletions helpers/billing_cost_component.h
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 &in;
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
122 changes: 114 additions & 8 deletions helpers/vrp_state_manager.cc
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);
}
6 changes: 5 additions & 1 deletion helpers/vrp_state_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class VRPStateManager: public StateManager<ProbInput, RoutePlan> {
StateManager<ProbInput, RoutePlan>(pi, "VRPStateManager") { }
~VRPStateManager() { ClearCostComponent(); }
void RandomState(RoutePlan&);
bool CheckConsistency(const RoutePlan&) const;
bool CheckConsistency(const RoutePlan&) const { return true; }
unsigned CostFunction(const RoutePlan&) const;
unsigned Objective(const RoutePlan&) const;
unsigned Violations(const RoutePlan&) const;
private:
void ResetState(RoutePlan&);
void UpdateTimeTable(RoutePlan&);
int ComputeDateViolationCost(const RoutePlan&, int);
int ComputeTimeViolationCost(const RoutePlan&, int);
int ComputeOptOrderCost(const RoutePlan&, int);
};

#endif

0 comments on commit 62b83e0

Please sign in to comment.