/
Chapter-Move_and_neighborhood_selection.xml
1352 lines (1042 loc) · 61.5 KB
/
Chapter-Move_and_neighborhood_selection.xml
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<chapter version="5.0"
xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd http://www.w3.org/1999/xlink http://www.docbook.org/xml/5.0/xsd/xlink.xsd"
xml:base="../" xml:id="moveAndNeighborhoodSelection" xmlns="http://docbook.org/ns/docbook"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:ns="http://docbook.org/ns/docbook">
<title><literal>Move</literal> and neighborhood selection</title>
<section xml:id="moveAndNeighborhoodSelectionIntroduction">
<title><literal>Move</literal> and neighborhood introduction</title>
<section xml:id="whatIsAMove">
<title>What is a <literal>Move</literal>?</title>
<para>A <literal>Move</literal> is a change (or set of changes) from a solution A to a solution B. For example,
the move below changes queen <literal>C</literal> from row <literal>0</literal> to row
<literal>2</literal>:</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/singleMoveNQueens04.png"/>
</imageobject>
</mediaobject>
<para>The new solution is called a <emphasis>neighbor</emphasis> of the original solution, because it can be
reached in a single <literal>Move</literal>. Although a single move can change multiple queens, the neighbors of a
solution should always be a very small subset of all possible solutions. For example, on that original solution,
these are all possible <literal>changeMove</literal>'s:</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/possibleMovesNQueens04.png"/>
</imageobject>
</mediaobject>
<para>If we ignore the 4 <literal>changeMove</literal>'s that have not impact and are therefore not doable, we can
see that number of moves is <literal>n * (n - 1) = 12</literal>. This is far less than the number of possible
solutions, which is <literal>n ^ n = 256</literal>. As the problem scales out, the number of possible moves
increases far less than the number of possible solutions.</para>
<para>Yet, in 4 <literal>changeMove</literal>'s or less we can reach any solution. For example we can reach a very
different solution in 3 <literal>changeMove</literal>'s:</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/sequentialMovesNQueens04.png"/>
</imageobject>
</mediaobject>
<note>
<para>There are many other types of moves besides <literal>changeMove</literal>'s. Many move types are included
out-of-the-box, but you can also implement custom moves.</para>
<para>A <literal>Move</literal> can affect multiple entities or even create/delete entities. But it must not
change the problem facts.</para>
</note>
<para>All optimization algorithms use <literal>Move</literal>'s to transition from one solution to a neighbor
solution. Therefor, all the optimization algorithms are confronted with <literal>Move</literal> selection: the
craft of creating and iterating moves efficiently and the art of finding the most promising subset of random moves
to evaluate first.</para>
</section>
<section xml:id="whatIsAMoveSelector">
<title>What is a <literal>MoveSelector</literal>?</title>
<para>A <literal>MoveSelector</literal>'s main function is to create <literal>Iterator<Move></literal> when
needed. An optimization algorithm will iterate through a subset of those moves.</para>
<para>Here's an example how to configure a <literal>changeMoveSelector</literal> for the optimization algorithm
Local Search:</para>
<programlisting language="xml"> <localSearch>
<changeMoveSelector/>
...
</localSearch></programlisting>
<para>Out of the box, this works and all properties of the <literal>changeMoveSelector</literal> are defaulted
sensibly (unless that fails fast due to ambiguity). On the other hand, the configuration can be customized
significantly for specific use cases. For example: you might want to configure a filter to discard pointless
moves.</para>
</section>
<section xml:id="subselectingOfEntitiesValuesAndOtherMoves">
<title>Subselecting of entities, values and other moves</title>
<para>To create a <literal>Move</literal>, a <literal>MoveSelector</literal> needs to select 1 or more planning
entities and/or planning values to move. Just like <literal>MoveSelector</literal>s,
<literal>EntitySelector</literal>s and <literal>ValueSelector</literal>s need to support a similar feature set
(such as scalable just-in-time selection). Therefore, they all implement a common interface
<literal>Selector</literal> and they are configured similarly.</para>
<para>A MoveSelector is often composed out of <literal>EntitySelector</literal>s,
<literal>ValueSelector</literal>s or even other <literal>MoveSelector</literal>s, which can be configured
individually if desired:</para>
<programlisting language="xml"> <unionMoveSelector>
<changeMoveSelector>
<entitySelector>
...
</entitySelector>
<valueSelector>
...
</valueSelector>
...
</changeMoveSelector>
<swapMoveSelector>
...
</swapMoveSelector>
</unionMoveSelector></programlisting>
<para>Together, this structure forms a <literal>Selector</literal> tree:</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/selectorTree.png"/>
</imageobject>
</mediaobject>
<para>The root of this tree is a <literal>MoveSelector</literal> which is injected into the optimization algorithm
implementation to be (partially) iterated in every step.</para>
</section>
</section>
<section xml:id="genericMoveSelectors">
<title>Generic MoveSelectors</title>
<section xml:id="changeMoveSelector">
<title><literal>changeMoveSelector</literal></title>
<para>For 1 planning variable, the <literal>ChangeMove</literal> selects 1 planning entity and 1 planning value
and assigns the entity's variable to that value.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/changeMove.png"/>
</imageobject>
</mediaobject>
<para>Simplest configuration:</para>
<programlisting language="xml"> <changeMoveSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <changeMoveSelector>
... <!-- Normal selector properties -->
<entitySelector>
<entityClass>...Lecture</entityClass>
...
</entitySelector>
<valueSelector>
<variableName>room</variableName>
...
</valueSelector>
</changeMoveSelector></programlisting>
<para>A <literal>ChangeMove</literal> is the finest grained move.</para>
<important>
<para>Almost every <literal>moveSelector</literal> configuration injected into a metaheuristic algorithm should
include a changeMoveSelector or a custom implementation. This guarantees that every possible
<literal>Solution</literal> can be reached through applying a number of moves in sequence (not taking <link
linkend="scoreTrap">score traps</link> into account). Of course, normally it is unioned with other, more course
grained move selectors.</para>
</important>
</section>
<section xml:id="swapMoveSelector">
<title>swapMoveSelector</title>
<para>The <literal>SwapMove</literal> selects 2 different planning entities and swaps the planning values of all
their planning variables.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/swapMove.png"/>
</imageobject>
</mediaobject>
<para>Although a <literal>SwapMove</literal> on a single variable is essentially just 2
<literal>ChangeMove</literal>s, it's often the winning step where the first of the 2
<literal>ChangeMove</literal>s would not be the winning step because it leave the solution in a state with broken
hard constraints. For example: swapping the room of 2 lectures doesn't bring the solution in a intermediate state
where both lectures are in the same room which breaks a hard constraint.</para>
<para>Simplest configuration:</para>
<programlisting language="xml"> <swapMoveSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <swapMoveSelector>
... <!-- Normal selector properties -->
<entitySelector>
<entityClass>...Lecture</entityClass>
...
</entitySelector>
<secondaryEntitySelector>
...
</secondaryEntitySelector>
<variableNameInclude>room</variableNameInclude>
<variableNameInclude>...</variableNameInclude>
</swapMoveSelector></programlisting>
<para>The <literal>secondaryEntitySelector</literal> is rarely needed: if it is not specified, entities from the
same <literal>entitySelector</literal> are swapped.</para>
<para>If one or more <literal>variableNameInclude</literal> properties are specified, not all planning variables
will be swapped, but only those specified. For example for course scheduling, specifying only
<literal>variableNameInclude</literal> room will make it only swap room, not period.</para>
</section>
<section xml:id="pillarSwapMoveSelector">
<title>pillarSwapMoveSelector</title>
<para>A <emphasis>pillar</emphasis> is a set of planning entities which have the same planning value(s) for each
of their planning variables. The <literal>pillarSwapMove</literal> selects 2 different entity pillars and swaps
the values of all their variables for all their entities.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/pillarSwapMove.png"/>
</imageobject>
</mediaobject>
<para>Simplest configuration:</para>
<programlisting language="xml"> <pillarSwapMoveSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <pillarSwapMoveSelector>
... <!-- Normal selector properties -->
<pillarSelector>
<entitySelector>
<entityClass>...Lecture</entityClass>
...
</entitySelector>
</pillarSelector>
<secondaryPillarSelector>
<entitySelector>
...
</entitySelector>
</secondaryPillarSelector>
<variableNameInclude>room</variableNameInclude>
<variableNameInclude>...</variableNameInclude>
</pillarSwapMoveSelector></programlisting>
<para>The <literal>secondaryPillarSelector</literal> is rarely needed: if it is not specified, entities from the
same <literal>pillarSelector</literal> are swapped.</para>
<para>The other properties are explained in <link linkend="swapMoveSelector">swapMoveSelector</link>.</para>
</section>
<section xml:id="subChainChangeMoveSelector">
<title>subChainChangeMoveSelector</title>
<para>A <emphasis>subChain</emphasis> is a set of planning entities with a chained planning variable which form
part of a chain. The <literal>subChainChangeMove</literal> selects a subChain and moves it to another place in a
different or the same anchor chain.</para>
<para>Simplest configuration:</para>
<programlisting language="xml"> <subChainChangeMoveSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <subChainChangeMoveSelector>
... <!-- Normal selector properties -->
<subChainSelector>
<entitySelector>
<entityClass>...Customer</entityClass>
...
</entitySelector>
<minimumSubChainSize>2</minimumSubChainSize>
<maximumSubChainSize>40</maximumSubChainSize>
</subChainSelector>
<valueSelector>
<variableName>previousStandstill</variableName>
...
</valueSelector>
<selectReversingMoveToo>true</selectReversingMoveToo>
</subChainChangeMoveSelector></programlisting>
<para>The <literal>subChainSelector</literal> selects a number of entities, no less than
<literal>minimumSubChainSize</literal> (defaults to 2) and no more than <literal>maximumSubChainSize</literal>
(defaults to infinity).</para>
<para>The property <literal>selectReversingMoveToo</literal> (defaults to true) enabled selecting the reverse of
every subchain too.</para>
</section>
<section xml:id="subChainSwapMoveSelector">
<title>subChainSwapMoveSelector</title>
<para>The <literal>subChainSwapMove</literal> selects 2 different subChains and moves it to another place in a
different or the same anchor chain.</para>
<para>Simplest configuration:</para>
<programlisting language="xml"> <subChainSwapMoveSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <subChainSwapMoveSelector>
... <!-- Normal selector properties -->
<subChainSelector>
<entitySelector>
<entityClass>...Customer</entityClass>
...
</entitySelector>
<minimumSubChainSize>2</minimumSubChainSize>
<maximumSubChainSize>40</maximumSubChainSize>
</subChainSelector>
<secondarySubChainSelector>
<entitySelector>
<entityClass>...Customer</entityClass>
...
</entitySelector>
<minimumSubChainSize>2</minimumSubChainSize>
<maximumSubChainSize>40</maximumSubChainSize>
</secondarySubChainSelector>
<selectReversingMoveToo>true</selectReversingMoveToo>
</subChainSwapMoveSelector></programlisting>
<para>The <literal>secondarySubChainSelector</literal> is rarely needed: if it is not specified, entities from the
same <literal>subChainSelector</literal> are swapped.</para>
<para>The other properties are explained in <link
linkend="subChainChangeMoveSelector">subChainChangeMoveSelector</link>.</para>
</section>
</section>
<section xml:id="combiningMultipleMoveSelectors">
<title>Combining multiple <literal>MoveSelector</literal>s</title>
<section xml:id="unionMoveSelector">
<title>unionMoveSelector</title>
<para>A <literal>unionMoveSelector</literal> selects a <literal>Move</literal> by selecting 1 of its child
<literal>MoveSelector</literal>s to supply the next <literal>Move</literal>.</para>
<para>Simplest configuration:</para>
<programlisting language="xml"> <unionMoveSelector>
<...MoveSelector/>
<...MoveSelector/>
<...MoveSelector/>
...
</unionMoveSelector></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <unionMoveSelector>
... <!-- Normal selector properties -->
<selectorProbabilityWeightFactoryClass>...ProbabilityWeightFactory</selectorProbabilityWeightFactoryClass>
<changeMoveSelector>
<fixedProbabilityWeight>...</fixedProbabilityWeight>
...
</changeMoveSelector>
<swapMoveSelector>
<fixedProbabilityWeight>...</fixedProbabilityWeight>
...
</swapMoveSelector>
<...MoveSelector>
<fixedProbabilityWeight>...</fixedProbabilityWeight>
...
</...MoveSelector>
...
</unionMoveSelector></programlisting>
<para>The <literal>selectorProbabilityWeightFactory</literal> determines in <literal>selectionOrder</literal>
<literal>RANDOM</literal> how often a child MoveSelector is selected to supply the next Move. By default, each
child <literal>MoveSelector</literal> has the same chance of being selected.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/selectorProbabilityInUnion.png"/>
</imageobject>
</mediaobject>
<para>Change the fixedProbabilityWeight of such a child to select it more often. For example, the
<literal>unionMoveSelector</literal> can return a <literal>SwapMove</literal> twice as often as a
<literal>ChangeMove</literal>:</para>
<programlisting language="xml"> <unionMoveSelector>
<changeMoveSelector>
<fixedProbabilityWeight>1.0</fixedProbabilityWeight>
...
</changeMoveSelector>
<swapMoveSelector>
<fixedProbabilityWeight>2.0</fixedProbabilityWeight>
...
</swapMoveSelector>
</unionMoveSelector></programlisting>
<para>The number of possible <literal>ChangeMove</literal>s is very different from the number of possible
<literal>SwapMove</literal>s and furthermore it's problem dependent. To give each individual
<literal>Move</literal> the same selection chance (as opposed to each <literal>MoveSelector</literal>), use the
<literal>FairSelectorProbabilityWeightFactory</literal>:</para>
<programlisting language="xml"> <unionMoveSelector>
<selectorProbabilityWeightFactoryClass>org.optaplanner.core.impl.heuristic.selector.common.decorator.FairSelectorProbabilityWeightFactory</selectorProbabilityWeightFactoryClass>
<changeMoveSelector/>
<swapMoveSelector/>
</unionMoveSelector></programlisting>
</section>
<section xml:id="cartesianProductMoveSelector">
<title>cartesianProductMoveSelector</title>
<para>A <literal>cartesianProductMoveSelector</literal> selects a new <literal>CompositeMove</literal>. It builds
that <literal>CompositeMove</literal> by selecting 1 <literal>Move</literal> per child
<literal>MoveSelector</literal> and adding it to the <literal>CompositeMove</literal>.</para>
<para>Simplest configuration:</para>
<programlisting language="xml"> <cartesianProductMoveSelector>
<...MoveSelector/>
<...MoveSelector/>
<...MoveSelector/>
...
</cartesianProductMoveSelector></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <cartesianProductMoveSelector>
... <!-- Normal selector properties -->
<ignoreEmptyChildIterators>true</ignoreEmptyChildIterators>
<changeMoveSelector>
...
</changeMoveSelector>
<swapMoveSelector>
...
</swapMoveSelector>
<...MoveSelector>
...
</...MoveSelector>
...
</cartesianProductMoveSelector></programlisting>
<para>The propery <literal>ignoreEmptyChildIterators</literal> (true by default) will ignore every empty
<literal>childMoveSelector</literal> to avoid returning no moves. For example: a cartesian product of
<literal>changeMoveSelector</literal> A and B, for which B is empty (because all it's entities are immovable)
returns no moves if <literal>ignoreEmptyChildIterators</literal> is <literal>false</literal> and the moves of A if
<literal>ignoreEmptyChildIterators</literal> is <literal>true</literal>.</para>
</section>
</section>
<section xml:id="entitySelector">
<title>EntitySelector</title>
<para>Simplest configuration:</para>
<programlisting language="xml"> <entitySelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <entitySelector>
... <!-- Normal selector properties -->
<entityClass>org.optaplanner.examples.curriculumcourse.domain.Lecture</entityClass>
</entitySelector></programlisting>
<para>The <literal>entityClass</literal> property is only required if it cannot be deduced automatically because
there are multiple entity classes.</para>
</section>
<section xml:id="valueSelector">
<title>ValueSelector</title>
<para>Simplest configuration:</para>
<programlisting language="xml"> <valueSelector/></programlisting>
<para>Advanced configuration:</para>
<programlisting language="xml"> <valueSelector>
... <!-- Normal selector properties -->
<variableName>room</variableName>
</valueSelector></programlisting>
<para>The <literal>variableName</literal> property is only required if it cannot be deduced automatically because
there are multiple variables (for the related entity class).</para>
</section>
<section xml:id="generalSelectorFeatures">
<title>General <literal>Selector</literal> features</title>
<section xml:id="cacheType">
<title><literal>CacheType</literal>: Create moves ahead of time or Just In Time</title>
<para>A <literal>Selector</literal>'s <literal>cacheType</literal> determines when a selection (such as a
<literal>Move</literal>, an entity, a value, ...) is created and how long it lives.</para>
<para>Almost every <literal>Selector</literal> supports setting a <literal>cacheType</literal>:</para>
<programlisting language="xml"> <changeMoveSelector>
<cacheType>PHASE</cacheType>
...
</changeMoveSelector></programlisting>
<para>The following <literal>cacheType</literal>s are supported:</para>
<itemizedlist>
<listitem>
<para><literal>JUST_IN_TIME</literal> (default): Not cached. Construct each selection
(<literal>Move</literal>, ...) just before it's used. This scales up well in memory footprint.</para>
</listitem>
<listitem>
<para><literal>STEP</literal>: Cached. Create each selection (<literal>Move</literal>, ...) at the beginning
of a step and cache them in a list for the remainder of the step. This scales up badly in memory
footprint.</para>
</listitem>
<listitem>
<para><literal>PHASE</literal>: Cached. Create each selection (<literal>Move</literal>, ...) at the beginning
of a <literal>SolverPhase</literal> and cache them in a list for the remainder of the
<literal>SolverPhase</literal>. Some selections cannot be phase cached because the list changes every step.
This scales up badly in memory footprint, but has a slight performance gain.</para>
</listitem>
<listitem>
<para><literal>SOLVER</literal>: Cached. Create each selection (<literal>Move</literal>, ...) at the beginning
of a <literal>Solver</literal> and cache them in a list for the remainder of the <literal>Solver</literal>.
Some selections cannot be solver cached because the list changes every step. This scales up badly in memory
footprint, but has a slight performance gain.</para>
</listitem>
</itemizedlist>
<para>A <literal>cacheType</literal> can be set on composite selectors too:</para>
<programlisting language="xml"> <unionMoveSelector>
<cacheType>PHASE</cacheType>
<changeMoveSelector/>
<swapMoveSelector/>
...
</unionMoveSelector></programlisting>
<para>Nested selectors of a cached selector cannot be configured to be cached themselves, unless it's a higher
<literal>cacheType</literal>. For example: a <literal>STEP</literal> cached <literal>unionMoveSelector</literal>
can hold a <literal>PHASE</literal> cached <literal>changeMoveSelector</literal>, but not a
<literal>STEP</literal> cached <literal>changeMoveSelector</literal>.</para>
</section>
<section xml:id="selectionOrder">
<title>SelectionOrder: original, sorted, random, shuffled or probabilistic</title>
<para>A <literal>Selector</literal>'s <literal>selectionOrder</literal> determines the order in which the
selections (such as <literal>Move</literal>s, entities, values, ...) are iterated. An optimization algorithm will
usually only iterate through a subset of its <literal>MoveSelector</literal>'s selections, starting from the
start, so the <literal>selectionOrder</literal> is critical to decide which <literal>Move</literal>s are actually
evaluated.</para>
<para>Almost every <literal>Selector</literal> supports setting a <literal>selectionOrder</literal>:</para>
<programlisting language="xml"> <changeMoveSelector>
...
<selectionOrder>RANDOM</selectionOrder>
...
</changeMoveSelector></programlisting>
<para>The following <literal>selectionOrder</literal>s are supported:</para>
<itemizedlist>
<listitem>
<para><literal>ORIGINAL</literal>: Select the selections (<literal>Move</literal>s, entities, values, ...) in
default order. Each selection will be selected only once.</para>
<itemizedlist>
<listitem>
<para>For example: A0, A1, A2, A3, ..., B0, B1, B2, B3, ..., C0, C1, C2, C3, ...</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>SORTED: Select the selections (<literal>Move</literal>s, entities, values, ...) in sorted order. Each
selection will be selected only once. Requires <literal>cacheType >= STEP</literal>. Mostly used on an
<literal>entitySelector</literal> or <literal>valueSelector</literal> for construction heuristics. See <link
linkend="sortedSelection">sorted selection</link>.</para>
<itemizedlist>
<listitem>
<para>For example: A0, B0, C0, ..., A2, B2, C2, ..., A1, B1, C1, ...</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>RANDOM (default): Select the selections (<literal>Move</literal>s, entities, values, ...) in
non-shuffled random order. A selection might be selected multiple times. This scales up well in performance
because it does not require caching.</para>
<itemizedlist>
<listitem>
<para>For example: C2, A3, B1, C2, A0, C0, ...</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>SHUFFLED: Select the selections (<literal>Move</literal>s, entities, values, ...) in shuffled random
order. Each selection will be selected only once. Requires <literal>cacheType >= STEP</literal>. This
scales up badly in performance, not just because it requires caching, but also because a random number is
generated for each element, even if it's not selected (which is the grand majority when scaling up).</para>
<itemizedlist>
<listitem>
<para>For example: C2, A3, B1, A0, C0, ...</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>PROBABILISTIC: Select the selections (<literal>Move</literal>s, entities, values, ...) in random order,
based on the selection probability of each element. A selection with a higher probability has a higher chance
to be selected than elements with a lower probability. A selection might be selected multiple times. Requires
<literal>cacheType >= STEP</literal>. Mostly used on an <literal>entitySelector</literal> or
<literal>valueSelector</literal>. See <link linkend="probabilisticSelection">probabilistic
selection</link>.</para>
<itemizedlist>
<listitem>
<para>For example: B1, B1, A1, B2, B1, C2, B1, B1, ...</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>A <literal>selectionOrder</literal> can be set on composite selectors too.</para>
<note>
<para>When a <literal>Selector</literal> is cached, all of its nested <literal>Selector</literal>s will
naturally default to <literal>selectionOrder</literal> <literal>ORIGINAL</literal>. Avoid overwriting the
<literal>selectionOrder</literal> of those nested <literal>Selector</literal>s.</para>
</note>
</section>
<section xml:id="recommendedCombinationsOfCacheTypeAndSelectionOrder">
<title>Recommended combinations of <literal>CacheType</literal> and <literal>SelectionOrder</literal></title>
<section xml:id="justInTimeRandomSelection">
<title>Just in time random selection (default)</title>
<para>This combination is great for big use cases (10 000 entities or more), as it scales up well in memory
footprint and performance. Other combinations are often not even viable on such sizes. It works for smaller use
cases too, so it's a good way to start out. It's the default, so this explicit configuration of
<literal>cacheType</literal> and <literal>selectionOrder</literal> is actually obsolete:</para>
<programlisting language="xml"> <unionMoveSelector>
<cacheType>JUST_IN_TIME</cacheType>
<selectionOrder>RANDOM</selectionOrder>
<changeMoveSelector/>
<swapMoveSelector/>
</unionMoveSelector></programlisting>
<para>Here's how it works. When <literal>Iterator<Move>.next()</literal> is called, a child
<literal>MoveSelector</literal> is randomly selected (1), which creates a random <literal>Move</literal> is
created (2, 3, 4) and is then returned (5):</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/jitRandomSelection.png"/>
</imageobject>
</mediaobject>
<para>Notice that <emphasis role="bold">it never creates a list of <literal>Move</literal>s</emphasis> and it
generates random numbers only for <literal>Move</literal>s that are actually selected.</para>
</section>
<section xml:id="cachedShuffledSelection">
<title>Cached shuffled selection</title>
<para>This combination often wins for small and medium use cases (5000 entities or less). Beyond that size, it
scales up badly in memory footprint and performance.</para>
<programlisting language="xml"> <unionMoveSelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SHUFFLED</selectionOrder>
<changeMoveSelector/>
<swapMoveSelector/>
</unionMoveSelector></programlisting>
<para>Here's how it works: At the start of the phase (or step depending on the <literal>cacheType</literal>),
all moves are created (1) and cached (2). When <literal>MoveSelector.iterator()</literal> is called, the moves
are shuffled (3). When <literal>Iterator<Move>.next()</literal> is called, the next element in the
shuffled list is returned (4):</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/cachedShuffledSelection.png"/>
</imageobject>
</mediaobject>
<para>Notice that <emphasis role="bold">each <literal>Move</literal> will only be selected once</emphasis>, even
though they are selected in random order.</para>
<para>Use cacheType PHASE if none of the (possibly nested) Selectors require <literal>STEP</literal>. Otherwise,
do something like this:</para>
<programlisting language="xml"> <unionMoveSelector>
<cacheType>STEP</cacheType>
<selectionOrder>SHUFFLED</selectionOrder>
<changeMoveSelector>
<cacheType>PHASE</cacheType>
</changeMoveSelector>
<swapMoveSelector/>
<cacheType>PHASE</cacheType>
</swapMoveSelector>
<pillarSwapMoveSelector/><!-- Does not support cacheType PHASE -->
</unionMoveSelector></programlisting>
</section>
<section xml:id="cachedRandomSelection">
<title>Cached random selection</title>
<para>This combination is often a worthy competitor for medium use cases, especially with fast stepping
optimization algorithms (such as simulated annealing). Unlike cached shuffled selection, it doesn't waste time
shuffling the move list at the beginning of every step.</para>
<programlisting language="xml"> <unionMoveSelector>
<cacheType>PHASE</cacheType>
<selectionOrder>RANDOM</selectionOrder>
<changeMoveSelector/>
<swapMoveSelector/>
</unionMoveSelector></programlisting>
</section>
</section>
<section xml:id="filteredSelection">
<title>Filtered selection</title>
<para>There can be certain moves that you don't want to select, because:</para>
<itemizedlist>
<listitem>
<para>The move is pointless and would only waste CPU time. For example, swapping 2 lectures of the same course
will result in the same score and the same schedule because all lectures of 1 course are interchangeable (same
teacher, same students, same topic).</para>
</listitem>
<listitem>
<para>Doing the move would break <link linkend="buildInHardConstraint">a build-in hard constraint</link>, so
the solution would be infeasible but the score function doesn't check build-in hard constraints (for
performance gain). For example, don't change a gym lecture to a room which is not a gym room.</para>
<itemizedlist>
<listitem>
<para>Note that any build-in hard constraint must usually be filtered on every move type. For example,
also don't swap the room of a gym lecture with another lecture if the other lecture's original room isn't
a gym room.</para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>Filtered selection can happen on any Selector in the selector tree, including any
<literal>MoveSelector</literal>, <literal>EntitySelector</literal> or <literal>ValueSelector</literal>. It works
with any <literal>cacheType</literal> and <literal>selectionOrder</literal>.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/filteredSelection.png"/>
</imageobject>
</mediaobject>
<para>Filtering uses the interface <literal>SelectionFilter</literal>:</para>
<programlisting language="java">public interface SelectionFilter<T> {
boolean accept(ScoreDirector scoreDirector, T selection);
}</programlisting>
<para>Implement the method <literal>accept</literal> to return <literal>false</literal> on a discarded
<literal>selection</literal>. Unaccepted moves will not be selected and will therefore never have their method
<literal>doMove</literal> called.</para>
<programlisting language="java">public class DifferentCourseSwapMoveFilter implements SelectionFilter<SwapMove> {
public boolean accept(ScoreDirector scoreDirector, SwapMove move) {
Lecture leftLecture = (Lecture) move.getLeftEntity();
Lecture rightLecture = (Lecture) move.getRightEntity();
return !leftLecture.getCourse().equals(rightLecture.getCourse());
}
}</programlisting>
<para>Apply the filter on the lowest level possible. In most cases, you 'll need to know both the entity and the
value involved and you'll have to apply a <literal>filterClass</literal> on the
<literal>moveSelector</literal>:</para>
<programlisting language="xml"> <swapMoveSelector>
<filterClass>org.optaplanner.examples.curriculumcourse.solver.move.DifferentCourseSwapMoveFilter</filterClass>
</swapMoveSelector></programlisting>
<para>But if possible, apply it on a lower levels, such as a <literal>filterClass</literal> on the
<literal>entitySelector</literal> or <literal>valueSelector</literal>:</para>
<programlisting language="xml"> <changeMoveSelector>
<entitySelector>
<filterClass>...EntityFilter</filterClass>
</entitySelector>
</changeMoveSelector></programlisting>
<para>You can configure multiple <literal>filterClass</literal> elements on a single selector.</para>
</section>
<section xml:id="sortedSelection">
<title>Sorted selection</title>
<para>Sorted selection can happen on any Selector in the selector tree, including any
<literal>MoveSelector</literal>, <literal>EntitySelector</literal> or <literal>ValueSelector</literal>. It does
not work with <literal>cacheType</literal> <literal>JUST_IN_TIME</literal> and <literal>it only works with
selectionOrder</literal> <literal>SORTED</literal>.</para>
<para>It's mostly used in construction heuristics.</para>
<note>
<para>If the choosen construction heuristic implies sorting, for example <literal>FIRST_FIT_DECREASING</literal>
implies that the <literal>EntitySelector</literal> is sorted, there is no need to explicitly configure a
<literal>Selector</literal> with sorting. If you do explicitely configure the <literal>Selector</literal>, it
overwrites the default settings of that construction heuristic.</para>
</note>
<section xml:id="sortedSelectionBySorterManner">
<title>Sorted selection by <literal>SorterManner</literal></title>
<para>Some <literal>Selector</literal> types implement a <literal>SorterManner</literal> out of the box:</para>
<itemizedlist>
<listitem>
<para><literal>EntitySelector</literal> supports:</para>
<itemizedlist>
<listitem>
<para><literal>DECREASING_DIFFICULTY</literal>: Sorts the planning entities according to decreasing
<link linkend="planningEntityDifficulty">planning entity difficulty</link>. Requires that planning
entity difficulty is annotated on the domain model.</para>
<programlisting language="xml"> <entitySelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterManner>DECREASING_DIFFICULTY</sorterManner>
</entitySelector></programlisting>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para><literal>ValueSelector</literal> supports:</para>
<itemizedlist>
<listitem>
<para><literal>INCREASING_STRENGTH</literal>: Sorts the planning values according to increasing <link
linkend="planningValueStrength">planning value strength</link>. Requires that planning value strength is
annotated on the domain model.</para>
<programlisting language="xml"> <valueSelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterManner>INCREASING_STRENGTH</sorterManner>
</valueSelector></programlisting>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</section>
<section xml:id="sortedSelectionByComparator">
<title>Sorted selection by <literal>Comparator</literal></title>
<para>An easy way to sort a <literal>Selector</literal> is with a plain old
<literal>Comparator</literal>:</para>
<programlisting language="java">public class CloudProcessDifficultyComparator implements Comparator<CloudProcess> {
public int compare(CloudProcess a, CloudProcess b) {
return new CompareToBuilder()
.append(a.getRequiredMultiplicand(), b.getRequiredMultiplicand())
.append(a.getId(), b.getId())
.toComparison();
}
}</programlisting>
<para>You 'll also need to configure it (unless it's annotated on the domain model and automatically applied by
the optimization algorithm):</para>
<programlisting language="xml"> <entitySelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterComparatorClass>...CloudProcessDifficultyComparator</sorterComparatorClass>
<sorterOrder>DESCENDING</sorterOrder>
</entitySelector></programlisting>
</section>
<section xml:id="sortedSelectionBySelectionSorterWeightFactory">
<title>Sorted selection by <literal>SelectionSorterWeightFactory</literal></title>
<para>If you need the entire <literal>Solution</literal> to sort a <literal>Selector</literal>, use a
<literal>SelectionSorterWeightFactory</literal> instead:</para>
<programlisting language="java">public interface SelectionSorterWeightFactory<Sol extends Solution, T> {
Comparable createSorterWeight(Sol solution, T selection);
}</programlisting>
<programlisting language="java">public class QueenDifficultyWeightFactory implements SelectionSorterWeightFactory<NQueens, Queen> {
public Comparable createSorterWeight(NQueens nQueens, Queen queen) {
int distanceFromMiddle = calculateDistanceFromMiddle(nQueens.getN(), queen.getColumnIndex());
return new QueenDifficultyWeight(queen, distanceFromMiddle);
}
// ...
public static class QueenDifficultyWeight implements Comparable<QueenDifficultyWeight> {
private final Queen queen;
private final int distanceFromMiddle;
public QueenDifficultyWeight(Queen queen, int distanceFromMiddle) {
this.queen = queen;
this.distanceFromMiddle = distanceFromMiddle;
}
public int compareTo(QueenDifficultyWeight other) {
return new CompareToBuilder()
// The more difficult queens have a lower distance to the middle
.append(other.distanceFromMiddle, distanceFromMiddle) // Decreasing
// Tie breaker
.append(queen.getColumnIndex(), other.queen.getColumnIndex())
.toComparison();
}
}
}</programlisting>
<para>You 'll also need to configure it (unless it's annotated on the domain model and automatically applied by
the optimization algorithm):</para>
<programlisting language="xml"> <entitySelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterWeightFactoryClass>...QueenDifficultyWeightFactory</sorterWeightFactoryClass>
<sorterOrder>DESCENDING</sorterOrder>
</entitySelector></programlisting>
</section>
<section xml:id="sortedSelectionBySelectionSorter">
<title>Sorted selection by <literal>SelectionSorter</literal></title>
<para>Alternatively, you can also use the interface <literal>SelectionSorter</literal> directly:</para>
<programlisting language="java">public interface SelectionSorter<T> {
void sort(ScoreDirector scoreDirector, List<T> selectionList);
}</programlisting>
<programlisting language="xml"> <entitySelector>
<cacheType>PHASE</cacheType>
<selectionOrder>SORTED</selectionOrder>
<sorterClass>...MyEntitySorter</sorterClass>
</entitySelector></programlisting>
</section>
</section>
<section xml:id="probabilisticSelection">
<title>Probabilistic selection</title>
<para>Probabilistic selection can happen on any Selector in the selector tree, including any
<literal>MoveSelector</literal>, <literal>EntitySelector</literal> or <literal>ValueSelector</literal>. It does
not work with <literal>cacheType</literal> <literal>JUST_IN_TIME</literal> and <literal>it only works with
selectionOrder</literal> <literal>PROBABILISTIC</literal>.</para>
<mediaobject>
<imageobject>
<imagedata fileref="images/Chapter-Move_and_neighborhood_selection/probabilisticSelection.png"/>
</imageobject>
</mediaobject>
<para>Each selection has a <literal>probabilityWeight</literal>, which determines the chance that's that selection
will be selected:</para>
<programlisting language="java">public interface SelectionProbabilityWeightFactory<T> {
double createProbabilityWeight(ScoreDirector scoreDirector, T selection);
}</programlisting>