-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (101 loc) · 4.25 KB
/
main.cpp
File metadata and controls
131 lines (101 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <chrono>
#include <functional>
#include "job.h"
#include "processor.h"
#include "read_config.h"
#include "greedy.h"
#include "write_config.h"
#include "input_change.h"
// looking for the best solution in time of ~5 minutes
void execute(std::vector<Job> &jobs, const std::vector<Processor> processors, std::vector<Job> &best_solution, std::chrono::steady_clock::time_point time_zero, int num_of_swaps){
float deadline = 4.5f*60.0f;
float max_time = -1;
int i = 1;
std::vector<Job> jobs_input = jobs;
// best_solution = greedy(jobs, processors, 1);
// wypisywanie jakości pierwszego znalezionego rozwiązania greedy
// sum_times(best_solution);
std::vector<Job> solution_greedy;
srand(time(NULL));
int j = 0;
int same_results_counter = 0;
int best_sum_of_times_current = 0;
while (true && same_results_counter < 20000){
auto start_greedy = std::chrono::steady_clock::now();
std::cout << j << std::endl;
solution_greedy = greedy(jobs, processors, 1);
if (best_solution.empty()){
best_solution = solution_greedy;
} else if (compare_quality(solution_greedy, best_solution, same_results_counter, best_sum_of_times_current)){
best_solution = solution_greedy;
}
auto end_greedy = std::chrono::steady_clock::now();
std::chrono::duration<float> elapsed_seconds_greedy = end_greedy - start_greedy;
std::cout << "Greedy time = " << elapsed_seconds_greedy.count() << std::endl;
// zmutuj listę jobs - szukanie w sąsiedztwie w następnej iteracji greedy
auto start_swaps = std::chrono::steady_clock::now();
jobs = change_input_vector(jobs_input, num_of_swaps);
auto end_swaps = std::chrono::steady_clock::now();
std::chrono::duration<float> elapsed_seconds_swapping = end_swaps - start_swaps;
std::cout << "Swapping time = " << elapsed_seconds_swapping.count() << std::endl;
std::cout << "Time left = " << deadline - static_cast<std::chrono::duration<float>>(end_swaps - time_zero).count() << std::endl;
if(elapsed_seconds_greedy.count() + elapsed_seconds_swapping.count() > max_time){
max_time = elapsed_seconds_greedy.count() + elapsed_seconds_swapping.count();
}
if(deadline - static_cast<std::chrono::duration<float>>(end_swaps - time_zero).count() < max_time){
sum_times(best_solution);
return;
}
j++;
}
// wypisywanie jakości najlepszego rozwiązania
sum_times(best_solution);
return;
}
int main(int argc, char **argv)
{
if (argc == 1 || argc > 4)
{
std::cerr << "Usage: " << std::string(argv[0]) << " <config_file> [jobs_limit]" << std::endl;
return -1;
}
auto start = std::chrono::steady_clock::now();
std::vector<Job> jobs;
if (argc == 3)
{
jobs = read_jobs(argv[1]);
}
else
{ // argc == 4
jobs = read_jobs(argv[1], std::stoi(argv[3]));
}
//reading processors
std::vector<Processor> processors = read_processors(argv[1]);
auto end_read = std::chrono::steady_clock::now();
//sorting input data
std::sort(jobs.begin(), jobs.end(), [](const Job& j1, const Job& j2){
if (j1.ready_time() == j2.ready_time()) {
return j1.processors_needed() < j2.processors_needed();
}
return j1.ready_time() < j2.ready_time();
});
auto end = std::chrono::steady_clock::now();
//measuring time for reading data and sorting data
//std::chrono::duration<float> time_left_quick = end_quick - end_read;
std::chrono::duration<float> time_left_sort = end - end_read;
std::chrono::duration<float> time_left_read = end_read - start;
std::cout << "Read time: " << time_left_read.count() << std::endl;
std::cout << "Sort time: " << time_left_sort.count() << std::endl;
//looking for the best solution
std::vector<Job> solution;
int size_of_jobs = jobs.size();
int num_of_swaps = static_cast<int>(size_of_jobs*0.02);
execute(jobs, processors, solution, start, num_of_swaps);
//writing data to a file
write_jobs(argv[2], solution);
return 0;
}