-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cut.h
184 lines (152 loc) · 4.33 KB
/
Cut.h
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
//
// Created by nandgate on 9/30/24.
//
//#ifndef SGUFP_SOLVER_CUT_H
//#define SGUFP_SOLVER_CUT_H
#pragma once
#include <unordered_map>
#include <map>
#include <tuple>
#include <memory>
using namespace std;
typedef int8_t shi; // short ints :)
enum CutType {
OPTIMALITY,
FEASIBILITY
};
inline static int cantor(int a, int b){
return (a+b+1)* (a+b)/2 + b;
}
/**
* hash function for tuple of three integers, to be used as key
* for cut coefficients map.
*/
struct cut_tuple_hash{
size_t operator()(const tuple<int,int,int>& tuple1) const{
int a = get<0>(tuple1);
int b = get<1>(tuple1);
int c = get<2>(tuple1);
return cantor(a, cantor(b,c));
}
};
/**
* equality of two tuples of three integers, to be used in comparing two keys in
* cut coefficients map.
*/
struct cut_tuple_equal{
bool operator()(const tuple<int,int,int>& tuple1, const tuple<int,int,int>& tuple2) const {
return get<0>(tuple1) == get<0>(tuple2) &&
get<1>(tuple1) == get<1>(tuple2) &&
get<2>(tuple1) == get<2>(tuple2);
}
};
struct unordered_map_hash {
/*
* Hash value of the unordered_map<tuple<int,int,int>>, double>
*/
size_t operator()(const unordered_map<tuple<int,int,int>, double>& coeff) {
size_t seed = 0;
for (const auto& pair: coeff) {
// get hash of tuple
auto h1 = cut_tuple_hash()(pair.first);
auto h2 = hash<double>{}(pair.second);
// TODO; use h1 and h2 in seed somehow.
// naive hashing (INFO highly inefficient)
}
return seed;
}
};
// typedef unordered_map<tuple<int,int,int>, double, cut_tuple_hash, cut_tuple_equal> CutCoefficients;
typedef map<tuple<int,int,int>,double> CutCoefficients; // LATER: change to unordered_map
class Cut{
size_t hash;
public:
bool operator==(const Cut& cut2) const { // ASAP fix this
return (this->cutType == cut2.cutType) && (this->RHS == cut2.RHS) &&
(this->cutCoeff == cut2.cutCoeff);
}
bool operator!=(const Cut& cut2) const {
return *this == cut2;
}
size_t getHash(){return hash;}
CutType cutType;
double RHS;
CutCoefficients cutCoeff;
[[nodiscard]] double get(uint a, uint b, uint c) const {
return cutCoeff.at(make_tuple(a,b,c));
}
Cut(CutType cutType_, double RHS_, CutCoefficients cutCoeff_):
cutType{cutType_}, RHS{RHS_}, cutCoeff{std::move(cutCoeff_)}{
// compute hash here.
}
};
/**
* hash function for Cut object.
*/
struct cut_hash{
size_t operator()(const Cut& cut1) const {
// bit manipulation of hash of RHS, cuttype, and coefficients.
size_t h1 = hash<double>{}(cut1.RHS);
size_t h2 = hash<int>{}(cut1.cutType);
size_t h3 = h1 ^ (h2 << 2);
// ASAP add hash function for cut coefficients.
// DO NOT USE THIS FUNCTION WITHOUT THE HASH OF CUT COEFFICIENTS.
size_t h4 = hash<int>{}(0);
return h4 ^ (h3 << 1);
}
};
static inline vector<vector<vector<shi>>> w2y(const vector<int>& w_solution, const shared_ptr<Network>& networkPtr){
vector<shi> y_1 (networkPtr->n,0);
vector<vector<shi>> y_2(networkPtr->n, y_1);
vector<vector<vector<shi>>> y_bar(networkPtr->n, y_2);
for (uint a = 0; a < w_solution.size(); a++){
if (w_solution[a] != -1){
auto arcId = networkPtr->processingOrder[a].second;
auto q = networkPtr->networkArcs[arcId].headId;
auto i = networkPtr->networkArcs[arcId].tailId;
auto j = networkPtr->networkArcs[w_solution[a]].headId;
y_bar[i][q][j] = 1;
//cout << "i: " << i << ", q: " << q << ", j " << j << endl;
}
}
return y_bar;
}
class CutContainer {
// unordered_set of containers or vector of containers.
#ifdef DEBUG
void displayCutStats() const {
string type = (cutType == FEASIBILITY) ? "FEASIBILITY" : "OPTIMALITY";
cout << "********************** Cut stats for nerds ************************" << endl;
cout << "Number of " << type <<" cuts: " << cuts.size() << endl;
cout << "*******************************************************************" << endl;
}
#endif
public:
vector<Cut> cuts;
CutType cutType;
public:
// begin(){ return cuts.begin();};
void doSomething() {
auto it = cuts.begin();
}
explicit CutContainer(CutType type_): cutType(type_){}
bool isCutExists(const Cut& cut) {
for (const auto& c : cuts) {
if (c == cut) return true;
}
return false;
}
void insertCut(Cut cut) {
cuts.push_back(cut);
}
void clearContainer() {
cuts.clear();
}
~CutContainer() {
#ifdef DEBUG
// displayCutStats();
#endif
cuts.clear();
}
};
//#endif //SGUFP_SOLVER_CUT_H