-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeasible.cpp
More file actions
196 lines (184 loc) · 4.24 KB
/
feasible.cpp
File metadata and controls
196 lines (184 loc) · 4.24 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
187
188
189
190
191
192
193
194
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <cassert>
#include "ts.h"
using namespace std;
bool quiet = false;
bool verbose = false;
bool equals(const int* a, const int* b, int n) {
for(int i=0; i<n; i++)
if(a[i]!=b[i]) return false;
return true;
}
int cardinality(int S, int n) {
int c = 0;
for(int i=0; i<n; i++)
if(S & (1<<i)) c++;
return c;
}
struct c_state {
TS& ts;
int* c;
c_state(TS& ts_) : ts(ts_) {
c = new int [ts.n];
for(int i=0; i<ts.n; i++) c[i] = 0;
}
c_state(const c_state& cs) : ts(cs.ts) {
c = new int [ts.n];
for(int i=0; i<ts.n; i++) c[i] = cs.c[i];
}
~c_state() {
delete [] c;
}
bool operator==(const c_state& cs) const {
return equals(c, cs.c, ts.n);
}
bool operator<(const c_state& cs) const {
for(int i=0; i<ts.n; i++) {
if(c[i] < cs.c[i]) return true;
if(cs.c[i] < c[i]) return false;
}
return false;
}
};
struct state {
TS& ts;
int pl; // player (1 or 2)
int* t;
set<c_state> Q;
typedef set<c_state>::const_iterator CI;
state(TS& ts_) : ts(ts_) {
t = new int [ts.n];
for(int i=0; i<ts.n; i++) t[i] = 0;
c_state c_zero(ts);
Q.insert(c_zero);
pl = 1;
}
state(const state& s) : ts(s.ts), pl(s.pl), Q(s.Q) {
t = new int [ts.n];
for(int i=0; i<ts.n; i++) {
t[i] = s.t[i];
}
}
~state() {
delete [] t;
}
bool operator==(const state& s) const { return (pl==s.pl) && equals(t,s.t,ts.n) && Q==s.Q; }
bool operator<(const state& s) const {
if(pl<s.pl) return true;
if(s.pl<pl) return false;
for(int i=0; i<ts.n; i++) {
if(t[i]<s.t[i]) return true;
if(s.t[i]<t[i]) return false;
}
if(Q < s.Q) return true;
if(s.Q < Q) return false;
return false;
}
int d(int i) const { return max(t[i] - (ts.T[i] - ts.D[i]), 0); }
void print() const {
cout << "P" << pl << " ";
for(int i=0; i<ts.n; i++) {
cout << "d[" << i << "]: " << d(i) << " ";
cout << "t["<< i << "]: " << t[i] << " ";
}
cout << endl << "{" << endl;
for(CI csi=Q.begin(); csi!=Q.end(); csi++) {
for(int i=0; i<ts.n; i++) {
cout << "\tc[" << i << "]: " << csi->c[i] << " ";
}
cout << endl;
}
if(Q.empty()) cout << "\t*FAIL*" << " " << endl;
cout << "}" << endl;
}
};
// Main data structures
TS ts; // the task system being analyzed
int m; // n. of processors
bool feasible(TS& ts) {
map<state, bool> generated;
bool failureGenerated = false;
state start(ts);
generated[start] = true;
queue<state> q;
q.push(start);
int count = 1;
while(!q.empty()) {
state s = q.front();
q.pop();
if(s.pl==1) {
// V1 -> V2
for(int K=0; K<(1<<ts.n); K++) {
//DEBUGE(K);
state v2 = s;
v2.pl = 2;
for(int i=0; i<ts.n; i++) {
if((K & (1<<i)) && s.t[i]==0) {
v2.t[i] = ts.T[i];
for(state::CI csi=v2.Q.begin(); csi!=v2.Q.end(); csi++) {
csi->c[i] = ts.C[i];
}
}
}
//v2.print();
//DEBUGE(generated[v2]);
if(!generated[v2]) {
generated[v2] = true;
count++;
q.push(v2);
}
}
}
else if(s.pl==2) {
// V2 -> V1
state v1 = s;
v1.pl = 1;
for(int i=0; i<ts.n; i++) {
v1.t[i] = max(v1.t[i]-1, 0);
}
v1.Q.clear();
for(state::CI csi=s.Q.begin(); csi!=s.Q.end(); csi++) {
for(int S=0; S<(1<<ts.n); S++) {
if(cardinality(S, ts.n) > m) continue;
c_state cs(*csi);
bool deadmiss = false;
for(int i=0; i<ts.n; i++) {
if((S & (1<<i))) {
cs.c[i] = max(csi->c[i]-1, 0);
}
if(v1.d(i)==0 && cs.c[i]>0) {
deadmiss = true;
break;
}
}
if(!deadmiss) {
v1.Q.insert(cs);
}
}
}
if(!generated[v1]) {
if(v1.Q.empty()) failureGenerated = true;
generated[v1] = true;
count++;
q.push(v1);
}
}
if(verbose) s.print();
}
cerr << count << " states generated. " << endl;
bool feasibleTS = !failureGenerated;
cerr << "The task system is " << (feasibleTS ? "FEASIBLE" : "INFEASIBLE") << "." << endl;
return feasibleTS;
}
int main(int argc, char* argv[]) {
if(argc>1 && string(argv[1])=="-v") verbose = true;
if(argc>1 && string(argv[1])=="-q") quiet = true;
if(!quiet) cerr << "Number of processors? " << endl;
cin >> m;
ts.read(quiet);
return ((int) feasible(ts));
}