Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- #include <fstream>
- #include <cstdlib>
- #include <time.h>
- #include <string.h>
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- //------------TASK-STRUCTURE-----------
- struct Task{
- int start_time;
- int time_length;
- int real_number;
- bool isPause;
- Task *next;
- Task();
- void add(int start_time, int time_length, int real_number, bool isPause, Task * &head);
- };
- Task::Task() {}
- void Task::add(int start_time, int time_length, int real_number, bool isPause, Task * &head) {
- Task *new_task = new Task;
- Task *temp;
- new_task->start_time = start_time;
- new_task->time_length = time_length;
- new_task->real_number = real_number;
- new_task->isPause = isPause;
- new_task->next = NULL;
- temp = head;
- if(temp) {
- while(temp->next) temp = temp->next;
- temp->next = new_task;
- } else head = new_task;
- }
- //------------SOLUTION-STRUCTURE---------------
- struct Solution {
- Task *machine_1_sequence;
- Task *machine_2_sequence;
- Solution *next;
- Solution();
- void add(Solution * &head);
- };
- Solution::Solution() {}
- void Solution::add(Solution * &head) {
- Solution *new_solution = new Solution;
- Solution *temp;
- new_solution->next = NULL;
- temp = head;
- if(temp) {
- while(temp->next) temp = temp->next;
- temp->next = new_solution;
- } else head = new_solution;
- }
- //-----------POPULATION-STRUCTURE--------------
- struct Population {
- Solution *solution;
- Population();
- };
- Population::Population() {
- solution = new Solution;
- }
- //---------GLOBAL-VARIABLES----------------
- Task *machine1_operations;
- Task *machine2_operations;
- Task *machine1_pauses;
- Task *machine2_pauses;
- void mutate(Task &task) {
- }
- void crossover(Task &task1, Task &task2) {
- }
- void selection(Solution &sol) {
- }
- void generate_population(int numberOfTasks, int sizeOfPopulation, Population * &population) {
- for(int i = 0 ; i < sizeOfPopulation ; i++) {
- population->solution->add(population->solution);
- int taskOrder[numberOfTasks], usedTasks[numberOfTasks];
- for(int j = 0 ; j < numberOfTasks ; j++) usedTasks[j] = 0;
- for(int j = 0 ; j < numberOfTasks ; j++) {
- while(true){
- int taskNumber =(int) (rand()% 100);
- if(usedTasks[taskNumber] == 0) {
- usedTasks[taskNumber] = 1;
- taskOrder[j] = taskNumber;
- break;
- }
- }
- }
- for(int j = 0 ; j < numberOfTasks ; j++) {
- int taskNumber = taskOrder[j];
- int task_time;
- Task *temp, *sequence;
- temp = machine1_operations;
- sequence = machine1_operations;
- for(int k = 0; k < taskNumber; k++) {
- temp->next;
- }
- while(sequence) {
- task_time = sequence->start_time + sequence->time_length;
- sequence->next;
- }
- population->solution->machine_1_sequence->add(task_time, temp->time_length, temp->real_number, false, population->solution->machine_1_sequence);
- }
- }
- }
- int load_instance(char fileName[20]) {
- char buf[100];
- int numberOfTasks;
- fstream plik;
- plik.open(fileName, ios::in);
- if(plik.good()) {
- plik.getline(buf, 6);
- numberOfTasks = atoi(buf);
- for(int i = 0 ; i < numberOfTasks ; i++) {
- int machine1_task, machine2_task;
- plik.getline(buf, 10, ';');
- machine1_task = atoi(buf);
- plik.getline(buf, 10);
- machine2_task = atoi(buf);
- machine1_operations->add(0, machine1_task, i, false, machine1_operations);
- machine2_operations->add(0, machine2_task, i, false, machine2_operations);
- }
- while(!plik.eof()) {
- int pauseNumber, machineNumber, timeForPause, whenPauseStart;
- plik.getline(buf, 10, ';');
- pauseNumber = atoi(buf);
- plik.getline(buf, 10, ';');
- machineNumber = atoi(buf);
- plik.getline(buf, 10, ';');
- timeForPause = atoi(buf);
- plik.getline(buf, 10);
- whenPauseStart = atoi(buf);
- if(timeForPause == 0 && machineNumber == 0 && whenPauseStart == 0) break;
- if(machineNumber == 0) machine1_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine1_pauses);
- if(machineNumber == 1) machine2_pauses->add(whenPauseStart, timeForPause, pauseNumber, true, machine2_pauses);
- }
- } else cout << "Ups...Something went wrong.";
- plik.close();
- return numberOfTasks;
- }
- void save_results(){
- }
- int main() {
- int numberOfFiles = 1;
- char fileName[20];
- srand(time(NULL));
- for(int i = 1; i <= numberOfFiles; i++) {
- Population *population = new Population;
- itoa(i * 100, fileName, 10);
- strncat(fileName, "RANDOM.txt", 10);
- generate_population(load_instance(fileName), 50, population);
- /*
- while(jakis_czas_pewnie_20_minut) {
- // DLA KAZDEGO ROZWIAZANIA Z NASZEJ POPULACJI
- //Losowanie czy ma byc mutacja - prawdopodobienstwo 30%
- mutation();
- //Losowanie czy ma byc krzyzowanie - prawdopodobienstwo 60%
- crossover();
- // KONIEC DLA WSZYSTKICH ROZWIAZAN
- selection();
- }
- */
- save_results();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement