Professional Documents
Culture Documents
Corect Form
Corect Form
Corect Form
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include "Constant.h"
#include <random>
#include <unordered_map>
#include<algorithm>
using namespace std;
class ChargingStation
{
public:
int cityId;
string cityName;
int distanceToLastCity;
int numberOfChargers;
int queueLength;
public:
ChargingStation(int id, string name, int chargers)
: cityId(id), cityName(name), numberOfChargers(chargers), queueLength(0) {}
void displayInfo()
{
cout << left << setw(4) << cityId;
cout << left << setw(25) << cityName;
cout << left << setw(25) << distanceToSydney(cityId);
cout << left << setw(20) << numberOfChargers;
cout << left << setw(15) << queueLength;
cout << left << setw(40) << fixed << setprecision(2) << (queueLength > 0 ?
(0.5 * static_cast<double>(queueLength) / numberOfChargers) : 0.0) << " hours" <<
endl;
}
};
class Vehicle
{
public:
int vehicleId; // can be any integer
int currentCityId; // initialised with 0 for Sydney
int destinationId; // any city other than Sydney
int capacityRange; // in kilometers
int remainRange; // in kilometers
vector<int> rechargeCities; // Vector to store recharge cities
int secondRecharge; // Variable to store the second recharge city if needed
public:
Vehicle(int id, int dest, int capacity, int remain)
: vehicleId(id), destinationId(dest), capacityRange(capacity),
remainRange(remain), currentCityId(0), secondRecharge(-1) {}
void calculateRechargeCities()
{
int remain = remainRange;
int distance = 0;
bool firstRechargeFound = false;
if (remain < 0)
{
if (!firstRechargeFound)
{
rechargeCities.push_back(i);
firstRechargeFound = true;
}
else if (secondRecharge == -1)
{
secondRecharge = i;
}
remain = capacityRange;
}
}
}
class DemandGenerator
{
public:
void generateDemandsToFile(const std::string &fileName, int numDemands)
{
std::ofstream file(fileName);
if (!file.is_open())
{
std::cerr << "File creation failed!" << std::endl;
return;
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> destDistribution(1, NUM_CITIES - 1); //
Destination ID range (1 to NUM_CITIES - 1)
std::uniform_int_distribution<int> capacityDistribution(MIN_CAPACITY,
MAX_CAPACITY);
std::uniform_int_distribution<int> remainDistribution(0, MAX_CAPACITY);
file << "[" << vehicleId << "," << destinationId << "," <<
capacityRange << "," << remainRange << "]\n";
}
file.close();
}
};
class ChargingAllocation
{
public:
void allocateChargingStations(const vector<Vehicle> &vehicles,
vector<ChargingStation> &stations)
{
// Initialize queue lengths for each charging station to 0
vector<int> queueLengths(NUM_CITIES, 0);
queueLength++;
queueLengths[rechargeCity] = queueLength;
}
}
cout << "Overall average waiting time per vehicle = " << fixed <<
setprecision(2) << (totalWaitingHours / vehicles.size()) << " hours" << endl;
}
// Iterate through vehicles and allocate them to stations with the shortest
queues
for (const Vehicle &vehicle : vehicles)
{
int minQueueLength = INT_MAX;
int selectedStationIndex = -1;
cout << "Improved overall average waiting time = " << fixed <<
setprecision(2) << bestOverallAvgWaitTime << " hours at simulation " << simulation
<< endl;
}
int main()
{
// Load charging station data
vector<ChargingStation> stations;
demandGenerator.generateDemandsToFile(fileName, numDemands);
vector<Vehicle> vehicles;
string line;
while (getline(file, line))
{
int id, dest, capacity, remain;
if (sscanf(line.c_str(), "[%d,%d,%d,%d]", &id, &dest, &capacity, &remain)
== 4)
{
Vehicle vehicle(id, dest, capacity, remain);
vehicle.calculateRechargeCities();
vehicles.push_back(vehicle);
}
}
file.close();
ChargingAllocation chargingAllocation;
chargingAllocation.allocateChargingStations(vehicles, stations);
const int numSimulations=8;
// Balance waiting queues with Monte-Carlo simulations
cout << "\nBalancing waiting queues with Monte-Carlo simulations." << endl;
cout << "Number of simulations = " << numSimulations << endl;
chargingAllocation.balanceWaitingQueues(vehicles, stations);
return 0;
}