-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow.java
More file actions
242 lines (152 loc) · 3.97 KB
/
Flow.java
File metadata and controls
242 lines (152 loc) · 3.97 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package src;
import java.util.ArrayList;
import umontreal.iro.lecuyer.randvar.NormalGen;
import umontreal.iro.lecuyer.randvar.PoissonGen;
import umontreal.iro.lecuyer.rng.MRG31k3p;
import umontreal.iro.lecuyer.rng.RandomStream;
public class Flow {
int fid;
int V;
int counter;
int time;
double arrReq; // the total energy request arrived at current slot
double qf;
double debet_queue;
double yf;
double xf; //total charged energy from out side at curren slot;
double debet2;
ArrayList<EV> d_ev;
ArrayList<Subflow> sfs;
public PoissonGen pg;
public NormalGen ng;
public Flow(int id, int v){
fid = id;
V = v;
arrReq =0;
qf = 0;
debet_queue =0;
yf=0;
time = 1;
counter=0;
// evList = new ArrayList<EV>();
sfs = new ArrayList<Subflow>(fid+1);
d_ev = new ArrayList<EV>();
debet2 = 0;
xf = 0;
}
public void genEV(int avg, String pro){
RandomStream rd = new MRG31k3p();
int arr_num=0;
if(pro=="uni")
{
arr_num = avg;
}else if(pro== "poi"){
pg = new PoissonGen(rd,avg);
arr_num = pg.nextInt();
}else if(pro=="gau"){
double sigma =1;
ng = new NormalGen(rd,avg,sigma);
arr_num = (int) Math.abs(ng.nextDouble());
}
ArrayList arrList = new ArrayList<EV>(arr_num);
arrReq = 0;
if(arr_num==0 ){
System.out.println("arr_num is 0" );
if(sfs.size()!=0)
sfs.set(0, null);
return;
}
for(int i=0;i<arr_num;i++){
int p =10;
EV ev = new EV(time,fid,p);
ev.capacity= 16 + Math.random()*(32-16);
// ev.capacity = 32;
ev.ini_eng = ev.capacity*Math.random()*0.8;
// ev.ini_eng = 12;
ev.current_eng = ev.ini_eng;
ev.fin_eng = ev.capacity*(Math.random()*0.2+ 0.8);
// ev.fin_eng = 32;
ev.req_eng = ev.fin_eng-ev.ini_eng;
ev.id = counter;
counter = counter+1;
arrList.add(ev);
arrReq = arrReq +ev.req_eng;
}
Subflow arr = new Subflow(fid,fid,V);
arr.q = arrReq;
arr.evList.addAll(arrList);
if(sfs.size()==0){
sfs.add(arr);
System.out.println("first generate");
}
else
sfs.set(0, arr);
}
/*
* schedule the subflows
*/
public void scheduling(double wind,double gamma){
/*
for(Subflow e: sfs){
e.time = time;
}
*/
xf =0;
yf = 0;
for(Subflow e:sfs){
if(e!=null && e.evList.size()>0){
e.time = time;
e.schedule(wind/(double) fid,gamma);
xf = xf+ e.x;
}
// yf = yf+e.y;
}
}
public void queueEvolution(){
int inisize = sfs.size();
if(inisize ==0)
return;
for(int i=0; i<inisize && i<= fid;i++){
Subflow e = sfs.get(i);
if(e!=null){
double tmp_q = e.q;
e.q = e.q-e.y;
if(e.q<0){
System.out.println("q at evo "+tmp_q);
System.out.println("y at evo"+e.y);
}
}
}
if(inisize<=fid){ //increase the size
Subflow tmp = sfs.get(0);
sfs.add(tmp);
}
int size = sfs.size();
for(int i=0;i<size-1;i++){
sfs.set(size-i-1, sfs.get(size-i-2));
}
if(inisize==fid+1){
Subflow debet = sfs.get(fid);
if(debet!=null){
debet_queue = debet_queue+ debet.q; //update debet queue
d_ev.addAll(debet.evList);
double a =0;
for(EV e: debet.evList){
a = a+ e.fin_eng-e.current_eng;
}
if(Math.abs(debet.q-a) > 0.01){
System.out.println("error");
System.out.println("q is "+ debet.q);
System.out.println("a is "+ a);
}
debet2 = debet2+a;
}
}
}
public void run(int time, int avg, double wp, double gamma){
// genEV(avg, "uni");
genEV(avg, "poi");
scheduling(wp, gamma);
queueEvolution();
}
}