Advertisement
patryk

Cos tam dziala - czyli genetyk w fazie produkcji

Jan 8th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 48.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <time.h>
  7. #include <string.h>
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <windows.h>
  11.  
  12.  
  13. using namespace std;
  14.  
  15.  
  16. //------------TASK-STRUCTURE-----------
  17. struct Task{
  18.     int start_time;
  19.     int time_length;
  20.     int real_number;
  21.     bool isPause;
  22.     Task *next;
  23.  
  24.     Task();
  25.     void add(int start_time, int time_length, int real_number, bool isPause, Task * &head);
  26. };
  27.  
  28. Task::Task() {}
  29.  
  30. void Task::add(int start_time, int time_length, int real_number, bool isPause, Task * &head) {
  31.     Task *new_task = new Task;
  32.     Task *temp;
  33.  
  34.     new_task->start_time = start_time;
  35.     new_task->time_length = time_length;
  36.     new_task->real_number = real_number;
  37.     new_task->isPause = isPause;
  38.     new_task->next = NULL;
  39.  
  40.     temp = head;
  41.     if(temp) {
  42.         while(temp->next) temp = temp->next;
  43.         temp->next = new_task;
  44.     } else head = new_task;
  45. }
  46.  
  47.  
  48. //------------SOLUTION-STRUCTURE---------------
  49. struct Solution {
  50.     Task *machine_1_sequence;
  51.     Task *machine_2_sequence;
  52.     int fitness_value;
  53.     Solution *next;
  54.  
  55.     Solution();
  56.     void add(Solution * &head);
  57. };
  58.  
  59. Solution::Solution() {}
  60.  
  61. void Solution::add(Solution * &head) {
  62.     Solution *new_solution = new Solution;
  63.     Solution *temp;
  64.  
  65.     new_solution->machine_1_sequence = NULL;
  66.     new_solution->machine_2_sequence = NULL;
  67.     new_solution->fitness_value = 0;
  68.  
  69.  
  70.     new_solution->next = NULL;
  71.  
  72.     temp = head;
  73.     if(temp) {
  74.         while(temp->next) temp = temp->next;
  75.         temp->next = new_solution;
  76.     } else head = new_solution;
  77. }
  78.  
  79.  
  80. //-----------POPULATION-STRUCTURE--------------
  81. struct Population {
  82.     Solution *solution;
  83.  
  84.     Population();
  85.     void add_solution(Solution * &old_solution);
  86.     int population_size();
  87. };
  88.  
  89. Population::Population() {
  90.     solution = NULL;
  91. }
  92.  
  93. void Population::add_solution(Solution * &old_solution) {
  94.     Task *machine1_task = old_solution->machine_1_sequence;
  95.     Task *machine2_task = old_solution->machine_2_sequence;
  96.  
  97.     solution->add(solution);
  98.     Solution *new_solution = solution;
  99.  
  100.     while(new_solution->next) new_solution = new_solution->next;
  101.  
  102.     while(machine1_task) {
  103.         new_solution->machine_1_sequence->add(machine1_task->start_time, machine1_task->time_length, machine1_task->real_number, false, new_solution->machine_1_sequence);
  104.         machine1_task = machine1_task->next;
  105.     }
  106.  
  107.     while(machine2_task) {
  108.         new_solution->machine_2_sequence->add(machine2_task->start_time, machine2_task->time_length, machine2_task->real_number, false, new_solution->machine_2_sequence);
  109.         machine2_task = machine2_task->next;
  110.     }
  111.     new_solution->fitness_value = old_solution->fitness_value;
  112. }
  113.  
  114. int Population::population_size() {
  115.     Solution *temp_solution = solution;
  116.     int population_size = 0;
  117.  
  118.     while(temp_solution) {
  119.         population_size++;
  120.         temp_solution = temp_solution->next;
  121.     }
  122.  
  123.     return population_size;
  124. }
  125.  
  126.  
  127. //---------GLOBAL-VARIABLES----------------
  128. Task *machine1_operations;
  129. Task *machine2_operations;
  130.  
  131. Task *machine1_pauses;
  132. Task *machine2_pauses;
  133.  
  134.  
  135.  
  136. void swapTasks(Task *sequence, Task *task1, Task *task2) {
  137.  
  138.     Task *temp_sequence = sequence;
  139.     Task *task1_prev, *task2_prev;
  140.     Task *temp_task;
  141.  
  142. //--ZAMIANA-GDY-ZADANIE-PIERWSZE-JEST-NA-PIERWSZYM-MIEJSCU-USZEREGOWANIA
  143.     if(task1->real_number == temp_sequence->real_number) {
  144.  
  145.          if(task1->next == task2) {
  146.  
  147.             task1->next = task2->next;
  148.             task2->next = task1;
  149.             sequence = task2;
  150.  
  151.             int tempStartTime = task1->start_time;
  152.             task1->start_time = task2->start_time;
  153.             task2->start_time = tempStartTime;
  154.             return;
  155.         }
  156.  
  157.         while(temp_sequence) {
  158.             if(temp_sequence->next->real_number == task2->real_number) {
  159.                 task2_prev = temp_sequence;
  160.                     break;
  161.             }
  162.             temp_sequence = temp_sequence->next;
  163.         }
  164.  
  165.         temp_task = task1->next;
  166.         task1->next = task2->next;
  167.         task2->next = temp_task;
  168.  
  169.         task2_prev = task1;
  170.         sequence = task2;
  171.  
  172.         int tempStartTime = task1->start_time;
  173.         task1->start_time = task2->start_time;
  174.         task2->start_time = tempStartTime;
  175.         return;
  176.     }
  177.  
  178. //--ZAMIANA-GDY-ZADANIE-DRUGIE-JEST-NA-PIERWSZYM-MIEJSCU-USZEREGOWANIA
  179.     if(task2->real_number == temp_sequence->real_number) {
  180.  
  181.         if(task2->next == task1) {
  182.  
  183.             task2->next = task1->next;
  184.             task1->next = task2;
  185.             sequence = task1;
  186.  
  187.             int tempStartTime = task1->start_time;
  188.             task1->start_time = task2->start_time;
  189.             task2->start_time = tempStartTime;
  190.             return;
  191.         }
  192.  
  193.         while(temp_sequence) {
  194.                 if(temp_sequence->next->real_number == task1->real_number) {
  195.                     task1_prev = temp_sequence;
  196.                     break;
  197.                 }
  198.             temp_sequence = temp_sequence->next;
  199.         }
  200.  
  201.         temp_task = task1->next;
  202.         task1->next = task2->next;
  203.         task2->next = temp_task;
  204.  
  205.         task1_prev = task2;
  206.         sequence = task1;
  207.  
  208.         int tempStartTime = task1->start_time;
  209.         task1->start_time = task2->start_time;
  210.         task2->start_time = tempStartTime;
  211.         return;
  212.     }
  213.  
  214. //--ZAMIANA-GDY-ZADANIA-NASTEPUJA-JEDEN-PO-DRUGIM
  215.     if(task1->next == task2) {
  216.         while(temp_sequence) {
  217.             if (temp_sequence->next->real_number == task1->real_number) {
  218.                 task1_prev = temp_sequence;
  219.             }
  220.             temp_sequence = temp_sequence->next;
  221.         }
  222.         task1_prev = task2;
  223.  
  224.         temp_task = task2->next;
  225.         task2->next = task1;
  226.         task1->next = temp_task;
  227.  
  228.         int tempStartTime = task1->start_time;
  229.         task1->start_time = task2->start_time;
  230.         task2->start_time = tempStartTime;
  231.         return;
  232.     }
  233.  
  234.     if(task2->next == task1) {
  235.         while(temp_sequence) {
  236.             if (temp_sequence->next->real_number == task2->real_number) {
  237.                 task2_prev = temp_sequence;
  238.             }
  239.             temp_sequence = temp_sequence->next;
  240.         }
  241.         task2_prev = task1;
  242.  
  243.         temp_task = task1->next;
  244.         task1->next = task2;
  245.         task2->next = temp_task;
  246.  
  247.         int tempStartTime = task1->start_time;
  248.         task1->start_time = task2->start_time;
  249.         task2->start_time = tempStartTime;
  250.         return;
  251.     }
  252.  
  253. //--ZAMIANA-W-KAZDYM-INNYM-PRZYPADKU
  254.     while(temp_sequence) {
  255.  
  256.         if(!temp_sequence->next) break;
  257.         if(temp_sequence->next->real_number == task1->real_number) task1_prev = temp_sequence;
  258.         if(temp_sequence->next->real_number == task2->real_number) task2_prev = temp_sequence;
  259.  
  260.         temp_sequence = temp_sequence->next;
  261.     }
  262.  
  263.     task1_prev = task2;
  264.     task2_prev = task1;
  265.  
  266.     temp_task = task1->next;
  267.     task1->next = task2->next;
  268.     task2->next = temp_task;
  269.  
  270.     int tempStartTime = task1->start_time;
  271.     task1->start_time = task2->start_time;
  272.     task2->start_time = tempStartTime;
  273. }
  274.  
  275. Task *takeFromSequence(Task *sequence, Task *task2){
  276.  
  277.     Task *temp;
  278.     Task *takenTask;
  279.     temp = sequence;
  280.  
  281.     if(task2 == sequence){
  282.         if(sequence){
  283.             takenTask = sequence;
  284.             sequence = sequence->next;
  285.         }
  286.  
  287.     } else {
  288.  
  289.         while(temp->next != task2){
  290.             temp = temp->next;
  291.         }
  292.  
  293.         takenTask = temp->next;
  294.         temp->next = task2->next;
  295.  
  296.     }
  297.     return takenTask;
  298. }
  299.  
  300.  
  301. bool isCollideWithPause(Task *task, Task *pauses){
  302.  
  303.     Task *pause = pauses;
  304.  
  305.     while(pause){
  306.         if( ( (task->start_time > pause->start_time) && (task->start_time < pause->start_time + pause->time_length) ) ||
  307.         ( (task->start_time + task->time_length > pause->start_time) && (task->start_time + task->time_length < pause->start_time + pause->time_length) ) ){
  308.             return true;
  309.         }
  310.         pause = pause->next;
  311.     }
  312.  
  313.     return false;
  314. }
  315.  
  316.  
  317. int returnPossibleStartTime(Solution *solution, Task *task){
  318.  
  319.     Task *next_task = solution->machine_1_sequence;
  320.  
  321.     while (next_task->real_number != task->real_number) {
  322.         next_task = next_task->next;
  323.         if(!next_task) return 0;
  324.  
  325.     }
  326.     return next_task->start_time + next_task->time_length;
  327.  
  328. }
  329.  
  330.  
  331. void insertToSequence(Solution *solution, Task *task, int machine_number) {
  332.  
  333.     if(machine_number == 1) {
  334.  
  335.         int task_start_time = 0;
  336.         int task_finish_time = task_start_time + task->time_length;
  337.         bool checkEverythingAgain = true;
  338.  
  339.         while(checkEverythingAgain) {
  340.             checkEverythingAgain = false;
  341.  
  342.             bool checkTasksAgain = true;
  343.             while(checkTasksAgain) {
  344.                 Task *solution_task = solution->machine_1_sequence;
  345.                 while(solution_task) {
  346.                     for(int i = task_start_time ; i <= task_finish_time ; i++) {
  347.                         if(i >= solution_task->start_time && i <= solution_task->start_time + solution_task->time_length) {
  348.                             task_start_time = solution_task->start_time + solution_task->time_length + 1;
  349.                             task_finish_time = task_start_time + task->time_length;
  350.                             checkTasksAgain = true;
  351.                             checkEverythingAgain = true;
  352.                             break;
  353.                         }
  354.                     }
  355.                     checkTasksAgain = false;
  356.                     solution_task = solution_task->next;
  357.                 }
  358.             }
  359.  
  360.             bool checkPausesAgain = true;
  361.             while(checkPausesAgain) {
  362.                 Task *pauseM1 = machine1_pauses;
  363.                 while(pauseM1) {
  364.                     for(int i = task_start_time ; i <= task_finish_time ; i++) {
  365.                         if(i >= pauseM1->start_time && i <= pauseM1->start_time + pauseM1->time_length) {
  366.                             task_start_time = pauseM1->start_time + pauseM1->time_length + 1;
  367.                             task_finish_time = task_start_time + task->time_length;
  368.                             checkPausesAgain = true;
  369.                             checkEverythingAgain = true;
  370.                             break;
  371.                         }
  372.                     }
  373.                     checkPausesAgain = false;
  374.                     pauseM1 = pauseM1->next;
  375.                 }
  376.             }
  377.         }
  378.  
  379.         Task *solution_task = solution->machine_1_sequence;
  380.         Task *new_task = new Task;
  381.         new_task->start_time = task_start_time;
  382.         new_task->real_number = task->real_number;
  383.         new_task->isPause = false;
  384.         new_task->time_length = task->time_length;
  385.         new_task->next=NULL;
  386.  
  387.         if(task_start_time < solution_task->start_time) {
  388.  
  389.             new_task->next = solution_task;
  390.             solution_task = new_task;
  391.  
  392.         } else {
  393.             while(solution_task) {
  394.                 if(!solution_task->next) {
  395.                     solution_task->next = new_task;
  396.                     break;
  397.                 }
  398.  
  399.                 if(solution_task->next->start_time > task_start_time) {
  400.  
  401.                     Task *temp_task = solution_task->next;
  402.                     solution_task->next = new_task;
  403.                     new_task->next = temp_task;
  404.                     break;
  405.                 }
  406.                 solution_task = solution_task->next;
  407.             }
  408.         }
  409.       //  cout << "left";
  410.       //  cout << task->real_number << " : " << task_start_time << endl;
  411.     } else if(machine_number == 2) {
  412.  
  413.         int task_start_time = returnPossibleStartTime(solution, task);
  414.         int task_finish_time = task_start_time + task->time_length;
  415.         bool checkEverythingAgain = true;
  416.  
  417.         while(checkEverythingAgain) {
  418.             checkEverythingAgain = false;
  419.  
  420.             bool checkTasksAgain = true;
  421.             while(checkTasksAgain) {
  422.                 Task *solution_task = solution->machine_2_sequence;
  423.                 while(solution_task) {
  424.                     for(int i = task_start_time ; i <= task_finish_time ; i++) {
  425.                         if(i >= solution_task->start_time && i <= solution_task->start_time + solution_task->time_length) {
  426.                             task_start_time = solution_task->start_time + solution_task->time_length + 1;
  427.                             task_finish_time = task_start_time + task->time_length;
  428.                             checkTasksAgain = true;
  429.                             checkEverythingAgain = true;
  430.                             break;
  431.                         }
  432.                     }
  433.                     checkTasksAgain = false;
  434.                     solution_task = solution_task->next;
  435.                 }
  436.             }
  437.  
  438.             bool checkPausesAgain = true;
  439.             while(checkPausesAgain) {
  440.                 Task *pauseM2 = machine2_pauses;
  441.                 while(pauseM2) {
  442.                     for(int i = task_start_time ; i <= task_finish_time ; i++) {
  443.                         if(i >= pauseM2->start_time && i <= pauseM2->start_time + pauseM2->time_length) {
  444.                             task_start_time = pauseM2->start_time + pauseM2->time_length + 1;
  445.                             task_finish_time = task_start_time + task->time_length;
  446.                             checkPausesAgain = true;
  447.                             checkEverythingAgain = true;
  448.                             break;
  449.                         }
  450.                     }
  451.                     checkPausesAgain = false;
  452.                     pauseM2 = pauseM2->next;
  453.                 }
  454.             }
  455.         }
  456.  
  457.         Task *solution_task = solution->machine_2_sequence;
  458.         Task *new_task = new Task;
  459.         new_task->start_time = task_start_time;
  460.         new_task->real_number = task->real_number;
  461.         new_task->isPause = false;
  462.         new_task->time_length = task->time_length;
  463.         new_task->next=NULL;
  464.  
  465.  
  466.         if(task_start_time < solution_task->start_time) {
  467.  
  468.            new_task->next = solution_task;
  469.             solution_task = new_task;
  470.  
  471.         } else {
  472.             while(solution_task) {
  473.                 if(!solution_task->next) {
  474.                     solution_task->next = new_task;
  475.                     break;
  476.                 }
  477.  
  478.                 if(solution_task->next->start_time > task_start_time) {
  479.  
  480.                     Task *temp_task = solution_task->next;
  481.                     solution_task->next = new_task;
  482.                     new_task->next = temp_task;
  483.                     break;
  484.                 }
  485.                 solution_task = solution_task->next;
  486.             }
  487.         }
  488.         //cout << task->real_number << " : " << task_start_time << endl;
  489.         //cout << "left";
  490.     }
  491.  
  492. }
  493.  
  494. void repair(Solution *solution, int numberOfTasks){
  495.  
  496.     Task *temp;
  497.     Task *pom;
  498.     Task *temp2;
  499.     bool tasks[numberOfTasks];
  500.  
  501. //--USUWANIE-POWIELEN-NA-MASZYNIE-1
  502.     for(int i = 0 ; i < numberOfTasks ; i++) tasks[i] = false;
  503.  
  504.     temp = solution->machine_1_sequence;
  505.     while(temp) {
  506.         if(tasks[temp->real_number]){
  507.             pom = temp->next;
  508.             delete takeFromSequence(solution->machine_1_sequence, temp);
  509.             temp = pom;
  510.         } else {
  511.             tasks[temp->real_number] = true;
  512.             temp = temp->next;
  513.         }
  514.     }
  515.  
  516. //--DOPISYWANIE-BRAKUJACYCH-ZADAN-DO-MASZYNY-1
  517.     for(int i = 0 ; i < numberOfTasks ; i++){
  518.  
  519.         if(!tasks[i]){
  520.             temp = machine1_operations;
  521.  
  522.             while(temp->real_number != i){
  523.                 temp = temp->next;
  524.             }
  525.  
  526.             insertToSequence(solution, temp, 1);
  527.         }
  528.     }
  529.  
  530.  
  531. //--USUWANIE-POWIELEN-NA-MASZYNIE-2
  532.     for(int i = 0 ; i < numberOfTasks ; i++) tasks[i] = false;
  533.  
  534.     temp = solution->machine_2_sequence;
  535.     while(temp) {
  536.         if(tasks[temp->real_number]){
  537.             pom = temp->next;
  538.             delete takeFromSequence(solution->machine_2_sequence, temp);
  539.             temp = pom;
  540.         } else {
  541.             tasks[temp->real_number] = true;
  542.             temp = temp->next;
  543.         }
  544.     }
  545.  
  546. //--DOPISYWANIE-BRAKUJACYCH-ZADAN-NA-MASZYNIE-2
  547.     for(int i = 0 ; i < numberOfTasks ; i++){
  548.  
  549.         if(!tasks[i]) {
  550.             temp = machine2_operations;
  551.  
  552.             while(temp->real_number != i){
  553.                 temp = temp->next;
  554.             }
  555.  
  556.             insertToSequence(solution, temp, 2);
  557.         }
  558.     }
  559.  
  560. //--PRZESTAWIANIE-NIEPASUJACYCH-ZADAN-NA-MASZYNIE-1
  561.     temp = solution->machine_1_sequence;
  562.  
  563.     while(temp->next){
  564.  
  565.         if( (temp->start_time + temp->time_length > temp->next->start_time) || isCollideWithPause(temp, machine1_pauses) ){
  566.             temp2 = temp->next;
  567.             pom = takeFromSequence(solution->machine_1_sequence, temp);
  568.             Task *global_task=machine1_operations;
  569.  
  570.             while(global_task->real_number != pom->real_number) global_task = global_task->next;
  571.  
  572.             insertToSequence(solution, global_task, 1);
  573.             delete pom;
  574.             temp = temp2;
  575.  
  576.         } else temp=temp->next;
  577.  
  578.     }
  579.  
  580. //--PRZESTAWIANIE-NIEPASUJACYCH-ZADAN-NA-MASZYNIE-2
  581.     temp = solution->machine_2_sequence;
  582.     while(temp->next){
  583.         if( (temp->start_time + temp->time_length > temp->next->start_time) || isCollideWithPause(temp, machine2_pauses) || returnPossibleStartTime(solution, temp) < temp->start_time){
  584.             temp2 = temp->next;
  585.             pom = takeFromSequence(solution->machine_2_sequence, temp);
  586.             Task *global_task=machine2_operations;
  587.             while(global_task->real_number != pom->real_number) global_task = global_task->next;
  588.             insertToSequence(solution, global_task, 2);
  589.             delete pom;
  590.             temp = temp2;
  591.         } else temp = temp->next;
  592.     }
  593.  
  594. }
  595.  
  596.  
  597.  
  598.  
  599. void mutate(Solution *old_solution, int numberOfTasks, Population *population) {
  600.  
  601.     population->add_solution(old_solution);
  602.     Solution *new_solution = population->solution;
  603.     while(new_solution->next) new_solution = new_solution->next;
  604.  
  605.  
  606.     Task *z1_op1 = new_solution->machine_1_sequence;                //---WSKAZNIK-NA-ZADANIE-1-NA-MASZYNIE-1
  607.     Task *z2_op1 = new_solution->machine_1_sequence;                //---WSKAZNIK-NA-ZADANIE-2-NA-MASZYNIE-1
  608.     Task *z1_op2 = new_solution->machine_2_sequence;                //---WSKAZNIK-NA-ZADANIE-1-NA-MASZYNIE-2
  609.     Task *z2_op2 = new_solution->machine_2_sequence;                //---WSKAZNIK-NA-ZADANIE-2-NA-MASZYNIE-2
  610.  
  611.     int z1_rand = rand() % (numberOfTasks - 1);                           //---LOSOWANIE-NUMERU-ZADANIA-1-NA-MASZYNIE-1
  612.     for(int i = 0 ; i < z1_rand ; i++) z1_op1 = z1_op1->next;             //---PRZESUNIECIE-WSKAZNIKA-DO-TEGO-ZADANIA
  613.  
  614.  
  615.     int z2_rand = rand() % (numberOfTasks - 1);                               //---LOSOWANIE-NUMERU-ZADANIA-2-NA-MASZYNIE-1
  616.     while (z1_rand == z2_rand) z2_rand = rand() % (numberOfTasks - 1);         //---SPRAWDZANIE-CZY-NIE-WYLOSOWANO-TEGO-SAMEGO-ZADANIA
  617.  
  618.     for(int i = 0 ; i < z2_rand ; i++) z2_op1 = z2_op1->next;             //---PRZESUNIECIE-WSKAZNIKA-DO-TEGO-ZADANIA
  619.  
  620. //--WYSZUKANIE-TYCH-ZADAN-NA-MASZYNIE-DRUGIEJ-I-USTAWIENIE-WSKAZNIKA
  621.     while(z1_op2->real_number != z1_op1->real_number) z1_op2 = z1_op2->next;
  622.     while(z2_op2->real_number != z2_op1->real_number) z2_op2 = z2_op2->next;
  623.  
  624.     swapTasks(new_solution->machine_1_sequence, z1_op1, z2_op1);            //---ZAMIANA-ZADAN-NA-MASZYNIE-1
  625.     swapTasks(new_solution->machine_2_sequence, z1_op2, z2_op2);            //---ZAMIANA-ZADAN-NA-MASZYNIE-2
  626.  
  627.           Task * temp1 = new_solution->machine_1_sequence;
  628.             Task * temp2 = new_solution->machine_2_sequence;
  629.             int a = 0, b = 0;
  630.  
  631.             while(temp1) {
  632.                 a++;
  633.                 temp1 = temp1->next;
  634.             }
  635.  
  636.             while(temp2) {
  637.                 b++;
  638.                 temp2 = temp2->next;
  639.             }
  640.             cout << "M1: " << a << " M2: " << b << endl;
  641.             getch();
  642.  
  643.     repair(new_solution, numberOfTasks);               //---NAPRAWIENIE-EWENTUALNIE-POWSTALYCH-BLEDOW-USZEREGOWANIA
  644. }
  645.  
  646.  
  647.  
  648.  
  649.  
  650. void crossover(Solution *old_solution1, Population *old_population, int numberOfTasks, Population *population) {
  651.  
  652. //--SZUKANIE-DRUGIEGO-,-INNEGO-ROZWIAZANIA-ZE-STAREJ-POPULACJI
  653.     Solution *old_solution2;
  654.     do {
  655.         old_solution2 = old_population->solution;
  656.         int rand_second_solution = rand() % (old_population->population_size() - 1);
  657.         for(int i = 0 ; i < rand_second_solution ; i++) old_solution2 = old_solution2->next;
  658.     } while (old_solution1->fitness_value == old_solution2->fitness_value);
  659.  
  660. //--DODANIE-PIERWSZEGO-STAREGO-ROZWIAZANIA-DO-NOWEJ-POPULLACJI-I-ZNALEZIENIE-GO
  661.     population->add_solution(old_solution1);
  662.     Solution *new_solution1 = population->solution;
  663.     while(new_solution1->next) new_solution1 = new_solution1->next;
  664.  
  665. //--DODANIE-DRUGIEGO-STAREGO-ROZWIAZANIA-DO-NOWEJ-POPULACJI-I-ZNALEZIENIE-GO
  666.     population->add_solution(old_solution2);
  667.     Solution *new_solution2 = population->solution;
  668.     while(new_solution2->next) new_solution2 = new_solution2->next;
  669.  
  670. /*KRZYZOWANIE-NA-MASZYNIE-1*/
  671.     Task *start_task1 = new_solution1->machine_1_sequence;
  672.     Task *over_task1 = new_solution1->machine_1_sequence;
  673.     Task *start_task2 = new_solution2->machine_1_sequence;
  674.     Task *over_task2 = new_solution2->machine_1_sequence;
  675.     Task *before_start_task1 = new_solution1->machine_1_sequence;
  676.     Task *before_start_task2 = new_solution2->machine_1_sequence;
  677.     Task *after_over_task1;
  678.     Task *after_over_task2;
  679.  
  680.     int task_start_time;
  681.     int rand_start_range;
  682.     int rand_over_range;
  683.  
  684.     do {
  685.         rand_start_range = rand() % (numberOfTasks - 2) + 1;
  686.         rand_over_range = rand() % (numberOfTasks - 2) + 1;
  687.     } while(rand_start_range != rand_over_range || rand_start_range == rand_over_range + 1 || rand_start_range == rand_over_range - 1);
  688.  
  689.     if(rand_start_range > rand_over_range) {
  690.         int temp = rand_start_range;
  691.         rand_start_range = rand_over_range;
  692.         rand_over_range = temp;
  693.     }
  694.  
  695. //--PRZESUNIECIE-WSKAZNIKA-NA-POCZATEK-ZAKRESU
  696.     for(int i = 0 ; i < rand_start_range ; i++){
  697.         start_task1 = start_task1->next;
  698.         start_task2 = start_task2->next;
  699.     }
  700.  
  701. //--PRZESUNIECIE-WSKAZNIKA-NA-KONIEC-ZAKRESU
  702.     for(int i = 0 ; i < rand_over_range ; i++){
  703.         over_task1 = over_task1->next;
  704.         over_task2 = over_task2->next;
  705.     }
  706.  
  707.     while(before_start_task1->next != start_task1) before_start_task1 = before_start_task1->next;
  708.     while(before_start_task2->next != start_task2) before_start_task2 = before_start_task2->next;
  709.  
  710.     after_over_task1 = over_task1->next;
  711.     after_over_task2 = over_task2->next;
  712.  
  713.     task_start_time = start_task1->start_time;
  714.     start_task1->start_time = start_task2->start_time;
  715.     start_task2->start_time = task_start_time;
  716.  
  717.     before_start_task1->next = start_task2;
  718.     before_start_task2->next = start_task1;
  719.     over_task1->next = after_over_task2;
  720.     over_task2->next = after_over_task1;
  721.  
  722. //--AKTUALIZACJA-CZASOW-ROZPOCZECIA
  723.     before_start_task1 = start_task1;
  724.     while(before_start_task1 != over_task1) {
  725.        // cout << before_start_task1->next << endl;
  726.         before_start_task1->next->start_time = before_start_task1->start_time + before_start_task1->time_length;
  727.         before_start_task1 = before_start_task1->next;
  728.     }
  729.  
  730.     before_start_task2 = start_task2;
  731.     while(before_start_task2 != over_task2) {
  732.         before_start_task2->next->start_time = before_start_task2->start_time + before_start_task2->time_length;
  733.         before_start_task2 = before_start_task2->next;
  734.     }
  735.  
  736. /*KRZYZOWANIE-NA-MASZYNIE-2*/
  737.     start_task1 = new_solution1->machine_2_sequence;
  738.     start_task2 = new_solution2->machine_2_sequence;
  739.     before_start_task1 = new_solution1->machine_2_sequence;
  740.     before_start_task2 = new_solution2->machine_2_sequence;
  741.     over_task1=new_solution1->machine_2_sequence;
  742.     over_task2=new_solution2->machine_2_sequence;
  743. //--PRZESUNIECIE-WSKAZNIKA-NA-POCZATEK-ZAKRESU
  744.     for(int i = 0 ; i < rand_start_range ; i++){
  745.         start_task1 = start_task1->next;
  746.         start_task2 = start_task2->next;
  747.     }
  748.  
  749. //--PRZESUNIECIE-WSKAZNIKA-NA-KONIEC-ZAKRESU
  750.     for(int i = 0 ; i < rand_over_range ; i++){
  751.         over_task1 = over_task1->next;
  752.         over_task2 = over_task2->next;
  753.     }
  754.  
  755.     while(before_start_task1->next != start_task1) before_start_task1 = before_start_task1->next;
  756.     while(before_start_task2->next != start_task2) before_start_task2=before_start_task2->next;
  757.  
  758.     after_over_task1 = over_task1->next;
  759.     after_over_task2 = over_task2->next;
  760.  
  761.     task_start_time = start_task1->start_time;
  762.     start_task1->start_time = start_task2->start_time;
  763.     start_task2->start_time = task_start_time;
  764.  
  765.     over_task1->next = after_over_task2;
  766.     over_task2->next = after_over_task1;
  767.     before_start_task1->next = start_task2;
  768.     before_start_task2->next = start_task1;
  769.  
  770. //--AKTUALIZACJA-CZASOW-ROZPOCZECIA
  771.     before_start_task1 = start_task1;
  772.     while(before_start_task1 != over_task1){
  773.         before_start_task1->next->start_time = before_start_task1->start_time + before_start_task1->time_length;
  774.         before_start_task1 = before_start_task1->next;
  775.     }
  776.  
  777.     before_start_task2=start_task2;
  778.     while(before_start_task2 != over_task2){
  779.         before_start_task2->next->start_time = before_start_task2->start_time + before_start_task2->time_length;
  780.         before_start_task2 = before_start_task2->next;
  781.     }
  782.  
  783.             /*    Task * temp1 = solution1->machine_1_sequence;
  784.             Task * temp2 = solution1->machine_2_sequence;
  785.             int a = 0, b = 0;
  786.  
  787.             while(temp1) {
  788.                 a++;
  789.                 temp1 = temp1->next;
  790.             }
  791.  
  792.             while(temp2) {
  793.                 b++;
  794.                 temp2 = temp2->next;
  795.             }
  796.             cout << "M1: " << a << " M2: " << b << endl;
  797.             getch();
  798.  
  799.             temp1 = solution2->machine_1_sequence;
  800.             temp2 = solution2->machine_2_sequence;
  801.             a = 0, b = 0;
  802.  
  803.             while(temp1) {
  804.                 a++;
  805.                 temp1 = temp1->next;
  806.             }
  807.  
  808.             while(temp2) {
  809.                 b++;
  810.                 temp2 = temp2->next;
  811.             }
  812.             cout << "M1: " << a << " M2: " << b << endl;
  813.             getch();*/
  814.  
  815.     repair(new_solution1, numberOfTasks);
  816.     repair(new_solution2, numberOfTasks);
  817.  
  818. }
  819.  
  820.  
  821. //---------------------SELECTION-OPERATOR---------------------------
  822. void calculate_fitness(Population *population) {
  823.     Solution *solution = population->solution;                                      //---WSKAZNIK-NA-PIERWSZE-ROZWIAZANIE-W-POPULACJI
  824.     int maxFitnessValue = 0;                                                        //---ZMIENNA-PRZECHOWUJACA-WARTOSC-NAJWIEKSZEJ-FUNKCJI
  825.  
  826. //--DLA-KAZDEGO-ROZWIAZANIA-WYLICZANA-JEST-WARTOSC-FUNKCJI-CELU-(IM-WIEKSZA-WARTOSC-TYM-LEPSZE-USZEREGOWANIE)
  827.     while(solution) {
  828.         Task *machine1_task = solution->machine_1_sequence;                         //---WSKAZNIK-NA-ZADANIE-DLA-DANEGO-ROZWIAZANIA-DLA-MASZYNY-1
  829.         Task *machine2_task = solution->machine_2_sequence;                         //---WSKAZNIK-NA-ZADANIE-DLA-DANEGO-ROZWIAZANIA-DLA-MASZYNY-2
  830.         int fitnessValue = 0;                               //---WARTOSC-FUNKCJI-CELU
  831.  
  832. //------SUMOWANIE-CZASOW-ZAKONCZENIA-OPERACJI-DLA-MASZYNY-1
  833.         while(machine1_task) {
  834.             fitnessValue += machine1_task->start_time + machine1_task->time_length;
  835.             machine1_task = machine1_task->next;
  836.         }
  837.  
  838. //------SUMOWANIE-CZASOW-ZAKONCZENIA-OPERACJI-DLA-MASZYNY-2
  839.         while(machine2_task) {
  840.             fitnessValue += machine2_task->start_time + machine2_task->time_length;
  841.             machine2_task = machine2_task->next;
  842.         }
  843.  
  844. //------SPRAWDZANIE-CZY-AKTUALNA-WARTOSC-FUNKCJI-CELU-JEST-NAJWIEKSZA
  845.         if(fitnessValue > maxFitnessValue) {
  846.             maxFitnessValue = fitnessValue + 1;
  847.         }
  848.  
  849.         solution->fitness_value = fitnessValue;     //---ZAPISANIE-WARTOSCI-FUNKCJI-CELU-(TU-JESZCZE-WIEKSZA-WARTOSC-JEST-GORSZYM-USZEREGOWANIEM)
  850.         solution = solution->next;
  851.     }
  852.     solution = population->solution;
  853.  
  854. //--ZAMIANA-WARTOSCI-FUNKCJI-CELU-(IM-WIEKSZA-TERAZ-JEST-LEPSZYM-USZEREGOWANIEM)
  855.     while(solution) {
  856.         solution->fitness_value = maxFitnessValue - solution->fitness_value;
  857.         solution = solution->next;
  858.     }
  859. }
  860.  
  861. void roulette_selection(Population *population, int new_population_size) {
  862.     int sumOfFitnessValues = 0;
  863.     Solution *solution = population->solution;
  864.  
  865.     while(solution) {
  866.         sumOfFitnessValues += solution->fitness_value;
  867.         solution = solution->next;
  868.     }
  869.  
  870.     for(int i = 0 ; i < new_population_size ; i++) {
  871.         int rouletteMarble = rand();
  872.     }
  873.  
  874. }
  875.  
  876. void tournament_selection(Population *population, Population *new_population, int new_population_size) {
  877.     Solution *solution1;
  878.     Solution *solution2;
  879.     int population_size = population->population_size();
  880.     int solution1_number;
  881.     int solution2_number;
  882.  
  883.     int popArray[population_size];
  884.     for(int i = 0 ; i < population_size ; i++) {
  885.         popArray[i] = 0;
  886.     }
  887.  
  888.     for(int i = 0 ; i < new_population_size ; i++) {
  889.  
  890.         solution1 = population->solution;
  891.         solution2 = population->solution;
  892.  
  893.         while(true) {
  894.             solution1_number = rand() % (population_size - 1);
  895.             if(popArray[solution1_number] == 0) {
  896.                 popArray[solution1_number] = 1;
  897.                 break;
  898.             }
  899.         }
  900.  
  901.         while(true) {
  902.             solution2_number = rand() % (population_size - 1);
  903.             if(popArray[solution2_number] == 0) {
  904.                 popArray[solution2_number] = 1;
  905.                 break;
  906.             }
  907.         }
  908.  
  909.         for(int i = 0 ; i < solution1_number ; i++) solution1 = solution1->next;
  910.         for(int i = 0 ; i < solution2_number ; i++) solution2 = solution2->next;
  911.  
  912.         if(solution1->fitness_value > solution2->fitness_value) {
  913.             popArray[solution2_number] = 0;
  914.             new_population->add_solution(solution1);
  915.         } else {
  916.             popArray[solution1_number] = 0;
  917.             new_population->add_solution(solution2);
  918.         }
  919.     }
  920. }
  921.  
  922. void selection(Population *population, Population* new_population, int new_population_size) {
  923.  
  924.     calculate_fitness(population);
  925.     //roulette_selection(population, new_population_size);
  926.     tournament_selection(population, new_population, new_population_size);
  927. }
  928.  
  929. //-----------------------------------------------------------
  930. int generate_population(int numberOfTasks, int sizeOfPopulation, Population * &population) {
  931.     for(int i = 0 ; i < sizeOfPopulation ; i++) {
  932.  
  933.         population->solution->add(population->solution);            //---DODANIE-NOWEGO-ROZWIAZANIA-DO-LISTY-ROZWIAZAŃ
  934.         Solution *solution = population->solution;
  935.         for(int j = 0 ; j < i; j++) solution = solution->next;      //---PRZESUNIECIE-WSKAZNNIKA-DO-AKTUALNIE-GENEROWANEGO-ROZWIAZANIA
  936.  
  937.         int taskOrder[numberOfTasks], usedTasks[numberOfTasks];
  938.         for(int j = 0 ; j < numberOfTasks ; j++) usedTasks[j] = 0;
  939.  
  940. //------GENEROWANIE-KOLEJNOŚCI-ZADAŃ-NA-PIERWSZEJ-MASZYNIE
  941.         for(int j = 0 ; j < numberOfTasks ; j++) {
  942.             while(true){
  943.                 int taskNumber =(int) (rand()% numberOfTasks);
  944.                 if(usedTasks[taskNumber] == 0) {
  945.                     usedTasks[taskNumber] = 1;
  946.                     taskOrder[j] = taskNumber;
  947.                     break;
  948.                 }
  949.             }
  950.         }
  951.  
  952. //------DODAWANIE-TASKÓW-DO-AKTUALNIE-GENEROWANEGO-ROZWIAZANIA
  953.         for(int j = 0 ; j < numberOfTasks ; j++) {
  954.  
  955.  
  956.  
  957. /*__________GENEROWANIE-USZEREGOWANIA-DLA-MASZYNY-1*/
  958.             int taskNumber = taskOrder[j];                  //---RZECZYWISTY-NUMER-ZADANIA-KTORE-MA-ZOSTAC-DODANE-DO-ROZWIAZANIA
  959.             int task_start_timeM1 = 0;                      //---MOMENT-CZASU-W-KTORYM-AKTUALNIE-DODAWANE-ZADANIE-MOZE-ROZPOCZAC-WYKONYWANIE
  960.             Task *machine1Task, *sequenceM1;
  961.  
  962.             machine1Task = machine1_operations;             //---WSKAŹNIK-NA-ZADANIE-Z-LISTY-ZADAŃ-WCZYTANYCH-Z-PLIKU
  963.             sequenceM1 = solution->machine_1_sequence;      //---WSKAŹNIK-POMOCNICZY-NA-AKTUALNIE-GENEROWANE-ROZWIAZANIE-DLA-MASZYNY-PIERWSZEJ
  964.  
  965. //----------PRZESUNIECIE-WSKAZNIKA-NA-POZYCJE-KTOREMU-ODPOWIADA-WCZESNIEJ-WYGENEROWANY-NUMER-ZADANIA
  966.             for(int k = 0; k < taskNumber ; k++) {
  967.                 machine1Task = machine1Task->next;
  968.             }
  969.  
  970. //----------ZNALEZIENIE-NAJBLIZSZEGO-MOZLIWEGO-CZASU-KIEDY-ZADANIE-MOZE-ROZPOCZAC-WYKONYWANIE
  971.             while(sequenceM1) {
  972.                 task_start_timeM1 = sequenceM1->start_time + sequenceM1->time_length;
  973.                 sequenceM1 = sequenceM1->next;
  974.             }
  975.  
  976.             Task *pauseM1;                                  //---WSKAZNIK-NA-LISTE-PRZERW-DLA-MASZYNY-1
  977.             bool checkAgain = true;
  978.  
  979. //----------ZNAJDOWANIE-NOWEGO-MOZLIWEGO-CZASU-ROZPOCZECIA-WYKONYWANIA-ZADANIA-W-ZALEZNOSCI-OD-PRZERW
  980.             while(checkAgain){
  981.                 pauseM1 = machine1_pauses;
  982.                 while(pauseM1) {
  983.                     for(int k = task_start_timeM1 ; k <= task_start_timeM1 + machine1Task->time_length ; k++) {
  984.                         if(k >= pauseM1->start_time && k <= pauseM1->start_time + pauseM1->time_length) {
  985.                             task_start_timeM1 = pauseM1->start_time + pauseM1->time_length;
  986.                             checkAgain = true;
  987.                             break;
  988.                         }
  989.                     }
  990.                     checkAgain = false;
  991.                     pauseM1 = pauseM1->next;
  992.                 }
  993.             }
  994.             solution->machine_1_sequence->add(task_start_timeM1, machine1Task->time_length, machine1Task->real_number, false, solution->machine_1_sequence);
  995.  
  996.  
  997.  
  998. /*__________GENEROWANIE-USZEREGOWANIA-DLA-MASZYNY-2*/
  999.             Task *machine2Task, *sequenceM2;
  1000.             int task_start_timeM2 = 0;                      //---MOMENT-CZASU-W-KTORYM-AKTUALNIE-DODAWANE-ZADANIE-MOZE-ROZPOCZAC-WYKONYWANIE
  1001.  
  1002.             machine2Task = machine2_operations;             //---WSKAZNIK-NA-ZADANIE-Z-LISTY-ZADAN-WCZYTANYCH-Z-PLIKU
  1003.             sequenceM2 = solution->machine_2_sequence;      //---WSKAZNIK-POMOCNICZY-NA-AKTUALNIE-GENEROWANE-ROZWIAZANIE-DLA-MASZYNY-DRUGIEJ
  1004.  
  1005. //---------PRZESUNIECIE-WSKAZNIKA-NA-POZYCJE-KTOREMU-ODPOWIADA-WCZESNIEJ-WYGENEROWANY-NUMER-ZADANIA
  1006.             for(int k = 0 ; k < taskNumber ; k++) {
  1007.                 machine2Task = machine2Task->next;
  1008.             }
  1009.  
  1010. //---------ZNALEZIENIE-NAJBLIZSZEGO-MOZLIWEGO-CZASU-KIEDY-ZADANIE-MOZE-ROZPOCZAC-WYKONYWANIE
  1011.             while(sequenceM2) {
  1012.                 task_start_timeM2 = sequenceM2->start_time + sequenceM2->time_length;
  1013.                 sequenceM2 = sequenceM2->next;
  1014.             }
  1015.  
  1016. //----------SPRAWDZENIE-CZY-ZADANIE-NA-MASZYNIE-2-NIE-ZACZYNA-SIE-SZYBCIEJ-NIZ-KONCZY-SIE-ZADANIE-NA-MASZYNIE-1
  1017.             if(task_start_timeM2 <= task_start_timeM1 + machine1Task->time_length) {
  1018.                 task_start_timeM2 = task_start_timeM1 + machine1Task->time_length;
  1019.             }
  1020.  
  1021.             Task *pauseM2;                      //---WSKAZNIK-NA-LISTE-PRZERW-DLA-MASZYNY-2
  1022.             checkAgain = true;
  1023.  
  1024. //----------ZNAJDOWANIE-NOWEGO-MOZLIWEGO-CZASU-ROZPOCZECIA-WYKONYWANIA-ZADANIA-W-ZALEZNOSCI-OD-PRZERW
  1025.             while(checkAgain){
  1026.                 pauseM2 = machine2_pauses;
  1027.                 while(pauseM2) {
  1028.                     for(int k = task_start_timeM2 ; k <= task_start_timeM2 + machine2Task->time_length ; k++) {
  1029.                         if(k >= pauseM2->start_time && k <= pauseM2->start_time + pauseM2->time_length) {
  1030.                             task_start_timeM2 = pauseM2->start_time + pauseM2->time_length + 1;
  1031.                             checkAgain = true;
  1032.                             break;
  1033.                         }
  1034.                     }
  1035.                     checkAgain = false;
  1036.                     pauseM2 = pauseM2->next;
  1037.                 }
  1038.             }
  1039.  
  1040.             solution->machine_2_sequence->add(task_start_timeM2, machine2Task->time_length, machine2Task->real_number, false, solution->machine_2_sequence);
  1041.         }
  1042.     }
  1043.     return numberOfTasks;
  1044. }
  1045.  
  1046.  
  1047. int load_instance(char fileName[20]) {
  1048.     char buf[100];                  //---ZMIENNA-POMOCNICZA-PRZECHOWUJACA-DANE-NA-TEMAT-ZADAN-I-PRZERW
  1049.     int numberOfTasks;              //---ILOSC-ZDAN-KTORE-WYSTEPUJA-W-DANEJ-INSTANCJI-PROBLEMU
  1050.     fstream plik;
  1051.  
  1052.     plik.open(fileName, ios::in);
  1053.  
  1054.     if(plik.good()) {
  1055.         plik.getline(buf, 6);
  1056.         numberOfTasks = atoi(buf);  //---WCZYTANIE-ILOSCI-ZADAN-(PIERWSZY-WIERSZ-PLIKU)
  1057.  
  1058. //------WCZYTANIE-I-DODANIE-WSZYSTKICH-ZADAN-DO-LISTY-ZADAN
  1059.         for(int i = 0 ; i < numberOfTasks ; i++) {
  1060.             int machine1_task, machine2_task;
  1061.  
  1062.             plik.getline(buf, 10, ';');
  1063.             machine1_task = atoi(buf);
  1064.             plik.getline(buf, 10);
  1065.             machine2_task = atoi(buf);
  1066.  
  1067.             machine1_operations->add(0, machine1_task, i, false, machine1_operations);
  1068.             machine2_operations->add(0, machine2_task, i, false, machine2_operations);
  1069.         }
  1070.  
  1071. //------WCZYTANIE-I-DODANIE-PRZERW-DO-LISTY-PRZERW
  1072.         while(!plik.eof()) {
  1073.             int pauseNumber, machineNumber, timeForPause, whenPauseStart;
  1074.  
  1075.             plik.getline(buf, 10, ';');
  1076.             pauseNumber = atoi(buf);
  1077.             plik.getline(buf, 10, ';');
  1078.             machineNumber = atoi(buf);
  1079.             plik.getline(buf, 10, ';');
  1080.             timeForPause = atoi(buf);
  1081.             plik.getline(buf, 10);
  1082.             whenPauseStart = atoi(buf);
  1083.  
  1084.             if(timeForPause == 0 && machineNumber == 0 && whenPauseStart == 0) break;
  1085.  
  1086.             if(machineNumber == 0) machine1_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine1_pauses);
  1087.             if(machineNumber == 1) machine2_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine2_pauses);
  1088.         }
  1089.  
  1090.     } else cout << "Ups...Something went wrong.";
  1091.  
  1092.     plik.close();
  1093.  
  1094.     return numberOfTasks;
  1095. }
  1096.  
  1097. void save_results(Population *population, char fileName[20]) {
  1098.     char new_fileName[30] ="solution/";
  1099.     strncat(new_fileName, fileName, 20);
  1100.     calculate_fitness(population);
  1101.  
  1102.     Solution *best_solution = population->solution;
  1103.     Solution *solution = population->solution;
  1104.  
  1105.     while(solution) {
  1106.         if(solution->fitness_value > best_solution->fitness_value) {
  1107.             best_solution = solution;
  1108.         }
  1109.         solution = solution->next;
  1110.     }
  1111.  
  1112.     Task *machine1_seq = best_solution->machine_1_sequence;
  1113.     Task *machine2_seq = best_solution->machine_2_sequence;
  1114.     int fitness_value = 0;
  1115.     int maximum_time;
  1116.  
  1117.     while(machine1_seq) {
  1118.         fitness_value += machine1_seq->start_time + machine1_seq->time_length;
  1119.         machine1_seq = machine1_seq->next;
  1120.     }
  1121.  
  1122.     while(machine2_seq) {
  1123.         fitness_value += machine2_seq->start_time + machine2_seq->time_length;
  1124.         maximum_time = machine2_seq->start_time + machine2_seq->time_length;
  1125.         machine2_seq = machine2_seq->next;
  1126.     }
  1127.  
  1128.     machine1_seq = best_solution->machine_1_sequence;
  1129.     machine2_seq = best_solution->machine_2_sequence;
  1130.     fstream plik;
  1131.     plik.open(new_fileName, ios::out);
  1132.  
  1133.     plik << maximum_time << ";" << fitness_value << endl;
  1134.  
  1135. //-----------WPISYWANIE-USZEREGOWANIA-NA-MASZYNIE-1
  1136.     int actuallTimeMoment = 0;
  1137.     int pause_number_M1 = 1;
  1138.     int idle_pause_number_M1 = 1;
  1139.     int sum_pause_M1 = 0;
  1140.     int sum_idle_pause_M1 = 0;
  1141.  
  1142.     plik << "M1:";
  1143.     while(machine1_seq) {
  1144.         bool idle = true;
  1145.  
  1146.         if(machine1_seq->start_time == actuallTimeMoment) {
  1147.             plik << "o1_z" << machine1_seq->real_number << "," << machine1_seq->start_time << "," << machine1_seq->time_length << ";";
  1148.             actuallTimeMoment = machine1_seq->start_time + machine1_seq->time_length;
  1149.             machine1_seq = machine1_seq->next;
  1150.             idle = false;
  1151.         }
  1152.  
  1153.         Task *machine1_p = machine1_pauses;
  1154.         while(machine1_p) {
  1155.             if(machine1_p->start_time == actuallTimeMoment) {
  1156.                 plik << "maint" << pause_number_M1 << "_M1," << machine1_p->start_time << "," << machine1_p->time_length << ";";
  1157.                 pause_number_M1++;
  1158.                 sum_pause_M1 += machine1_p->time_length;
  1159.                 actuallTimeMoment = machine1_p->start_time + machine1_p->time_length;
  1160.                 idle = false;
  1161.             }
  1162.             machine1_p = machine1_p->next;
  1163.         }
  1164.         if(idle) {
  1165.             machine1_p = machine1_pauses;
  1166.             int closest_Pause = MAXINTATOM;
  1167.             int closest_Task = MAXINTATOM;
  1168.  
  1169.             Task *temp_machine1_seq = best_solution->machine_1_sequence;
  1170.             while(temp_machine1_seq) {
  1171.                 if(temp_machine1_seq->start_time >= actuallTimeMoment) {
  1172.                     closest_Task = temp_machine1_seq->start_time;
  1173.                     break;
  1174.                 }
  1175.                 temp_machine1_seq = temp_machine1_seq->next;
  1176.             }
  1177.  
  1178.             while(machine1_p) {
  1179.                 if(machine1_p->start_time < closest_Pause && machine1_p->start_time >= actuallTimeMoment) closest_Pause = machine1_p->start_time;
  1180.                 machine1_p = machine1_p->next;
  1181.             }
  1182.  
  1183.             if(closest_Task < closest_Pause) {
  1184.                 plik << "idle" << idle_pause_number_M1 << "_M1," << actuallTimeMoment << "," << closest_Task - actuallTimeMoment << ";";
  1185.                 idle_pause_number_M1++;
  1186.                 sum_idle_pause_M1 += closest_Task - actuallTimeMoment;
  1187.                 actuallTimeMoment = closest_Task;
  1188.             } else {
  1189.                 plik << "idle" << idle_pause_number_M1 << "_M1," << actuallTimeMoment << "," << closest_Pause - actuallTimeMoment << ";";
  1190.                 idle_pause_number_M1++;
  1191.                 sum_idle_pause_M1 += closest_Pause - actuallTimeMoment;
  1192.                 actuallTimeMoment = closest_Pause;
  1193.             }
  1194.         }
  1195.     }
  1196.     plik << endl;
  1197.  
  1198.  
  1199. //-----------WPISYWANIE-USZEREGOWANIA-NA-MASZYNIE-2
  1200.     actuallTimeMoment = 0;
  1201.     int pause_number_M2 = 1;
  1202.     int idle_pause_number_M2 = 1;
  1203.     int sum_pause_M2 = 0;
  1204.     int sum_idle_pause_M2 = 0;
  1205.  
  1206.     plik << "M2:";
  1207.     while(machine2_seq) {
  1208.         bool idle = true;
  1209.  
  1210.         if(machine2_seq->start_time == actuallTimeMoment) {
  1211.             plik << "o2_z" << machine2_seq->real_number << "," << machine2_seq->start_time << "," << machine2_seq->time_length << ";";
  1212.             actuallTimeMoment = machine2_seq->start_time + machine2_seq->time_length;
  1213.             machine2_seq = machine2_seq->next;
  1214.             idle = false;
  1215.         }
  1216.  
  1217.         Task *machine2_p = machine2_pauses;
  1218.  
  1219.         while(machine2_p) {
  1220.             if(machine2_p->start_time == actuallTimeMoment) {
  1221.                 plik << "maint" << pause_number_M2 << "_M2," << machine2_p->start_time << "," << machine2_p->time_length << ";";
  1222.                 pause_number_M2++;
  1223.                 sum_pause_M2 += machine2_p->time_length;
  1224.                 actuallTimeMoment = machine2_p->start_time + machine2_p->time_length;
  1225.                 idle = false;
  1226.             }
  1227.             machine2_p = machine2_p->next;
  1228.         }
  1229.         if(idle) {
  1230.             machine2_p = machine2_pauses;
  1231.             int closest_Pause = MAXINTATOM;
  1232.             int closest_Task = MAXINTATOM;
  1233.  
  1234.             Task *temp_machine2_seq = best_solution->machine_2_sequence;
  1235.             while(temp_machine2_seq) {
  1236.                 if(temp_machine2_seq->start_time >= actuallTimeMoment) {
  1237.                     closest_Task = temp_machine2_seq->start_time;
  1238.                     break;
  1239.                 }
  1240.                 temp_machine2_seq = temp_machine2_seq->next;
  1241.             }
  1242.  
  1243.             while(machine2_p) {
  1244.                 if(machine2_p->start_time < closest_Pause && machine2_p->start_time >= actuallTimeMoment) closest_Pause = machine2_p->start_time;
  1245.                 machine2_p = machine2_p->next;
  1246.             }
  1247.  
  1248.             if(closest_Task < closest_Pause) {
  1249.                 plik << "idle" << idle_pause_number_M2 << "_M2," << actuallTimeMoment << "," << closest_Task - actuallTimeMoment << ";";
  1250.                 idle_pause_number_M2++;
  1251.                 sum_idle_pause_M2 += closest_Task - actuallTimeMoment;
  1252.                 actuallTimeMoment = closest_Task;
  1253.             } else {
  1254.                 plik << "idle" << idle_pause_number_M2 << "_M2," << actuallTimeMoment << "," << closest_Pause - actuallTimeMoment << ";";
  1255.                 idle_pause_number_M2++;
  1256.                 sum_idle_pause_M2 += closest_Task - actuallTimeMoment;
  1257.                 actuallTimeMoment = closest_Pause;
  1258.             }
  1259.         }
  1260.     }
  1261.     plik << endl;
  1262.     plik << pause_number_M1 << "," << sum_pause_M1 << endl;
  1263.     plik << pause_number_M2 << "," << sum_pause_M2 << endl;
  1264.     plik << idle_pause_number_M1 << "," << sum_idle_pause_M1 << endl;
  1265.     plik << idle_pause_number_M2 << "," << sum_idle_pause_M2;
  1266.     plik.close();
  1267. }
  1268.  
  1269. double getTime() {
  1270.   long long f, t;
  1271.   QueryPerformanceFrequency( (PLARGE_INTEGER) & f );
  1272.   QueryPerformanceCounter( (PLARGE_INTEGER) & t);
  1273.   return (double)t / (double)f;
  1274. }
  1275.  
  1276. int main() {
  1277.  
  1278.     #define START_POPULATION_SIZE  50                    //---ROZMIAR-POPULACJI-STARTOWEJ
  1279.     #define MAIN_POPULATION_SIZE  30                     //---ROZMIAR-POPULACJI-DO-KTOREGO-ROZMIARU-OPERATOR-SELEKCJI-BEDZIE-ZMNIEJSZAC
  1280.     #define CROSSOVER_PROPABILITY 95                  //---PRAWDOPODOBIENSTWO-WYSTAPIENIA-KRZYZOWANIA
  1281.     #define MUTATION_PROPABILITY 55                     //---PRAWDOPODOBIENSTWO-WYSTAPIENIA-MUTACJI
  1282.  
  1283.     int numberOfFiles = 1;                  //---LICZBA-PLIKOW-ZAWIERAJACYCH-ROZNE-INSTANCJE-PROBLEMU
  1284.     char fileName[20];
  1285.  
  1286.     srand(time(NULL));
  1287.  
  1288.     for(int i = 1; i <= numberOfFiles; i++) {
  1289.         Population *population = new Population;        //---WSKAZNIK-NA-POPULACJE-STARTOWA
  1290.  
  1291.         itoa(i * 100, fileName, 10);
  1292.         strncat(fileName, "RANDOM.txt", 10);
  1293.  
  1294.         int numberOfTasks = generate_population(load_instance(fileName), START_POPULATION_SIZE, population);        //---GENEROWANIE-STARTOWEJ-POPULACJI
  1295.         double startTime = getTime();
  1296.         double currentTime;
  1297.  
  1298. //------PRZEZ-OKRESLONY-CZAS-WYKONYWANE-SA-OPERACJE-MUTACJI-KRZYZOWANIA-I-SELEKCJI
  1299.        // do {
  1300.             Population *new_population = new Population;                            //---UTWORZENIE-NOWEGO-MIEJSCA-W-PAMIECI-DLA-NOWEJ-POPULACJI
  1301. /* SEL */   selection(population, new_population, MAIN_POPULATION_SIZE);            //---SELEKCJA-STAREJ-POPULACJI-W-NOWA-POPULACJE
  1302.             delete population;
  1303.             population = new Population;
  1304.  
  1305. //----------WYKONWANIE-OPERACJI-MUTACJI-I-KRZYZOWANIA-DLA-KAZDEGO-ZADANIA
  1306.             Solution *solution = new_population->solution;
  1307.  
  1308.             while(solution) {
  1309.  
  1310.                 int crossoverPropability = (rand() % 100) + 1;
  1311. /* CRO */       if(crossoverPropability <= CROSSOVER_PROPABILITY) crossover(solution, new_population, numberOfTasks, population);
  1312.  
  1313.                 int mutationPropability = (rand() % 100) + 1;
  1314. /* MUT */       if(mutationPropability <= MUTATION_PROPABILITY) {mutate(solution, numberOfTasks, population); cout << "MUTATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";}
  1315.  
  1316.                 solution = solution->next;
  1317.  
  1318.             }
  1319.             delete new_population;
  1320.  
  1321.             currentTime = getTime();
  1322.         //    } while(currentTime - startTime < 10);
  1323.  
  1324.         save_results(population, fileName);         //---ZAPISANIE-WYNIKU-PRZEPROWADZENIA-ALGORYTMU-DO-PLIKU-TEKSTOWEGO
  1325.  
  1326.         Solution * temp;
  1327.         temp = population->solution;
  1328.  
  1329.             Task *temp1;
  1330.             temp1 = temp->machine_1_sequence;
  1331.  
  1332.         while(temp) {
  1333.             Task *temp1, *temp2;
  1334.             temp1 = temp->machine_1_sequence;
  1335.             temp2 = temp->machine_2_sequence;
  1336.             while(temp1) {
  1337.                 cout << temp1->start_time << ":" << temp1->start_time + temp1->time_length <<"\t";
  1338.                 temp1 = temp1->next;
  1339.             }
  1340.             cout << endl << endl << "_________________________________________________" << endl << endl;
  1341.             while(temp2) {
  1342.                 cout << temp2->start_time << ":" << temp2->start_time + temp2->time_length << "\t";
  1343.                 temp2 = temp2->next;
  1344.             }
  1345.             cout << endl << endl << endl << endl << temp->fitness_value << endl << endl << endl;
  1346.             temp = temp->next;
  1347.         }
  1348.     }
  1349.  
  1350.     return 0;
  1351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement