/
MoveSelectorConfig.java
400 lines (349 loc) · 19.6 KB
/
MoveSelectorConfig.java
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2012 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.optaplanner.core.config.heuristic.selector.move;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamInclude;
import org.apache.commons.collections.CollectionUtils;
import org.optaplanner.core.config.heuristic.policy.HeuristicConfigPolicy;
import org.optaplanner.core.config.heuristic.selector.SelectorConfig;
import org.optaplanner.core.config.heuristic.selector.common.SelectionOrder;
import org.optaplanner.core.config.heuristic.selector.move.composite.CartesianProductMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.composite.UnionMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.factory.MoveIteratorFactoryConfig;
import org.optaplanner.core.config.heuristic.selector.move.factory.MoveListFactoryConfig;
import org.optaplanner.core.config.heuristic.selector.move.generic.ChangeMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.generic.PillarSwapMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.generic.SwapMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainChangeMoveSelectorConfig;
import org.optaplanner.core.config.heuristic.selector.move.generic.chained.SubChainSwapMoveSelectorConfig;
import org.optaplanner.core.config.util.ConfigUtils;
import org.optaplanner.core.impl.heuristic.selector.common.SelectionCacheType;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.ComparatorSelectionSorter;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionFilter;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionProbabilityWeightFactory;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionSorter;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionSorterOrder;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.SelectionSorterWeightFactory;
import org.optaplanner.core.impl.heuristic.selector.common.decorator.WeightFactorySelectionSorter;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.CachingMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.FilteringMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.ProbabilityMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.SelectedCountLimitMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.ShufflingMoveSelector;
import org.optaplanner.core.impl.heuristic.selector.move.decorator.SortingMoveSelector;
/**
* General superclass for {@link ChangeMoveSelectorConfig}, etc.
*/
@XStreamInclude({
UnionMoveSelectorConfig.class, CartesianProductMoveSelectorConfig.class,
ChangeMoveSelectorConfig.class, SwapMoveSelectorConfig.class, PillarSwapMoveSelectorConfig.class,
SubChainChangeMoveSelectorConfig.class, SubChainSwapMoveSelectorConfig.class,
MoveListFactoryConfig.class, MoveIteratorFactoryConfig.class
})
public abstract class MoveSelectorConfig extends SelectorConfig {
protected SelectionCacheType cacheType = null;
protected SelectionOrder selectionOrder = null;
@XStreamImplicit(itemFieldName = "filterClass")
protected List<Class<? extends SelectionFilter>> filterClassList = null;
protected Class<? extends Comparator> sorterComparatorClass = null;
protected Class<? extends SelectionSorterWeightFactory> sorterWeightFactoryClass = null;
protected SelectionSorterOrder sorterOrder = null;
protected Class<? extends SelectionSorter> sorterClass = null;
protected Class<? extends SelectionProbabilityWeightFactory> probabilityWeightFactoryClass = null;
protected Long selectedCountLimit = null;
private Double fixedProbabilityWeight = null;
public SelectionCacheType getCacheType() {
return cacheType;
}
public void setCacheType(SelectionCacheType cacheType) {
this.cacheType = cacheType;
}
public SelectionOrder getSelectionOrder() {
return selectionOrder;
}
public void setSelectionOrder(SelectionOrder selectionOrder) {
this.selectionOrder = selectionOrder;
}
public List<Class<? extends SelectionFilter>> getFilterClassList() {
return filterClassList;
}
public void setFilterClassList(List<Class<? extends SelectionFilter>> filterClassList) {
this.filterClassList = filterClassList;
}
public Class<? extends Comparator> getSorterComparatorClass() {
return sorterComparatorClass;
}
public void setSorterComparatorClass(Class<? extends Comparator> sorterComparatorClass) {
this.sorterComparatorClass = sorterComparatorClass;
}
public Class<? extends SelectionSorterWeightFactory> getSorterWeightFactoryClass() {
return sorterWeightFactoryClass;
}
public void setSorterWeightFactoryClass(Class<? extends SelectionSorterWeightFactory> sorterWeightFactoryClass) {
this.sorterWeightFactoryClass = sorterWeightFactoryClass;
}
public SelectionSorterOrder getSorterOrder() {
return sorterOrder;
}
public void setSorterOrder(SelectionSorterOrder sorterOrder) {
this.sorterOrder = sorterOrder;
}
public Class<? extends SelectionSorter> getSorterClass() {
return sorterClass;
}
public void setSorterClass(Class<? extends SelectionSorter> sorterClass) {
this.sorterClass = sorterClass;
}
public Class<? extends SelectionProbabilityWeightFactory> getProbabilityWeightFactoryClass() {
return probabilityWeightFactoryClass;
}
public void setProbabilityWeightFactoryClass(Class<? extends SelectionProbabilityWeightFactory> probabilityWeightFactoryClass) {
this.probabilityWeightFactoryClass = probabilityWeightFactoryClass;
}
public Long getSelectedCountLimit() {
return selectedCountLimit;
}
public void setSelectedCountLimit(Long selectedCountLimit) {
this.selectedCountLimit = selectedCountLimit;
}
public Double getFixedProbabilityWeight() {
return fixedProbabilityWeight;
}
public void setFixedProbabilityWeight(Double fixedProbabilityWeight) {
this.fixedProbabilityWeight = fixedProbabilityWeight;
}
// ************************************************************************
// Builder methods
// ************************************************************************
/**
*
* @param configPolicy never null
* @param minimumCacheType never null, If caching is used (different from {@link SelectionCacheType#JUST_IN_TIME}),
* then it should be at least this {@link SelectionCacheType} because an ancestor already uses such caching
* and less would be pointless.
* @param inheritedSelectionOrder never null
* @return never null
*/
public MoveSelector buildMoveSelector(HeuristicConfigPolicy configPolicy,
SelectionCacheType minimumCacheType, SelectionOrder inheritedSelectionOrder) {
SelectionCacheType resolvedCacheType = SelectionCacheType.resolve(cacheType, minimumCacheType);
SelectionOrder resolvedSelectionOrder = SelectionOrder.resolve(selectionOrder, inheritedSelectionOrder);
validateCacheTypeVersusSelectionOrder(resolvedCacheType, resolvedSelectionOrder);
validateSorting(resolvedSelectionOrder);
validateProbability(resolvedSelectionOrder);
validateSelectedLimit(minimumCacheType);
MoveSelector moveSelector = buildBaseMoveSelector(configPolicy,
SelectionCacheType.max(minimumCacheType, resolvedCacheType),
determineBaseRandomSelection(resolvedCacheType, resolvedSelectionOrder));
moveSelector = applyFiltering(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applySorting(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applyProbability(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applyShuffling(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applyCaching(resolvedCacheType, resolvedSelectionOrder, moveSelector);
moveSelector = applySelectedLimit(resolvedCacheType, resolvedSelectionOrder, moveSelector);
return moveSelector;
}
protected boolean determineBaseRandomSelection(
SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder) {
switch (resolvedSelectionOrder) {
case ORIGINAL:
return false;
case SORTED:
case SHUFFLED:
case PROBABILISTIC:
// baseValueSelector and lower should be ORIGINAL if they are going to get cached completely
return false;
case RANDOM:
// Predict if caching will occur
return resolvedCacheType.isNotCached() || (isBaseInherentlyCached() && !hasFiltering());
default:
throw new IllegalStateException("The selectionOrder (" + resolvedSelectionOrder
+ ") is not implemented.");
}
}
protected boolean isBaseInherentlyCached() {
return false;
}
/**
*
* @param configPolicy never null
* @param minimumCacheType never null, If caching is used (different from {@link SelectionCacheType#JUST_IN_TIME}),
* then it should be at least this {@link SelectionCacheType} because an ancestor already uses such caching
* and less would be pointless.
* @param randomSelection true is equivalent to {@link SelectionOrder#RANDOM},
* false is equivalent to {@link SelectionOrder#ORIGINAL}
* @return never null
*/
protected abstract MoveSelector buildBaseMoveSelector(
HeuristicConfigPolicy configPolicy,
SelectionCacheType minimumCacheType, boolean randomSelection);
private boolean hasFiltering() {
return !CollectionUtils.isEmpty(filterClassList);
}
private MoveSelector applyFiltering(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (hasFiltering()) {
List<SelectionFilter> filterList = new ArrayList<SelectionFilter>(filterClassList.size());
for (Class<? extends SelectionFilter> filterClass : filterClassList) {
filterList.add(ConfigUtils.newInstance(this, "filterClass", filterClass));
}
moveSelector = new FilteringMoveSelector(moveSelector, filterList);
}
return moveSelector;
}
private void validateSorting(SelectionOrder resolvedSelectionOrder) {
if ((sorterComparatorClass != null || sorterWeightFactoryClass != null
|| sorterOrder != null || sorterClass != null)
&& resolvedSelectionOrder != SelectionOrder.SORTED) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with sorterComparatorClass (" + sorterComparatorClass
+ ") and sorterWeightFactoryClass (" + sorterWeightFactoryClass
+ ") and sorterOrder (" + sorterOrder
+ ") and sorterClass (" + sorterClass
+ ") has a resolvedSelectionOrder (" + resolvedSelectionOrder
+ ") that is not " + SelectionOrder.SORTED + ".");
}
if (sorterComparatorClass != null && sorterWeightFactoryClass != null) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") has both a sorterComparatorClass (" + sorterComparatorClass
+ ") and a sorterWeightFactoryClass (" + sorterWeightFactoryClass + ").");
}
if (sorterComparatorClass != null && sorterClass != null) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") has both a sorterComparatorClass (" + sorterComparatorClass
+ ") and a sorterClass (" + sorterClass + ").");
}
if (sorterWeightFactoryClass != null && sorterClass != null) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") has both a sorterWeightFactoryClass (" + sorterWeightFactoryClass
+ ") and a sorterClass (" + sorterClass + ").");
}
if (sorterClass != null && sorterOrder != null) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with sorterClass (" + sorterClass
+ ") has a non-null sorterOrder (" + sorterOrder + ").");
}
}
private MoveSelector applySorting(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (resolvedSelectionOrder == SelectionOrder.SORTED) {
SelectionSorter sorter;
if (sorterComparatorClass != null) {
Comparator<Object> sorterComparator = ConfigUtils.newInstance(this,
"sorterComparatorClass", sorterComparatorClass);
sorter = new ComparatorSelectionSorter(sorterComparator,
SelectionSorterOrder.resolve(sorterOrder));
} else if (sorterWeightFactoryClass != null) {
SelectionSorterWeightFactory sorterWeightFactory = ConfigUtils.newInstance(this,
"sorterWeightFactoryClass", sorterWeightFactoryClass);
sorter = new WeightFactorySelectionSorter(sorterWeightFactory,
SelectionSorterOrder.resolve(sorterOrder));
} else if (sorterClass != null) {
sorter = ConfigUtils.newInstance(this, "sorterClass", sorterClass);
} else {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with resolvedSelectionOrder (" + resolvedSelectionOrder
+ ") needs a sorterComparatorClass (" + sorterComparatorClass
+ ") or a sorterWeightFactoryClass (" + sorterWeightFactoryClass
+ ") or a sorterClass (" + sorterClass + ").");
}
moveSelector = new SortingMoveSelector(moveSelector, resolvedCacheType, sorter);
}
return moveSelector;
}
private void validateProbability(SelectionOrder resolvedSelectionOrder) {
if (probabilityWeightFactoryClass != null
&& resolvedSelectionOrder != SelectionOrder.PROBABILISTIC) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with probabilityWeightFactoryClass (" + probabilityWeightFactoryClass
+ ") has a resolvedSelectionOrder (" + resolvedSelectionOrder
+ ") that is not " + SelectionOrder.PROBABILISTIC + ".");
}
}
private MoveSelector applyProbability(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (resolvedSelectionOrder == SelectionOrder.PROBABILISTIC) {
if (probabilityWeightFactoryClass == null) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with resolvedSelectionOrder (" + resolvedSelectionOrder
+ ") needs a probabilityWeightFactoryClass ("
+ probabilityWeightFactoryClass + ").");
}
SelectionProbabilityWeightFactory probabilityWeightFactory = ConfigUtils.newInstance(this,
"probabilityWeightFactoryClass", probabilityWeightFactoryClass);
moveSelector = new ProbabilityMoveSelector(moveSelector,
resolvedCacheType, probabilityWeightFactory);
}
return moveSelector;
}
private void validateSelectedLimit(SelectionCacheType minimumCacheType) {
if (selectedCountLimit != null
&& minimumCacheType.compareTo(SelectionCacheType.JUST_IN_TIME) > 0) {
throw new IllegalArgumentException("The moveSelectorConfig (" + this
+ ") with selectedCountLimit (" + selectedCountLimit
+ ") has a minimumCacheType (" + minimumCacheType
+ ") that is higher than " + SelectionCacheType.JUST_IN_TIME + ".");
}
}
private MoveSelector applySelectedLimit(
SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (selectedCountLimit != null) {
moveSelector = new SelectedCountLimitMoveSelector(moveSelector, selectedCountLimit);
}
return moveSelector;
}
private MoveSelector applyShuffling(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (resolvedSelectionOrder == SelectionOrder.SHUFFLED) {
moveSelector = new ShufflingMoveSelector(moveSelector, resolvedCacheType);
}
return moveSelector;
}
private MoveSelector applyCaching(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder,
MoveSelector moveSelector) {
if (resolvedCacheType.isCached() && resolvedCacheType.compareTo(moveSelector.getCacheType()) > 0) {
moveSelector = new CachingMoveSelector(moveSelector, resolvedCacheType,
resolvedSelectionOrder.toRandomSelectionBoolean());
}
return moveSelector;
}
protected void inherit(MoveSelectorConfig inheritedConfig) {
super.inherit(inheritedConfig);
cacheType = ConfigUtils.inheritOverwritableProperty(cacheType, inheritedConfig.getCacheType());
selectionOrder = ConfigUtils.inheritOverwritableProperty(selectionOrder, inheritedConfig.getSelectionOrder());
filterClassList = ConfigUtils.inheritOverwritableProperty(
filterClassList, inheritedConfig.getFilterClassList());
sorterComparatorClass = ConfigUtils.inheritOverwritableProperty(
sorterComparatorClass, inheritedConfig.getSorterComparatorClass());
sorterWeightFactoryClass = ConfigUtils.inheritOverwritableProperty(
sorterWeightFactoryClass, inheritedConfig.getSorterWeightFactoryClass());
sorterOrder = ConfigUtils.inheritOverwritableProperty(
sorterOrder, inheritedConfig.getSorterOrder());
sorterClass = ConfigUtils.inheritOverwritableProperty(
sorterClass, inheritedConfig.getSorterClass());
probabilityWeightFactoryClass = ConfigUtils.inheritOverwritableProperty(
probabilityWeightFactoryClass, inheritedConfig.getProbabilityWeightFactoryClass());
selectedCountLimit = ConfigUtils.inheritOverwritableProperty(
selectedCountLimit, inheritedConfig.getSelectedCountLimit());
fixedProbabilityWeight = ConfigUtils.inheritOverwritableProperty(
fixedProbabilityWeight, inheritedConfig.getFixedProbabilityWeight());
}
}