-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSC_Test_Code.cpp
More file actions
133 lines (114 loc) · 4.39 KB
/
SC_Test_Code.cpp
File metadata and controls
133 lines (114 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
using namespace std;
// Struct to represent the resource metadata
struct Resource {
string name; // Type of resource
double cost; // Cost per unit
double importance; // Priority factor (higher is more important)
double availableUnits; // Total units available
};
// Struct to represent the sector metadata
struct Sector {
string name; // Type of sector
double requiredUnits; // Total units needed
double allocatedUnits; // Units allocated
vector<string> resourcePreferences; // Ordered list of preferred resources
};
// Struct to represent each edge of the graph
struct Edge {
int to;
double capacity;
};
// Class to manage resources and allocation
class ResourceAllocator {
vector<Sector> sectors;
vector<Resource> resources;
public:
// Constructor to initialize sectors and resources
ResourceAllocator(const vector<Sector>& sectors, const vector<Resource>& resources)
: sectors(sectors), resources(resources) {}
// Assign resources to sectors based on availability and priority (greedy algorithm)
void allocateResources() {
// Sort sectors by requiredUnits using selection sort
for (int i = 0; i < sectors.size(); ++i) {
int maxIndex = i;
for (int j = i + 1; j < sectors.size(); ++j) {
if (sectors[j].requiredUnits > sectors[maxIndex].requiredUnits) {
maxIndex = j;
}
}
if (maxIndex != i) {
swap(sectors[i], sectors[maxIndex]);
}
}
// Allocate resources to sectors
for (auto & sector : sectors) {
for (int j = 0; j < sector.resourcePreferences.size(); ++j) {
const string& preferredResource = sector.resourcePreferences[j];
// Search to find the preferred resource
auto resourceCur = resources.end();
for (int k = 0; k < resources.size(); ++k) {
if (resources[k].name == preferredResource) {
resourceCur = resources.begin() + k;
break;
}
}
if (resourceCur != resources.end() && resourceCur->availableUnits > 0) {
double allocatable = min(resourceCur->availableUnits, sector.requiredUnits);
resourceCur->availableUnits -= allocatable;
sector.allocatedUnits += allocatable;
sector.requiredUnits -= allocatable;
if (sector.requiredUnits <= 0) {
break;
}
}
}
}
}
// Display allocation results
void displayAllocation() const {
cout << "Resource Allocation Results:\n";
for (const auto & sector : sectors) {
cout << "Sector: " << sector.name << ", Allocated Units: " << sector.allocatedUnits
<< ", Remaining Requirement: " << sector.requiredUnits << "\n";
}
cout << "Remaining Resource Availability:\n";
for (const auto & resource : resources) {
cout << "Resource: " << resource.name << ", Available Units: " << resource.availableUnits << "\n";
}
}
};
// Class to represent a graph for resource flow
class ResourceFlowGraph {
vector<vector<Edge>> adjList;
public:
explicit ResourceFlowGraph(int n) : adjList(n) {}
void addEdge(int from, int to, double capacity) {
adjList[from].push_back({to, capacity});
adjList[to].push_back({from, 0}); // Reverse edge for flow adjustment
}
};
int main() {
// Resource and sector test data
vector<Resource> resources = {
{"Water", 10, 0.9, 1000},
{"Electricity", 20, 0.8, 500},
{"Gas", 15, 0.7, 300}
};
vector<Sector> sectors = {
{"Household", 400, 0, {"Water", "Electricity"}},
{"Business", 600, 0, {"Electricity", "Gas"}},
{"Industrial", 800, 0, {"Gas", "Water"}}
};
// Allocation example
ResourceAllocator allocator(sectors, resources);
allocator.allocateResources();
allocator.displayAllocation();
// Graph setup example
ResourceFlowGraph flowGraph(5);
flowGraph.addEdge(0, 1, 10);
flowGraph.addEdge(1, 2, 5);
cout << "Resource Allocation and Flow complete.\n";
return 0;
}