Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- #define Population 10000
- #define Mutation 300
- #define Population_Childs 40// always Population_Childs is Constant^2
- #define Generation 500
- #define Difference 10
- #define MAX_REPEAT 40
- using namespace std;
- vector <int> Tasks;
- int Processor_Number, Task_Number, Time_Suma = 0, Number = 0;//Number is a number of childs
- struct Time{
- int Value;
- int Index;
- };
- Time Time_Tab[Population_Childs * Population_Childs];
- bool sortByTime(Time A, Time B){
- return A.Value < B.Value;
- }
- void Parents_produce(int ** Parents_Tab){
- for(int i = 0; i < Population; i++){
- for(int j = 0; j < Task_Number; j++){
- Parents_Tab[i][j] = rand() % Processor_Number;//which processor takes j-th task in i-th solution
- }
- }
- }
- void Childs_produce(int ** Parents_Tab, int ** Childs_Tab){
- Number = 0;
- for(int i = 0; i < Population_Childs; i++){
- for(int j = 0; j < Population_Childs; j++){
- if(i != j){
- int Border = rand() % Task_Number;
- for(int l = 0; l < Task_Number; l++){
- if(rand() % Mutation == 2){
- Childs_Tab[Number][l] = rand() % Processor_Number;
- }
- else{
- if(l < Border)//outbreeding parents, what if i is equal to j?
- Childs_Tab[Number][l] = Parents_Tab[i][l];
- else
- Childs_Tab[Number][l] = Parents_Tab[j][l];
- }
- }
- Number++;
- }
- }
- }
- }
- void Processing_Time(int ** Childs_Tab) {
- int Maks;
- int Time_Task[Processor_Number];// set it to 0
- for(int i = 0; i < Number; i++){
- for(int j = 0; j < Processor_Number; j++)
- Time_Task[j] = 0;
- for(int j = 0; j < Task_Number; j++){
- Time_Task[Childs_Tab[i][j]] += Tasks[j];// estimate time of each processor
- }
- Maks = Time_Task[0];
- for(int j = 1; j < Processor_Number; j++){
- if(Time_Task[j] > Maks)
- Maks = Time_Task[j];
- }
- Time_Tab[i].Value = Maks;//estimate maximum from all processors, which is time of computation
- Time_Tab[i].Index = i;
- }
- sort(Time_Tab, Time_Tab + Number, sortByTime);// does it sort properly? Yes it does.
- }
- void Parents_choose_new(int ** Parents_Tab, int ** Childs_Tab){
- int Maks_Parent, Counter = 0;
- int Time_Task[Processor_Number];// set it to 0
- for(int j = 0; j < Processor_Number; j++)
- Time_Task[j] = 0;
- for(int j = 0; j < Task_Number; j++){
- Time_Task[Parents_Tab[0][j]] += Tasks[j];// estimate time of each processor for parent, to find difference
- }
- Maks_Parent = Time_Task[0];
- for(int j = 1; j < Processor_Number; j++){
- if(Time_Task[j] > Maks_Parent)
- Maks_Parent = Time_Task[j];
- }
- Processing_Time(Childs_Tab);
- while(Maks_Parent - Time_Tab[0].Value < Difference && Counter < MAX_REPEAT){
- Childs_produce(Parents_Tab, Childs_Tab);
- Processing_Time(Childs_Tab);
- Counter++;
- }
- /*cout << "hahaha" << Counter << endl;*/
- for(int i = 0; i < Population_Childs; i++){
- for(int j = 0; j < Task_Number; j++)
- Parents_Tab[i][j] = Childs_Tab[Time_Tab[i].Index][j];
- }
- for(int i = 0; i < 4; i++)
- cout << Time_Tab[i].Index << " " << Time_Tab[i].Value << endl;
- cout << endl;
- }
- int main(){
- srand(time(NULL));
- ifstream plik;
- ofstream plik_1;
- plik.open("scheduling-input5");
- plik_1.open("wykres", ios::app);
- plik >> Processor_Number >> Task_Number;
- int ** Parents_Tab = new int * [Population];
- for(int i = 0; i < Population; i++)
- Parents_Tab[i] = new int [Task_Number];
- int ** Childs_Tab = new int * [Population_Childs * Population_Childs];
- for(int i = 0; i < Population_Childs * Population_Childs; i++)
- Childs_Tab[i] = new int [Task_Number];
- for(int i = 0; i < Task_Number; i++){
- int Task;
- plik >> Task;
- Time_Suma += Task;
- Tasks.push_back(Task);
- }
- cout << Time_Suma / Processor_Number << endl;
- Parents_produce(Parents_Tab);
- for(int k = 0; k < Generation; k++){
- Childs_produce(Parents_Tab, Childs_Tab);
- Parents_choose_new(Parents_Tab, Childs_Tab);
- }
- plik_1 << /*Population << " " << Number << " " <<*/ Time_Tab[0].Value /*<< " " << Time_Suma / Processor_Number*/ << endl;
- for(int i = 0; i < Population; i++)
- delete [] Parents_Tab[i];
- delete Parents_Tab;
- for(int i = 0; i < Population_Childs * Population_Childs; i++)
- delete [] Childs_Tab[i];
- delete Childs_Tab;
- plik.close();
- plik_1.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement