-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimulatedAnneallingSearch.cs
169 lines (154 loc) · 3.98 KB
/
SimulatedAnneallingSearch.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SimulatedAnneallingSearch.cs" company="pc">
// 2007-2009
// </copyright>
// <summary>
// Defines the SimulatedAnneallingSearch type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Cream
{
using System;
using System.Collections;
/// <summary>
/// SA Solving Method
/// </summary>
public class SimulatedAnneallingSearch : LocalSearch
{
/// <summary>
/// The gamma.
/// </summary>
private double _gamma = 0.999;
/// <summary>
/// The temperature.
/// </summary>
private double _temperature = 100.0;
/// <summary>
/// Initializes a new instance of the <see cref="SimulatedAnneallingSearch"/> class.
/// </summary>
/// <param name="network">
/// The network.
/// </param>
public SimulatedAnneallingSearch(Network network) : this(network, Default, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimulatedAnneallingSearch"/> class.
/// </summary>
/// <param name="network">
/// The network.
/// </param>
/// <param name="option">
/// The option.
/// </param>
public SimulatedAnneallingSearch(Network network, int option) : this(network, option, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimulatedAnneallingSearch"/> class.
/// </summary>
/// <param name="network">
/// The network.
/// </param>
/// <param name="name">
/// The solver name.
/// </param>
public SimulatedAnneallingSearch(Network network, string name) : this(network, Default, name)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimulatedAnneallingSearch"/> class.
/// </summary>
/// <param name="network">
/// The network.
/// </param>
/// <param name="option">
/// The option.
/// </param>
/// <param name="name">
/// The solver name.
/// </param>
public SimulatedAnneallingSearch(Network network, int option, string name) : base(network, option, name)
{
ExchangeRate = 0.05;
}
/// <summary>
/// Gets or sets Temperature.
/// </summary>
public virtual double Temperature
{
get
{
lock (this)
{
return _temperature;
}
}
set
{
lock (this)
{
_temperature = value;
}
}
}
/// <summary>
/// The next search.
/// </summary>
protected internal override void NextSearch()
{
if (totalTimeout > 0)
{
long elapsedTime = Math.Max(1, ((DateTime.Now.Ticks - 621355968000000000) / 10000) - startTime);
double iterationRate = (double) iteration/elapsedTime;
var expectedIteration = (int) (iterationRate*(totalTimeout - elapsedTime));
if (expectedIteration > 0)
{
_gamma = Math.Exp(Math.Log(1.0/_temperature)/expectedIteration);
_gamma = Math.Min(0.9999, _gamma);
}
}
_temperature *= _gamma;
solution = GetCandidate();
int objectiveIntValue = solution.ObjectiveIntValue;
Code code = solution.Code;
IList operations = code.Operations();
while (operations.Count > 0)
{
var i = (int) (operations.Count*SupportClass.Random.NextDouble());
object tempObject = operations[i];
operations.RemoveAt(i);
var op = (Operation) tempObject;
code.To = network;
op.ApplyTo(network);
Solution sol = solver.FindBest(iterTimeout);
if (sol == null)
continue;
double delta = sol.ObjectiveIntValue - objectiveIntValue;
if (IsOption(Maximize))
{
delta = -delta;
}
if (delta < 0)
{
solution = sol;
return;
}
double p = Math.Exp((-delta)/_temperature);
if (p < SupportClass.Random.NextDouble())
continue;
solution = sol;
return;
}
solution = null;
}
/// <summary>
/// The start search.
/// </summary>
protected internal override void StartSearch()
{
base.StartSearch();
_temperature = solution==null?0:solution.ObjectiveIntValue/10.0;
}
}
}