00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _REVOLVE_H
00014 #define _REVOLVE_H
00015
00016 #include <vector>
00017
00018 using namespace std;
00019
00020 #define checkup 10000
00021 #define repsup 6400
00022 #define MAXINT 2147483647
00023
00024 namespace ACTION
00025 {
00026 enum action { advance, takeshot, restore, firsturn, youturn, terminate, error} ;
00027 }
00028
00029
00030
00053 class Checkpoint
00054 {
00055 public:
00056
00057 Checkpoint(int s) { snaps=s; ch.reserve(snaps); number_of_writes.reserve(snaps); number_of_reads.reserve(snaps); }
00058
00059 vector <int> ch;
00060 vector <int> ord_ch;
00061 vector <int> number_of_writes;
00062 vector <int> number_of_reads;
00063 void print_ch(ostream &file) { file << endl; for(int i=0;i<snaps;i++) file << ch[i] << " "; }
00064 void print_ord_ch(ostream &file) { file << endl; for(int i=0;i<snaps;i++) file << ord_ch[i] << " "; }
00065 void init_ord_ch() { ord_ch.reserve(snaps); }
00066
00067 int advances, takeshots, commands;
00068 ~Checkpoint() { ch.clear(); ord_ch.clear(); number_of_writes.clear(); number_of_reads.clear();};
00069
00070 private:
00071
00072 int snaps;
00073
00074 };
00075
00076
00077
00083 class Schedule
00084 {
00085 public:
00086
00089 Schedule(int sn,Checkpoint *c);
00090 Schedule(int sn) { snaps=sn; }
00091
00094 virtual ACTION::action revolve(ostream & str) = 0;
00101 int numforw(int steps, int snaps, ostream & str);
00103 virtual int get_capo()=0;
00105 virtual int get_fine()=0;
00107 virtual int get_check()=0;
00109 virtual void set_fine(int f)=0;
00111 virtual void set_capo(int c)=0;
00113 int get_snaps() { return snaps; }
00116 Checkpoint *get_CP() { return checkpoint; }
00117
00119 int get_advances() { return checkpoint->advances; }
00121 int get_shots() { return checkpoint->takeshots; }
00123 int get_commands() { return checkpoint->commands; }
00125 virtual int get_info(){ return info;}
00126
00127
00128 ~Schedule() { }
00129
00130 protected:
00131 Checkpoint *checkpoint;
00133 int snaps;
00134 int info;
00135 };
00136
00137
00143 class Online : public Schedule
00144 {
00145 public:
00146
00148 Online(int sn,Checkpoint *c,bool o);
00150 Online(Online &o);
00151
00152 ACTION::action revolve(ostream & str) { return ACTION::error;}
00153
00154
00156 int get_check() { return check; }
00157 int get_capo() { return capo; }
00158
00159
00160 int get_fine() { return -1; }
00161 void set_fine(int) {}
00163 bool get_output() { return output; }
00164 void set_capo(int c) { capo=c; }
00165
00166 ~Online();
00167
00168 protected:
00169
00171 int check;
00173 bool output;
00175 int capo;
00176
00177 };
00178
00179
00180
00187 class Online_r2 : public Online
00188 {
00189 public:
00190
00192 Online_r2(int sn,Checkpoint *c,bool o);
00194 Online_r2(Online &o);
00195
00197 ACTION::action revolve(ostream & str);
00198 int get_check() { return check; }
00199 int get_capo() { return capo; }
00200 int get_fine() { return -1; }
00201 bool get_output() { return output; }
00202 void set_fine(int) {}
00203
00204 ~Online_r2();
00205
00206 private:
00207
00208 int oldcapo_o,offset,incr,iter,t,oldind,old_f,ind;
00210 vector <int> num_rep;
00211
00212 };
00213
00214
00222 class Online_r3 : public Online
00223 {
00224 public:
00225
00227 Online_r3(int sn,Checkpoint *c);
00228 Online_r3(Online_r3 &o) ;
00229
00231 ACTION::action revolve(ostream & str);
00232
00234 int get_capo() { return capo; }
00236 int get_fine() { return -1; }
00237
00238 void set_fine(int) {}
00239
00241 int choose_cp(int number);
00243 void tdiff_akt();
00245 void akt_cp();
00246
00247
00248 int get_tdiff_end(int i) { return tdiff_end[i]; }
00249
00250 ~Online_r3();
00251
00252 protected:
00253
00254
00256 int forward;
00258 int ind_now;
00260 int cp;
00262 vector <int> ch3;
00264 vector <int> tdiff;
00266 vector <int> tdiff_end;
00268 vector <bool> cp_fest;
00269 };
00270
00271
00272
00273
00280 class Arevolve : public Online
00281 {
00282 public:
00283
00285 Arevolve(int sn,Checkpoint *c, ostream & str);
00287 Arevolve(Arevolve &o);
00288
00290 int tmin(int steps, int snaps, ostream & str);
00292 int sumtmin();
00294 int mintmin();
00296 int get_fine() { return fine; }
00298 void akt_cp(int cp);
00299
00300 void set_fine(int f) { fine=f; }
00301
00303 enum ACTION::action revolve(ostream & str);
00304
00305 ~Arevolve() { }
00306
00307 private:
00308
00309 int checkmax;
00310 int fine,oldfine,newcapo,oldcapo;
00311 ostream & str;
00312
00313 };
00314
00321 class Moin : public Online
00322 {
00323 public:
00324
00326 Moin(int sn,Checkpoint *c);
00328 Moin(Moin &o);
00329
00330 bool is_dispensable(int *index);
00331 int get_lmin();
00332 void adjust_cp(int index);
00334 enum ACTION::action revolve(ostream & str);
00335 int get_fine() { return -1; }
00336 void set_fine(int) {}
00337
00338 ~Moin() { }
00339
00340 private:
00341
00342 vector<int> l;
00343 vector<bool> d;
00344 bool start,start1,is_d;
00345 int forward,lmin,index_old;
00346
00347 };
00348
00349
00350
00356 class Offline : public Schedule
00357 {
00358 public:
00359
00361 Offline(int st,int sn,Checkpoint *c, ostream & str);
00363 Offline(int sn,Checkpoint *c,Online *o,int f, ostream & str);
00365 Offline(Schedule *o, ostream & str) ;
00367 Offline(Offline &o);
00368
00371 ACTION::action revolve(ostream & str);
00372
00373 int get_check() { return check; }
00374 int get_capo() { return capo; }
00375 int get_fine() { return fine; }
00376 int get_snaps() { return snaps; }
00377 int get_commands() { return checkpoint->commands; }
00378 int get_steps() { return steps; }
00379 bool get_online() { return online; }
00380 vector <int> get_num_ch() { return num_ch; }
00381 int get_num_ch(int i) { return num_ch[i]; }
00382
00383 void set_fine(int f) { fine=f;}
00384 void set_capo(int c) { capo=c; }
00385
00386
00387 ~Offline() { };
00388
00389 private:
00390
00391 int check, steps, oldsnaps, oldfine, capo, fine, turn,ind ;
00392 vector <int> num_ch;
00393 bool online;
00394 ostream & str;
00395 };
00396
00397
00406 class Revolve
00407 {
00408 public:
00410 Revolve(int st,int sn,ostream & _str);
00412 Revolve(int st,int sn,int sn_ram, ostream & _str);
00414 Revolve(int sn, ostream & _str);
00415
00419 ACTION::action revolve(int* check,int* capo,int* fine,int snaps,int* info,bool *where_to_put);
00420 ACTION::action revolve(int* check,int* capo,int* fine,int snaps,int* info);
00421 ACTION::action revolve();
00422
00424 int adjust(int steps, ostream & str);
00426 int maxrange(int ss, int tt, ostream & str);
00428 double expense(int steps, int snaps, ostream & str);
00430 int numforw(int steps, int snaps, ostream & str) { return f->numforw(steps,snaps,str); }
00432 void turn(int fine);
00433
00434
00435 vector <int> get_write_and_read_counts();
00436 int get_number_of_writes_i(int l, int c,int i);
00437 int get_number_of_reads_i(int l, int c,int i);
00438
00439
00440 int getadvances() { return f->get_advances(); }
00441 int getcheck() { return check; }
00442 int getcheckram() { return indizes_ram[check]; }
00443 int getcheckrom() { return indizes_rom[check]; }
00444 int getcapo() { return capo; }
00445 int getfine() { return fine; }
00446 int getinfo() { return info; }
00447 int getoldcapo() { return oldcapo; }
00448 bool getwhere() { return where_to_put; }
00449 void set_info(int inf) { info=inf; }
00450 void number_of_writes() { str << endl; for(int i=0;i<snaps;i++) str << checkpoint->number_of_writes[i] << " "; }
00451 void number_of_reads() { str << endl; for(int i=0;i<snaps;i++) str << checkpoint->number_of_reads[i] << " "; }
00452
00453 int get_r(int steps,int snaps, ostream & str);
00454 int get_r(ostream & str);
00455
00456 ~Revolve() { delete checkpoint; }
00457
00458 private:
00459
00460 int check,capo,fine,snaps,info,steps,r,oldcapo;
00461 Schedule *f;
00462 bool online, multi, where_to_put;
00463 Checkpoint *checkpoint;
00464 vector <bool> where;
00465 vector <int> indizes_ram,indizes_rom;
00466
00467 ostream & str;
00468 };
00469
00470
00471 #endif