-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.cpp
More file actions
186 lines (159 loc) · 4.92 KB
/
Network.cpp
File metadata and controls
186 lines (159 loc) · 4.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Created by nandgate on 6/2/24
*/
#include "Network.h"
#include <fstream>
#include <cstring>
#include <string>
Network::Network(const std::string& p_fileName){
// open file.
std::ifstream file {p_fileName};
if (file.is_open()){
/* file opened for reading. */
uint nNodes,m, scenarios;
file >> nNodes >> m >> scenarios;
std::vector<NetworkNode> netNodes(nNodes);
std::vector<NetworkArc> netArcs(m);
for (uint i = 0; i < nNodes; i++)
netNodes[i].nodeId = i;
for (uint i = 0; i < m; i++){
// from second line, read each line and its corresponding scenario.
// each line contains headId, headId,
uint tailId, headId;
file >> tailId >> headId;
vi lowerCapacities(scenarios);
vi upperCapacities (scenarios);
vi rewards (scenarios);
for (uint j = 0; j < scenarios; j++){
// for each scenario read lb, ub, reward
int lb, ub, reward;
file >> lb >> ub >> reward;
lowerCapacities[j] = lb;
upperCapacities[j] = ub;
rewards[j] = reward;
}
netArcs[i] = {i, tailId, headId, upperCapacities, lowerCapacities, rewards};
// update node's incoming and outgoing attributes.
netNodes[tailId].outNodeIds.push_back(headId);
netNodes[headId].inNodeIds.push_back(tailId);
netNodes[tailId].outgoingArcs.push_back(i);
netNodes[headId].incomingArcs.push_back(i);
}
// read v_bar
std::string temp; file >> temp;
// read v_bar nodes and build network.
vector<uint> vBarNodes;
uint index;
while (file >> index){
vBarNodes.push_back(index);
netNodes[index].isVbar = true;
}
// populate a1,a2,a3,a4 for subproblem formulation.
vui a1 = {}, a2 = {}, a3 = {}, a4 = {};
for (const auto& arc: netArcs){
auto i = arc.tailId;
auto j = arc.headId;
if (netNodes[i].incomingArcs.empty()){
if (netNodes[j].outgoingArcs.empty()) a4.push_back(arc.arcId);
else a1.push_back(arc.arcId);
}
else {
if (netNodes[j].outgoingArcs.empty()) a2.push_back(arc.arcId);
else a3.push_back(arc.arcId);
}
}
a4 = {};
this->n = nNodes;
this->edges = m;
this->networkNodes = std::move(netNodes);
this->networkArcs = std::move(netArcs);
this->nScenarios = scenarios;
this->Vbar = std::move(vBarNodes);
this->A1 = std::move(a1);
this->A2 = std::move(a2);
this->A3 = std::move(a3);
this->A4 = std::move(a4);
shuffleVBarNodes();
// INFO change the order of nodes in the Vbar. Make sure that arcs of a particular node are together.
int i = 0;
for (const auto& id: Vbar){
// another way of doing state update map.
auto& node = networkNodes[id];
set<int> states (node.outgoingArcs.begin(), node.outgoingArcs.end());
// if (node.outgoingArcs.size() < node.incomingArcs.size())
states.insert(-1);
stateUpdateMap.insert({i, states});
bool first = true;
// sort incoming arcs based in decreasing order of reward
// std::sort(node.incomingArcs.begin(), node.incomingArcs.end(),
// [this](const auto a, const auto b) constexpr {
// return this->networkArcs[a].rewards[0] > this->networkArcs[b].rewards[0];
//
// });
for (const auto& inId: node.incomingArcs){
if (first) {this->hasStateChanged.push_back(true); first = false;}
else this->hasStateChanged.push_back(false);
processingOrder.emplace_back(i++, inId);
layerRewards.push_back(networkArcs[inId].rewards[0]);
}
}
hasStateChanged.push_back(false); // extra element for buildNextLayer in DD class.
for (auto id : Vbar)
totalLayers += networkNodes[id].incomingArcs.size();
}
else {
// error in opening file. exit program.
cerr << "File could not be opened!" << endl;
cerr << "Error code: " << strerror(errno);
}
}
#include <algorithm>
void Network::shuffleVBarNodes() noexcept {
auto copyOfVbar = Vbar;
vector<uint> demandPoints;
vector<uint> neworder = {};
for (auto node: networkNodes) {
if (node.outgoingArcs.size() == 1 && networkArcs[node.outgoingArcs[0]].headId == n-1 ) {
demandPoints.push_back(node.nodeId);
}
}
for (auto id: demandPoints) {
auto it = find(copyOfVbar.begin(), copyOfVbar.end(), id);
if (it != copyOfVbar.end()) {
neworder.push_back(*it);
}
}
for (auto id: neworder) {
auto it = find(copyOfVbar.begin(), copyOfVbar.end(), id);
if (it != copyOfVbar.end()) {
copyOfVbar.erase(it);
}
}
vector<uint> childsVec = demandPoints;
vector<uint> parentsVec = {};
while (copyOfVbar.size() > 0) {
parentsVec={};
for (auto id: childsVec) {
for (auto id: networkNodes[id].incomingArcs) {
parentsVec.push_back(networkArcs[id].tailId);
}
}
for (auto id: parentsVec) {
auto it = find(copyOfVbar.begin(), copyOfVbar.end(), id);
if (it != copyOfVbar.end()) {
auto it1 = find(neworder.begin(), neworder.end(), id);
if (it1 == neworder.end()) {
neworder.push_back(*it);
}
}
}
for (auto id: neworder) {
auto it = find(copyOfVbar.begin(), copyOfVbar.end(), id);
if (it != copyOfVbar.end()) {
copyOfVbar.erase(it);
}
}
childsVec = parentsVec;
}
Vbar = neworder;
}