?? problem.cpp
字號(hào):
#include "Problem.h"
#include <cmath>
Problem::Problem( Control& control) {
// This function has the following (side) effects:
// 1.reads in a problem instance from file,
// 2.computes the cost matrix, if coordinates are given in problem instance,
// 3.computes demands and probability distribution of demands
// for each customer
// 4.if the capacity is given from command line, the vehicle capacity is
// assigned this value, instead of the value read from the input file (after verification
// that the new value is not smaller than the biggest possible customer demand).
// Note: probability distribution of customer i is uniform on the interval
// [avgDmnd[i]-spread[i], avgDmnd[i]+spread[i]].
istream& ifs = control.getInputStream();
ifs >> numberOfCustomers;
vector<int> avgDmnd(numberOfCustomers);
vector<int> spread(numberOfCustomers);
vector<vector<double> > xyCustomers(numberOfCustomers);
//Allocation of distance matrix.
distanceMatrix = vector<vector<double> >(numberOfCustomers);
for(int i=0;i<numberOfCustomers; i++)
distanceMatrix[i] = vector<double>(numberOfCustomers);
if(control.getFlagMatrix() == false){
//Temporary quantities to be read from ifs.
for(int i=0; i<numberOfCustomers; i++)
xyCustomers[i] = vector<double>(2);
//Read data from ifs.
int i=0;
while(i<numberOfCustomers){
ifs >> i;
ifs >> xyCustomers[i][0] >> xyCustomers[i][1];
ifs >> avgDmnd[i] >> spread[i];
i++;
}
//Compute distance matrix.
for(int i=0;i<numberOfCustomers; i++){
for(int j=0; j<numberOfCustomers; j++){
distanceMatrix[i][j]= sqrt((xyCustomers[i][0]-xyCustomers[j][0])
*(xyCustomers[i][0]-xyCustomers[j][0]) +
(xyCustomers[i][1]-xyCustomers[j][1])
*(xyCustomers[i][1]-xyCustomers[j][1]));
}
}
}
else if(control.getFlagMatrix() == true){
//Read demand and spread data from ifs.
int i=0;
while(i<numberOfCustomers){
ifs >> i;
ifs >> avgDmnd[i] >> spread[i];
//cout << avgDmnd[i] << " " << spread[i] << " " << endl;/////
i++;
}
//Read distance matrix from ifs.
i=0;
int j=0;
while(i<numberOfCustomers){
while(j<numberOfCustomers){
ifs >> distanceMatrix[i][j];
//cout << distanceMatrix[i][j] << " ";//////
j++;
}
//cout << endl;///
j=0;
i++;
}
}
else {
cerr << "Problem.cpp Error: control.getMatrix is neither true nor false, exiting" << endl;
exit(-1);
}
//Compute customers demands and probabilities (uniform on an interval).
customerDemand = vector<vector<PossibleDemand> >(numberOfCustomers);
int maxDmnd=0; //maximum possible demand
double averageDmnd=0.0; //average demand over all customers
for(int i=0; i<numberOfCustomers; i++){
//Dealing with boundary condition
if ((avgDmnd[i] - spread[i])<0) {
avgDmnd[i] = spread[i];
}
customerDemand[i] = vector<PossibleDemand>(2*spread[i]+1);
averageDmnd+= avgDmnd[i];
for(int d=0; d< (2*spread[i] +1); d++){
int demand = avgDmnd[i]-(spread[i] - d);
//Dealing with boundary condition.
//if ( demand > 0 )
customerDemand[i][d].demand_ = demand;
//else customerDemand[i][d].demand_ = 0;
customerDemand[i][d].probability_ = 1.0/(2.0*spread[i] +1.0);
//keep tracks of maximum possible demand
if(customerDemand[i][d].demand_ > maxDmnd)
maxDmnd = customerDemand[i][d].demand_;
}
}
//If given as input parameter, set the capacity equal to the command line value.
if( control.capacityExists() ) {
capacity = control.getCapacity();
if (capacity < maxDmnd){ cerr << "Error: vehicle capacity given in input from command line is too small. Vehicle capacity must be bigger than the maximum customer demand: " << maxDmnd << ". Exit" << endl;
exit(-1);
}
}
//Else read the capacity from the input file.
else
ifs >> capacity;
#ifdef DEBUG
//Print out some stats to verify initialization.
cout << "# Problem data:" << endl;
cout << "# numberOfCustomers (depot incl.) " << numberOfCustomers << endl;
cout << "# total average demand " << averageDmnd << endl;
cout << "# capacity " << capacity << endl;
#endif
/*
for(int i=0; i<numberOfCustomers; i++){
cout << i << " ";
cout << xyCustomers[i][0] << " ";
cout << xyCustomers[i][1] << " ";
cout << avgDmnd[i] << " ";
cout << spread[i] << endl;
for(int j=0; j<numberOfCustomers; j++)
cout << distanceMatrix[i][j] << " ";
cout << endl;
for(int d=0; d<2*spread[i]+1; d++)
cout << customerDemand[i][d].demand_ << " " << customerDemand[i][d].probability_ << " ";
cout << endl;
}
*/
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -