Advertisement
patryk

Untitled

Dec 30th, 2014
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.65 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.  
  11.  
  12. using namespace std;
  13.  
  14.  
  15. //------------TASK-STRUCTURE-----------
  16. struct Task{
  17.     int start_time;
  18.     int time_length;
  19.     int real_number;
  20.     bool isPause;
  21.     Task *next;
  22.  
  23.     Task();
  24.     void add(int start_time, int time_length, int real_number, bool isPause, Task * &head);
  25. };
  26.  
  27. Task::Task() {}
  28.  
  29. void Task::add(int start_time, int time_length, int real_number, bool isPause, Task * &head) {
  30.     Task *new_task = new Task;
  31.     Task *temp;
  32.  
  33.     new_task->start_time = start_time;
  34.     new_task->time_length = time_length;
  35.     new_task->real_number = real_number;
  36.     new_task->isPause = isPause;
  37.     new_task->next = NULL;
  38.  
  39.     temp = head;
  40.     if(temp) {
  41.         while(temp->next) temp = temp->next;
  42.         temp->next = new_task;
  43.     } else head = new_task;
  44. }
  45.  
  46. //------------SOLUTION-STRUCTURE---------------
  47. struct Solution {
  48.     Task *machine_1_sequence;
  49.     Task *machine_2_sequence;
  50.     Solution *next;
  51.  
  52.     Solution();
  53.     void add(Solution * &head);
  54. };
  55.  
  56. Solution::Solution() {}
  57.  
  58. void Solution::add(Solution * &head) {
  59.     Solution *new_solution = new Solution;
  60.     Solution *temp;
  61.  
  62.     new_solution->next = NULL;
  63.  
  64.     temp = head;
  65.     if(temp) {
  66.         while(temp->next) temp = temp->next;
  67.         temp->next = new_solution;
  68.     } else head = new_solution;
  69. }
  70.  
  71. //-----------POPULATION-STRUCTURE--------------
  72. struct Population {
  73.     Solution *solution;
  74.  
  75.     Population();
  76. };
  77.  
  78. Population::Population() {
  79.     solution = new Solution;
  80. }
  81.  
  82.  
  83. //---------GLOBAL-VARIABLES----------------
  84. Task *machine1_operations;
  85. Task *machine2_operations;
  86.  
  87. Task *machine1_pauses;
  88. Task *machine2_pauses;
  89.  
  90.  
  91. void mutate(Task &task) {
  92.  
  93. }
  94.  
  95. void crossover(Task &task1, Task &task2) {
  96.  
  97. }
  98.  
  99. void selection(Solution &sol) {
  100.  
  101. }
  102.  
  103. void generate_population(int numberOfTasks, int sizeOfPopulation, Population * &population) {
  104.     for(int i = 0 ; i < sizeOfPopulation ; i++) {
  105.  
  106.         population->solution->add(population->solution);
  107.  
  108.         int taskOrder[numberOfTasks], usedTasks[numberOfTasks];
  109.         for(int j = 0 ; j < numberOfTasks ; j++) usedTasks[j] = 0;
  110.  
  111.         for(int j = 0 ; j < numberOfTasks ; j++) {
  112.             while(true){
  113.                 int taskNumber =(int) (rand()% 100);
  114.                 if(usedTasks[taskNumber] == 0) {
  115.                     usedTasks[taskNumber] = 1;
  116.                     taskOrder[j] = taskNumber;
  117.                     break;
  118.                 }
  119.             }
  120.         }
  121.  
  122.         for(int j = 0 ; j < numberOfTasks ; j++) {
  123.             int taskNumber = taskOrder[j];
  124.             int task_time;
  125.             Task *temp, *sequence;
  126.  
  127.             temp = machine1_operations;
  128.             sequence = machine1_operations;
  129.  
  130.             for(int k = 0; k < taskNumber; k++) {
  131.                 temp->next;
  132.             }
  133.  
  134.             while(sequence) {
  135.                 task_time = sequence->start_time + sequence->time_length;
  136.                 sequence->next;
  137.             }
  138.  
  139.             population->solution->machine_1_sequence->add(task_time, temp->time_length, temp->real_number, false, population->solution->machine_1_sequence);
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145. int load_instance(char fileName[20]) {
  146.     char buf[100];
  147.     int numberOfTasks;
  148.     fstream plik;
  149.  
  150.     plik.open(fileName, ios::in);
  151.  
  152.     if(plik.good()) {
  153.         plik.getline(buf, 6);
  154.         numberOfTasks = atoi(buf);
  155.  
  156.         for(int i = 0 ; i < numberOfTasks ; i++) {
  157.             int machine1_task, machine2_task;
  158.  
  159.             plik.getline(buf, 10, ';');
  160.             machine1_task = atoi(buf);
  161.             plik.getline(buf, 10);
  162.             machine2_task = atoi(buf);
  163.  
  164.             machine1_operations->add(0, machine1_task, i, false, machine1_operations);
  165.             machine2_operations->add(0, machine2_task, i, false, machine2_operations);
  166.         }
  167.  
  168.         while(!plik.eof()) {
  169.             int pauseNumber, machineNumber, timeForPause, whenPauseStart;
  170.  
  171.             plik.getline(buf, 10, ';');
  172.             pauseNumber = atoi(buf);
  173.             plik.getline(buf, 10, ';');
  174.             machineNumber = atoi(buf);
  175.             plik.getline(buf, 10, ';');
  176.             timeForPause = atoi(buf);
  177.             plik.getline(buf, 10);
  178.             whenPauseStart = atoi(buf);
  179.  
  180.             if(timeForPause == 0 && machineNumber == 0 && whenPauseStart == 0) break;
  181.  
  182.             if(machineNumber == 0) machine1_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine1_pauses);
  183.             if(machineNumber == 1) machine2_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine2_pauses);
  184.         }
  185.  
  186.     } else cout << "Ups...Something went wrong.";
  187.  
  188.     plik.close();
  189.  
  190.     return numberOfTasks;
  191. }
  192.  
  193. void save_results(){
  194.  
  195. }
  196.  
  197. int main() {
  198.     int numberOfFiles = 1;
  199.     char fileName[20];
  200.  
  201.     srand(time(NULL));
  202.  
  203.     for(int i = 1; i <= numberOfFiles; i++) {
  204.         Population *population = new Population;
  205.  
  206.         itoa(i * 100, fileName, 10);
  207.         strncat(fileName, "RANDOM.txt", 10);
  208.  
  209.         generate_population(load_instance(fileName), 50, population);
  210.  
  211.         /*
  212.         while(jakis_czas_pewnie_20_minut) {
  213.             // DLA KAZDEGO ROZWIAZANIA Z NASZEJ POPULACJI
  214.                 //Losowanie czy ma byc mutacja - prawdopodobienstwo 30%
  215.                 mutation();
  216.  
  217.                 //Losowanie czy ma byc krzyzowanie - prawdopodobienstwo 60%
  218.                 crossover();
  219.             // KONIEC DLA WSZYSTKICH ROZWIAZAN
  220.  
  221.             selection();
  222.         }
  223.         */
  224.  
  225.         save_results();
  226.  
  227.     }
  228.  
  229.     return 0;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement