This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
try_eval_p1.cpp
203 lines (178 loc) · 5.57 KB
/
try_eval_p1.cpp
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
195
196
197
198
199
200
201
202
203
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <list>
#include <climits>
#include "graph.h"
using namespace std;
const double INF_DOUBLE = 1e20;
double loss_prob = 0.2;
vector<int> edges[NODE_NUM_MAX];
int weight[NODE_NUM_MAX];
int TW;
int n; // tot node number
int in_deg[NODE_NUM_MAX];
double value[NODE_NUM_MAX];
int np = 1; // tot package number
vector<int> pkgs[PKG_NUM_MAX];
vector<vector<int>> rst;
static double preset(int cur,int depth)
{
static bool vis[NODE_NUM_MAX];
if(edges[cur].empty())return 1;
int s = edges[cur].size();
double temp_val = 0;
for(int i = 0;i < s;i++)
{
if(!vis[edges[cur][i]]){
if(loss_prob > 0.3)temp_val += preset(edges[cur][i], depth+1)*pow(1-loss_prob, depth); //1、丢包率小的情况下可以做改进。
//2、看前置的装的包的个数
else temp_val += preset(edges[cur][i], depth+1) * (1-loss_prob);
vis[edges[cur][i]] = true;
}
else temp_val += value[edges[cur][i]];
}
if(cur)value[cur] += temp_val;
return temp_val;
}
void evaluate_nodes()
{
// static int acc_w[NODE_NUM_MAX];
// static int layer[NODE_NUM_MAX];
// memset(acc_w, 255, sizeof(acc_w));
// struct Node {
// int index;
// };
// vector<Node> *q = new vector<Node>;
// vector<Node> *q2 = new vector<Node>;
// acc_w[0] = 0;
// q->push_back({0});
// for (int h = 0; !q->empty(); ++h) {
// for (Node& node : *q) {
// int cur = node.index;
// for (int nxt : edges[cur]) {
// if (weight[nxt] == -1) {
// acc_w[nxt] = weight[nxt] + acc_w[cur];
// q->push_back({nxt});
// }
// else if (weight[nxt] + acc_w[cur] < acc_w[nxt]) {
// acc_w[nxt] = weight[nxt] + acc_w[cur];
// }
// }
// }
// q->clear();
// swap(q, q2);
// }
// for (int x = 1; x <= n; ++x) {
// layer[x] = (acc_w[x] + PKG_SIZE - 1) / PKG_SIZE;
// value[x] = 10000. / weight[x] * pow(1 - loss_prob, layer[x]);
// }
preset(0, 0);
}
static int cnt[NODE_NUM_MAX];
struct Node {
int index;
int count;
double value;
bool operator< (const Node& n) const {
return value < n.value;
}
};
static priority_queue<Node> pq; // 大顶堆
static int tw2 = 0;
// static int cnt_debug[NODE_NUM_MAX];
void dfs_packaging_helper(int cur, int c)
{
static int cur_weight_sum = 0;
static bool vis[NODE_NUM_MAX] = {0};
if (cur == 0)
memset(vis, 0, sizeof(vis));
for (int nxt : edges[cur]) {
if (!vis[nxt] && c <= cnt[nxt]) {
vis[nxt] = true;
if (2 * (tw2 + weight[nxt]) > 3 * TW)
break;
if (cur_weight_sum + weight[nxt] <= PKG_SIZE) {
/* 当前包的剩余容量够装 */
cur_weight_sum += weight[nxt];
pkgs[np].push_back(nxt);
}
else {
/* 当前包的剩余容量不够装,开一个新的包 */
cur_weight_sum = weight[nxt];
pkgs[++np].push_back(nxt);
}
tw2 += weight[nxt];
// ++cnt_debug[nxt];
dfs_packaging_helper(nxt, c);
}
}
}
static int tw = 0;
void make_packages()
{
/* Stage 1: decide which node(and how many times) to be packaged */
pq.push({0,1,INF_DOUBLE});
int flag = 1;
static int cnt2[NODE_NUM_MAX];
for (;;) {
Node node = pq.top(); pq.pop();
int cur = node.index;
if ((tw + weight[cur]) * 2 > TW * 3)
break;
tw += weight[cur];
ASSERT(cnt[cur] == node.count - 1);
++cnt[cur];
for (int nxt : edges[cur]) {
if (cnt2[nxt] < cnt[cur]) {
ASSERT(cnt2[nxt] == cnt[cur]-1);
pq.push({nxt, cnt[cur], value[nxt] * pow(loss_prob, cnt[cur]-1)});
++cnt2[nxt];
}
}
/* 只要有非零的k层次节点被扩展,那么k+1层次的节点就要做好准备 */
if (cur != 0 && cnt[cur] == flag) {
flag += 1;
pq.push({0,cnt[cur]+1,INF_DOUBLE});
}
}
/* Stage 2: grouping */
for (int c = 1; c < flag; ++c) {
dfs_packaging_helper(0, c);
++np; // 要求两个层次的节点不能混装在一个包中
}
rst = vector<vector<int>>(pkgs+1, pkgs+np+1);
}
int main(int argc, char* argv[])
{
if (LOG) {
freopen("log.txt", "w", stdout);
}
char *edge_path, *weight_path, *answer_path;
if (argc == 5) {
edge_path = argv[1];
weight_path = argv[2];
answer_path = argv[3];
loss_prob = atoi(argv[4]) / 100.;
}
else {
edge_path = (char *)"data/Out_OutGraph_Basketball_480_Slice16_Gop8_10.log";
weight_path = (char *)"data/Out_SliceSize_Basketball_480_Slice16_Gop8_10.log";
answer_path = (char *)"result.txt";
loss_prob = 0.2;
}
read_graph(edge_path, weight_path); debug_printf("TW = %d\n", TW);
evaluate_nodes();
make_packages();
printf("np = %d, rst.size() = %u\n", np, rst.size());
printf("tw = %d, tw2 = %d\n", tw, tw2);
output(answer_path);
// for (int i = 1; i <= n; ++i) {
// if (cnt[i] != cnt_debug[i])
// printf("cnt[%d] = %d, cnt_debug[%d] = %d\n", i, cnt[i], i, cnt_debug[i]);
// }
return 0;
}