-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolutionBase.cs
118 lines (103 loc) · 3.23 KB
/
SolutionBase.cs
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
using Deadline;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
public class SolutionBase
{
/// <summary>
/// In miliseconds.
/// </summary>
protected double timeRemaining;
public static Random random = new Random();
Stopwatch timer = new Stopwatch();
IClient client;
protected TCPClient tcpClient { get { return client as TCPClient; } }
protected IOClient ioClient { get { return client as IOClient; } }
protected World World; // global constants of a container for a number of state (if many games going concurrently)
protected GameState state;
protected Result best;
public const long INF = long.MaxValue / 10;
public SolutionBase(IClient client, double time = 0)
{
this.client = client;
this.timeRemaining = time;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
}
public void Restart()
{
World = null;
}
public virtual void GetData()
{
this.state = new GameState();
client.LearnState(state);
}
protected virtual bool TakeBestAction()
{
return client.TakeAction(best);
}
public virtual bool Act()
{
// solve problem described in this.state
// solution present as Result object
// then send results (PrintBest)
//
// or talk in a more complicated way to tcp server
throw new NotImplementedException();
}
public void RunIterations(Action<RunInfo> go, double timeAvailable, int iterations)
{
Stopwatch timer = new Stopwatch();
double timeLeft = timeAvailable;
for (int iteration = 0; iteration < iterations; iteration++)
{
timer.Start();
go(new RunInfo(iteration, timeLeft / (iterations - iteration)));
timer.Stop();
timeLeft = timeAvailable - timer.ElapsedMilliseconds;
double perIteration = (double)timer.ElapsedMilliseconds / (iteration + 1);
if (1.1 * perIteration >= timeLeft)
break;
}
}
public void RunForTime(Func<RunInfo,bool> go, double timeAvailable)
{
Stopwatch timer = new Stopwatch();
int iterations = 0;
double perIteration = 0;
double timeLeft = timeAvailable;
while (1.5 * perIteration < timeLeft)
{
timer.Start();
var stop = go(new RunInfo(iterations, timeLeft)); // maximal time to take (should take very small part of it)
timer.Stop();
iterations++;
timeLeft = timeAvailable - timer.ElapsedMilliseconds;
perIteration = (double)timer.ElapsedMilliseconds / iterations;
if (stop)
break;
}
}
public void MeasureTime(string name, Action<int> go)
{
timer.Reset();
timer.Start();
go(0);
timer.Stop();
Console.Error.WriteLine(name + ": took {0} ms", timer.ElapsedMilliseconds);
}
}
public struct RunInfo
{
public readonly int Seed;
public readonly double TimeLeft;
public RunInfo(int s, double t)
{
Seed = s;
TimeLeft = t;
}
}