-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
66 lines (57 loc) · 1.84 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include "input.hpp"
#include "checker.hpp"
inline void Output( const std::string &outputFile, const std::vector< std::vector< int > > sol )
{
std::ofstream output(outputFile);
for( size_t i=0;i<sol.size();i++ )
{
output<<sol[i].size()<<" ";
for( size_t j=0;j<sol[i].size();j++ )
output<<sol[i][j]<<" ";
output<<std::endl;;
}
output.close();
}
int main()
{
std::string inputFile = "a_example.in";
std::cerr<<"Enter input file: ";
std::cin>>inputFile;
std::string outputFile = "solution.out";
std::cerr<<"Enter output file: ";
std::cin>>outputFile;
std::ifstream input( inputFile );
world.Input( input );
/* Create an empty solution */
std::vector< std::vector< int > > sol(world.fleetSize);
/* Copy(in order to preserve the original order) and sort all the rides, based on the latest starting time */
std::vector< Ride > orderedRides = world.allRides;
std::sort( orderedRides.begin(), orderedRides.end(), []( const Ride &a, const Ride &b ){ return a.latestStart< b.latestStart; } );
/* Greedy-ly assign the rides */
for( size_t i=0;i<world.allRides.size();++i )
sol[ i % world.fleetSize ].push_back( orderedRides[i].id );
long long max = checker.SimulateFleet( sol );
std::cerr<<"Start score: "<<max<<"\n";
Output( outputFile, sol );
while( true )
{
/* Apply a random mutation and check if it improved the score */
std::vector< std::vector< int > > newSol = sol;
size_t fleet = std::rand()%world.fleetSize;
size_t pos1 = std::rand()%newSol[fleet].size();
size_t pos2 = std::rand()%newSol[fleet].size();
std::swap( newSol[fleet][pos1], newSol[fleet][pos2] );
long long score = checker.SimulateFleet( newSol );
if( score > max )
{
max = score;
sol = newSol;
std::cerr<<"New max score: "<<max<<"\n";
Output( outputFile, sol );
}
}
}