-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cpp
More file actions
185 lines (151 loc) · 4.04 KB
/
node.cpp
File metadata and controls
185 lines (151 loc) · 4.04 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
extern "C"
{
#include "vrp_types.h"
}
#include "node.h"
#include "vehicle_manager.h"
#include "simulator.h"
Node::~Node(void)
{
delete[] child_;
delete[] tabu_;
}
void Node::addTabu(int customer)
{
tabu_[customer] = true;
}
void Node::setChildAndRemoveTabu(int child_customer)
{
child_[child_size_++].customer_ = child_customer;
tabu_[child_customer] = false; /* tabu_リストから外す */
}
void Node::expand(const BaseVrp& vrp, const VehicleManager& vm)
{
const int vertnum = vrp.CustomerSize()+1;
child_ = new Node[vertnum];
tabu_ = new bool[vertnum];
for (int i=0; i < vertnum; i++)
addTabu(i);
if (vm.nextVehicleRemain(vrp))
setChildAndRemoveTabu(VehicleManager::kChange);
for (int i=1; i < vertnum; i++)
if (!vm.isVisit(i) && vm.canVisitCustomer(vrp, i))
setChildAndRemoveTabu(i);
}
double Node::computeUcb(int parent_count)
{
double ucb = 1e6 + (rand() % (int)1e6);
if (count_ != 0)
ucb = - value_ / count_ + 1.0 * sqrt(log((double)parent_count+1)) / count_;
return ucb;
}
Node *Node::selectMaxUcbChild(void)
{
double max_ucb = - 1e6;
Node *selected = NULL;
for (int i=0; i < child_size_; i++)
{
Node *child = &child_[i];
bool child_is_tabu = tabu_[child->customer()];
if (child_is_tabu) continue;
double ucb = child->computeUcb(count_);
if (ucb > max_ucb)
{
max_ucb = ucb;
selected = child;
}
}
return selected;
}
bool Node::isLeaf(void) const
{
return (child_size_ == 0);
}
bool Node::isTabu(const BaseVrp& vrp) const
{
const int vertnum = vrp.CustomerSize()+1;
for (int i=0; i < vertnum; i++)
if (!tabu_[i])
return false;
return true;
}
void Node::update(int value)
{
count_++;
value_ += value;
}
void Node::build(const BaseVrp& vrp, const VehicleManager& vm, int count)
{
/* 引数として渡されるvmは変更しない
* そのため変更させるための変数を作成 */
VehicleManager vm_copy = vm;
Node *visited[300];
int visited_size = 0;
Node *parent = NULL;
Node *node = this;
/* 木の根は訪問済み */
visited[visited_size++] = this;
/* SELECTION */
while (!node->isLeaf())
{
if (node->isTabu(vrp))
{
parent->addTabu(node->customer());
return ; /* 探索を破棄 */
}
parent = node;
node = node->selectMaxUcbChild();
visited[visited_size++] = node;
vm_copy.move(vrp, node->customer());
}
VehicleManager parent_vm = vm_copy;
/* nodeが全探索木の葉でなければexpandする*/
/* if (!vehicleManagerIsTerminal(vm_copy, vrp)) */
if (vm_copy.isMovable(vrp))
{
/* EXPANSION */
node->expand(vrp, vm_copy);
parent = node;
node = node->selectMaxUcbChild();
vm_copy.move(vrp, node->customer());
}
/* SIMULATION */
int cost = 1e6;
Simulator simulator;
while ((cost = simulator.sequentialRandomSimulation(vrp, vm_copy, count)) == 1e6)
{
parent->addTabu(node->customer());
vm_copy = parent_vm; /* VehicleManagerを直前の状態に移す */
if (parent->isTabu(vrp))
return ; /* 探索を破棄 */
node = parent->selectMaxUcbChild();
vm_copy.move(vrp, node->customer());
}
visited[visited_size++] = node;
/* BACKPROPAGATION */
for (int i=0; i < visited_size; i++)
visited[i]->update(cost);
}
Node *Node::selectMostVisitedChild(void) const
{
Node *selected = NULL;
int maxCount = -1;
for (int i=0; i < child_size_; i++)
{
int count = child_[i].count();
if (count > maxCount)
{
maxCount = count;
selected = &child_[i];
}
}
return selected;
}
int Node::selectNextMove(void) const
{
Node *selected = selectMostVisitedChild();
return selected->customer();
}