-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_def.py
322 lines (244 loc) · 11.8 KB
/
problem_def.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from abc import ABC, abstractmethod
from typing import Tuple
import torch
class State:
def __init__(self, batch_size: int, problem_size: int or Tuple[int], pomo_size: int, node_features: torch.Tensor or None, adj_matrix: torch.Tensor or None,
edge_features: torch.Tensor or None, solutions: torch.Tensor or None, mask: torch.Tensor or None, is_complete: bool = False,
seed: int or None = None, device: str or torch.device = 'cpu', **kwargs):
"""
The state of the environment. It contains all the information needed to represent the problem instance and the
current solution.
"""
self.data = kwargs # User-defined attributes stored in a dictionary
self.batch_size = batch_size # Number of instances in the batch
self.problem_size = problem_size # Size of the problem (number of nodes)
self.pomo_size = pomo_size # Number of parallel initializations (Policy Optimization with Multiple Optima)
self.adj_matrix = adj_matrix # Adjacency matrix
self.node_features = node_features # Node features
self.edge_features = edge_features # Edge features
self.solutions = solutions # Current solutions
self.mask = mask # Mask to avoid selecting certain actions
self.is_complete = is_complete # Is the solution complete?
self.memory_info = None # Memory information
self.last_action = None # Last action taken
self.seed = seed # Seed for reproducibility
self.device = device # Device
class Problem(ABC):
def __init__(self, device: str or torch.device):
"""
Abstract class for the problem definition.
:param device: str or torch.device: Device to use for computations.
"""
self.device = torch.device(device)
# Auxiliary state to get the dimensions of the node and edge features of the user-defined problem definition
aux_state = State(batch_size=1, problem_size=1, pomo_size=1, node_features=None, adj_matrix=None, edge_features=None,
solutions=None, mask=None, is_complete=False, device=self.device)
aux_state = self._init_instances(aux_state)
aux_state = self._init_solutions(aux_state)
aux_state = self._init_features(aux_state)
self.node_in_dim = 0 if aux_state.node_features is None else aux_state.node_features.size(-1)
self.edge_in_dim = 0 if aux_state.edge_features is None else aux_state.edge_features.size(-1)
@abstractmethod
def generate_state(self, batch_size: int, problem_size: int, pomo_size: int, seed: int or None = None) -> State:
"""
Generate a batch of states for the problem. Follows the following steps:
1 - Generate new graphs
2 - Initialize the solution
3 - Compute the initial objective value
4 - Update state with features
5 - Initialize mask
:param batch_size: int: Number of instances in the batch.
:param problem_size: int or list: Size of the problem (number of nodes).
:param pomo_size: int: Number of parallel initializations (Policy Optimization with Multiple Optima).
:param seed: int or None: Seed for reproducibility.
:return: State: A state class with features representing the problem instance.
"""
raise NotImplementedError
@abstractmethod
def update_state(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> (State, torch.Tensor):
"""
Update the state with the given action. Follows the following steps:
1 - Apply the action to the environment: update the solution
2 or 3 - Compute the objective value
2 or 3 - Check completeness
4 - Update mask
5 - Update features with new solution
:param state: State: The current state of the environment.
:param action: Tuple[torch.Tensor, torch.Tensor]: The action to apply to the environment. (selected node/edge, selected class)
:return: (State, torch.Tensor): A state class with updated features and the objective value.
"""
raise NotImplementedError
@abstractmethod
def _init_instances(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_solutions(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_features(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_mask(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _obj_function(self, state: State) -> float:
raise NotImplementedError
@abstractmethod
def _update_features(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_solutions(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_mask(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _check_completeness(self, state: State) -> State:
raise NotImplementedError
class ConstructiveProblem(Problem):
def __init__(self, device: str or torch.device):
"""
:param device: str or torch.device: Device to use for computations.
"""
super().__init__(device)
self.device = torch.device(device)
def generate_state(self, problem_size: int, batch_size: int, pomo_size: int, seed: int or None = None) -> (State, torch.Tensor):
"""
Generate a batch of states for the problem.
Returns:
- A state class with features representing the problem instance: node_features, edge_features, solution, mask...
"""
# Initialize the state
state = State(batch_size=batch_size, problem_size=problem_size, pomo_size=pomo_size, node_features=None,
adj_matrix=None, edge_features=None, solutions=None, mask=None, is_complete=False,
device=self.device, seed=seed)
# 1 - Generate new graphs
state = self._init_instances(state)
# 2 - Initialize the solution
state = self._init_solutions(state)
# 3- Compute the initial objective value: in constructive frameworks, the objective value is typically computed at the end
obj_value = torch.zeros(1, device=state.device)
# 4 - Update state with features
state = self._init_features(state)
# 5 - Initialize mask
state = self._init_mask(state)
return state, obj_value
def update_state(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> (State, torch.Tensor):
"""
Update the state with the given action.
Returns:
- A state class with updated features.
"""
# 1 - Apply the action to the environment: update the solution
state = self._update_solutions(state, action)
# 2 - Check completeness
state = self._check_completeness(state)
# 3 - Compute the objective value: only if the solution is complete
if state.is_complete:
obj_value = self._obj_function(state)
# reshape to (batch_size*pomo_size)
obj_value = obj_value.view(-1)
else:
obj_value = torch.empty(0, device=state.device)
# 4 - Update mask
state = self._update_mask(state, action)
# 5 - Update features with new solutions
state = self._update_features(state, action)
return state, obj_value
@abstractmethod
def _init_instances(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_solutions(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_features(self, state: State) -> State:
raise NotImplementedError
def _init_mask(self, state: State) -> State:
state.mask = torch.zeros((state.batch_size, state.problem_size, 1), device=state.device)
return state
@abstractmethod
def _obj_function(self, state: State) -> torch.Tensor:
raise NotImplementedError
@abstractmethod
def _update_features(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_solutions(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_mask(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _check_completeness(self, state: State) -> State:
raise NotImplementedError
class ImprovementProblem(Problem):
def __init__(self, device: str or torch.device):
"""
:param device: str or torch.device: Device to use for computations.
"""
super().__init__(device)
self.device = torch.device(device)
def generate_state(self, problem_size: int, batch_size: int, pomo_size: int, seed: int = None):
"""
Generate a batch of states for the problem.
Returns:
- A state class with features representing the problem instance: node_features, edge_features, solution, mask...
"""
# Initialize the state
state = State(batch_size=batch_size, problem_size=problem_size, pomo_size=pomo_size,
node_features=None, adj_matrix=None, edge_features=None,
solutions=None, mask=None, is_complete=False, device=self.device, seed=seed)
# 1 - Generate new graphs
state = self._init_instances(state)
# 2 - Initialize the solution
state = self._init_solutions(state)
# 3- Compute the initial objective value
obj_value = self._obj_function(state)
# 4 - Update state with features
state = self._init_features(state)
# 5 - Initialize mask
state = self._init_mask(state)
return state, obj_value
def update_state(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> (State, torch.Tensor):
"""
Update the state with the given action.
Returns:
- A state class with updated features.
"""
# 1 - Apply the action to the environment: update the solution
state = self._update_solutions(state, action)
# 2 - Compute the objective value: a solution is always complete in improvement frameworks
obj_value = self._obj_function(state)
# 3 - Update mask
state = self._update_mask(state, action)
# 5 - Update features with new solutions
state = self._update_features(state, action)
return state, obj_value
@abstractmethod
def _init_instances(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_solutions(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_features(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _init_mask(self, state: State) -> State:
raise NotImplementedError
@abstractmethod
def _obj_function(self, state: State) -> torch.Tensor:
raise NotImplementedError
@abstractmethod
def _update_features(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_solutions(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _update_mask(self, state: State, action: Tuple[torch.Tensor, torch.Tensor]) -> State:
raise NotImplementedError
@abstractmethod
def _check_completeness(self, state: State) -> State:
raise NotImplementedError