|
1 | 1 | package ai.timefold.solver.core.impl.heuristic.selector.value; |
2 | 2 |
|
| 3 | +import static ai.timefold.solver.core.config.heuristic.selector.common.SelectionOrder.SORTED; |
| 4 | + |
3 | 5 | import java.util.ArrayList; |
4 | 6 | import java.util.Comparator; |
5 | 7 | import java.util.List; |
@@ -273,37 +275,38 @@ private static Class<? extends ComparatorFactory> determineComparatorFactoryClas |
273 | 275 |
|
274 | 276 | private SelectionSorter<Solution_, Object> determineSorter(GenuineVariableDescriptor<Solution_> variableDescriptor, |
275 | 277 | SelectionOrder resolvedSelectionOrder, ClassInstanceCache instanceCache) { |
276 | | - SelectionSorter<Solution_, Object> sorter = null; |
277 | | - if (resolvedSelectionOrder == ai.timefold.solver.core.config.heuristic.selector.common.SelectionOrder.SORTED) { |
278 | | - var sorterManner = config.getSorterManner(); |
279 | | - var comparatorClass = determineComparatorClass(config); |
280 | | - var comparatorFactoryClass = determineComparatorFactoryClass(config); |
281 | | - if (sorterManner != null) { |
282 | | - if (!ValueSelectorConfig.hasSorter(sorterManner, variableDescriptor)) { |
283 | | - return null; |
284 | | - } |
285 | | - sorter = ValueSelectorConfig.determineSorter(sorterManner, variableDescriptor); |
286 | | - } else if (comparatorClass != null) { |
287 | | - Comparator<Object> sorterComparator = |
288 | | - instanceCache.newInstance(config, determineComparatorPropertyName(config), comparatorClass); |
289 | | - sorter = new ComparatorSelectionSorter<>(sorterComparator, |
290 | | - SelectionSorterOrder.resolve(config.getSorterOrder())); |
291 | | - } else if (comparatorFactoryClass != null) { |
292 | | - var comparatorFactory = instanceCache.newInstance(config, determineComparatorFactoryPropertyName(config), |
293 | | - comparatorFactoryClass); |
294 | | - sorter = new ComparatorFactorySelectionSorter<>(comparatorFactory, |
295 | | - SelectionSorterOrder.resolve(config.getSorterOrder())); |
296 | | - } else if (config.getSorterClass() != null) { |
297 | | - sorter = instanceCache.newInstance(config, "sorterClass", config.getSorterClass()); |
298 | | - } else { |
299 | | - throw new IllegalArgumentException(""" |
300 | | - The valueSelectorConfig (%s) with resolvedSelectionOrder (%s) needs \ |
301 | | - a sorterManner (%s) or a %s (%s) or a %s (%s) \ |
302 | | - or a sorterClass (%s).""" |
303 | | - .formatted(config, resolvedSelectionOrder, sorterManner, determineComparatorPropertyName(config), |
304 | | - comparatorClass, determineComparatorFactoryPropertyName(config), comparatorFactoryClass, |
305 | | - config.getSorterClass())); |
| 278 | + if (resolvedSelectionOrder != SORTED) { |
| 279 | + return null; |
| 280 | + } |
| 281 | + SelectionSorter<Solution_, Object> sorter; |
| 282 | + var sorterManner = config.getSorterManner(); |
| 283 | + var comparatorClass = determineComparatorClass(config); |
| 284 | + var comparatorFactoryClass = determineComparatorFactoryClass(config); |
| 285 | + if (sorterManner != null) { |
| 286 | + if (!ValueSelectorConfig.hasSorter(sorterManner, variableDescriptor)) { |
| 287 | + return null; |
306 | 288 | } |
| 289 | + sorter = ValueSelectorConfig.determineSorter(sorterManner, variableDescriptor); |
| 290 | + } else if (comparatorClass != null) { |
| 291 | + Comparator<Object> sorterComparator = |
| 292 | + instanceCache.newInstance(config, determineComparatorPropertyName(config), comparatorClass); |
| 293 | + sorter = new ComparatorSelectionSorter<>(sorterComparator, |
| 294 | + SelectionSorterOrder.resolve(config.getSorterOrder())); |
| 295 | + } else if (comparatorFactoryClass != null) { |
| 296 | + var comparatorFactory = instanceCache.newInstance(config, determineComparatorFactoryPropertyName(config), |
| 297 | + comparatorFactoryClass); |
| 298 | + sorter = new ComparatorFactorySelectionSorter<>(comparatorFactory, |
| 299 | + SelectionSorterOrder.resolve(config.getSorterOrder())); |
| 300 | + } else if (config.getSorterClass() != null) { |
| 301 | + sorter = instanceCache.newInstance(config, "sorterClass", config.getSorterClass()); |
| 302 | + } else { |
| 303 | + throw new IllegalArgumentException(""" |
| 304 | + The valueSelectorConfig (%s) with resolvedSelectionOrder (%s) needs \ |
| 305 | + a sorterManner (%s) or a %s (%s) or a %s (%s) \ |
| 306 | + or a sorterClass (%s).""" |
| 307 | + .formatted(config, resolvedSelectionOrder, sorterManner, determineComparatorPropertyName(config), |
| 308 | + comparatorClass, determineComparatorFactoryPropertyName(config), comparatorFactoryClass, |
| 309 | + config.getSorterClass())); |
307 | 310 | } |
308 | 311 | return sorter; |
309 | 312 | } |
@@ -372,14 +375,14 @@ protected void validateSorting(SelectionOrder resolvedSelectionOrder) { |
372 | 375 | var sorterOrder = config.getSorterOrder(); |
373 | 376 | var sorterClass = config.getSorterClass(); |
374 | 377 | if ((sorterManner != null || comparatorClass != null || comparatorFactoryClass != null |
375 | | - || sorterOrder != null || sorterClass != null) && resolvedSelectionOrder != SelectionOrder.SORTED) { |
| 378 | + || sorterOrder != null || sorterClass != null) && resolvedSelectionOrder != SORTED) { |
376 | 379 | throw new IllegalArgumentException(""" |
377 | 380 | The valueSelectorConfig (%s) with sorterManner (%s) \ |
378 | 381 | and %s (%s) and %s (%s) and sorterOrder (%s) and sorterClass (%s) \ |
379 | 382 | has a resolvedSelectionOrder (%s) that is not %s.""" |
380 | 383 | .formatted(config, sorterManner, comparatorPropertyName, comparatorClass, comparatorFactoryPropertyName, |
381 | 384 | comparatorFactoryClass, sorterOrder, sorterClass, resolvedSelectionOrder, |
382 | | - SelectionOrder.SORTED)); |
| 385 | + SORTED)); |
383 | 386 | } |
384 | 387 | assertNotSorterMannerAnd(config, comparatorPropertyName, ValueSelectorFactory::determineComparatorClass); |
385 | 388 | assertNotSorterMannerAnd(config, comparatorFactoryPropertyName, |
@@ -421,7 +424,7 @@ private static void assertNotSorterClassAnd(ValueSelectorConfig config, String p |
421 | 424 |
|
422 | 425 | protected ValueSelector<Solution_> applySorting(SelectionCacheType resolvedCacheType, SelectionOrder resolvedSelectionOrder, |
423 | 426 | ValueSelector<Solution_> valueSelector, ClassInstanceCache instanceCache) { |
424 | | - if (resolvedSelectionOrder == SelectionOrder.SORTED) { |
| 427 | + if (resolvedSelectionOrder == SORTED) { |
425 | 428 | var sorter = determineSorter(valueSelector.getVariableDescriptor(), resolvedSelectionOrder, instanceCache); |
426 | 429 | if (!valueSelector.getVariableDescriptor().canExtractValueRangeFromSolution() |
427 | 430 | && resolvedCacheType == SelectionCacheType.STEP) { |
|
0 commit comments