Synthia
Generic and flexible data structure generator
MutatedLists.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.mutation;
20 
21 import java.util.List;
22 
29 import ca.uqac.lif.synthia.util.Choice;
31 import ca.uqac.lif.synthia.util.Freeze;
32 import ca.uqac.lif.synthia.util.Mutate;
33 import ca.uqac.lif.synthia.util.Mutator;
34 import ca.uqac.lif.synthia.util.Offset;
35 import ca.uqac.lif.synthia.util.Replace;
36 import ca.uqac.lif.synthia.util.Tick;
37 
38 /**
39  * Illustration of mutation operations on lists and list elements. The program
40  * generates lists and then applies various {@link Mutator Mutators} on them.
41  * The wiring of pickers in this program is illustrated by the following
42  * diagram:
43  * <p>
44  * <img src="./doc-files/mutation/Mutators.png" alt="Picker wiring diagram" />
45  * <p>
46  * The diagram culminates into a {@link Mutate} picker (box A) that receives as
47  * one of its inputs an arbitrary list of numbers (not shown). The picker that
48  * provides a mutator for each list is a {@link Choice} (box B), which may pick
49  * with equal probability one of three possible mutators, represented by boxes
50  * 1-3. The first is an instance of {@link MutateElement}, whose mutator picker
51  * is another {@link Choice} (box a), selecting either an {@link Offset} (box
52  * b) or a {@link Replace} feeding the constant value 0 (box c). The other two
53  * list mutators are instances of {@link Swap} (box 2) and
54  * {@link DeleteElement} (box 3). All these pickers use a distinct
55  * {@link Random\-Float} as their source of randomness for making their
56  * respective choices. The end result is that, for every list given to the
57  * picker of box A, three equally probable outcomes are possible:
58  * <ol>
59  * <li>the first element is swapped with another one</li>
60  * <li>one element is deleted</li>
61  * <li>one element is chosen, and, with equal probability, is either offset by a random value in [0,1], or replaced with 0.</li>
62  * </ol>
63  * The program takes a single list and repeatedly passes it to {@link Mutate}.
64  * It produces the following output:
65  * <pre>
66  * [56.0, 55.0, 57.0]
67  * [55.0, 56.78632, 57.0]
68  * [56.0, 57.0]
69  * [56.0, 57.0]
70  * [55.0, 57.0]
71  * [55.135525, 56.0, 57.0]
72  * [55.93449, 56.0, 57.0]
73  * [56.0, 55.0, 57.0]
74  * [55.0, 56.0, 57.367645]
75  * [0, 56.0, 57.0]
76  * </pre>
77  * One can see how the original list [55, 56, 57] is modified in different
78  * ways:
79  * <ul>
80  * <li>The first two elements are swapped (lists 1, 8)</li>
81  * <li>The second element is offset from a random value (list 2)</li>
82  * <li>The first element is deleted (lists 3-4)</li>
83  * <li>The second element is deleted (list 5)</li>
84  * <li>The first element is offset from a random value (lists 6-7)</li>
85  * <li>The third element is offset from a random value (list 9)</li>
86  * <li>The first element is replaced by 0 (list 10)</li>
87  * </ul>
88  * <p>
89  * Note that although the diagram looks complex, the part of the program
90  * that builds the wiring of pickers only contains 13 lines.
91  *
92  * @ingroup Examples
93  */
94 public class MutatedLists
95 {
96  public static void main(String[] args)
97  {
98  /* A random float. For the sake of simplicity, we slightly diverge from the
99  * diagram and reuse the same RandomFloat instance everywhere it is needed. */
100  RandomFloat rf = new RandomFloat();
101 
102  /* A picker that will generate lists of integers whose values increment .*/
103  RandomInteger start = new RandomInteger(0, 100);
104  Tick t = new Tick(start, new Constant<Integer>(1));
105  ComposeList<Number> lists = new ComposeList<Number>(t, new RandomInteger(2, 10));
106 
107  /* A mutator for list elements that will choose between
108  * offsetting an element or replacing it with 0. */
110  e_choices.add(new Offset(null, rf), 0.5);
111  e_choices.add(new Replace<Number>(null, new Constant<Number>(0)), 0.5);
112  MutateElement<Number> me = new MutateElement<Number>(null, rf, e_choices);
113 
114  /* A mutator for lists that chooses between mutating an element,
115  * swapping two elements or deleting one. We use a Freeze picker so that the
116  * same list is mutated every time, to see the effect. */
118  l_choices.add(me, 0.33);
119  l_choices.add(new Swap<Number>(null, rf, rf), 0.33);
120  l_choices.add(new DeleteElement<Number>(lists, rf), 0.34);
121  Mutate<List<Number>> m = new Mutate<List<Number>>(new Freeze<List<Number>>(lists), l_choices);
122 
123  /* Iterate a few times over the picker. */
124  for (int i = 0; i < 10; i++)
125  {
126  System.out.println(m.pick());
127  }
128  }
129 }
ca.uqac.lif.synthia.util.Mutate
Transforms an object from a picker by selecting a mutator and applying it to an object.
Definition: Mutate.java:44
examples.mutation.MutatedLists.main
static void main(String[] args)
Definition: MutatedLists.java:96
ca.uqac.lif.synthia.util.Freeze
Picker that returns the first object fetched from another picker and repeats it endlessly.
Definition: Freeze.java:48
ca.uqac.lif.synthia.collection
Pickers generating and manipulating collections, such as lists and sets.
Definition: ComparableList.java:19
ca.uqac.lif.synthia.util.Mutate.pick
T pick()
Picks an object.
Definition: Mutate.java:58
ca.uqac.lif.synthia.util
Miscellaneous pickers performing various functions.
Definition: ArrayPicker.java:19
ca.uqac.lif.synthia.collection.MutateElement
Picker that selects an element of a list and applies the result of a mutator on this element.
Definition: MutateElement.java:35
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.util.Constant
Picker that returns the same object every time.
Definition: Constant.java:37
ca.uqac.lif.synthia.util.Replace
Definition: Replace.java:12
ca.uqac.lif
ca.uqac.lif.synthia.util.Choice.add
Choice< T > add(ProbabilityChoice< T > pc)
Adds an object-probability association.
Definition: Choice.java:72
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca.uqac.lif.synthia.util.Tick
Generates a sequence of monotonically increasing numerical values.
Definition: Tick.java:51
ca
examples.mutation.MutatedLists
Illustration of mutation operations on lists and list elements.
Definition: MutatedLists.java:94
ca.uqac.lif.synthia.util.Mutator
A picker that applies a transformation ("mutation") on the value produced by another picker.
Definition: Mutator.java:33
ca.uqac.lif.synthia.random.RandomFloat
Picks a floating point number uniformly in an interval.
Definition: RandomFloat.java:30
ca.uqac.lif.synthia.util.Offset
Takes the numerical value of a picker, and offsets it by an amount determined by another picker.
Definition: Offset.java:28
ca.uqac.lif.synthia.util.Choice
Picks an element from a collection, where the probability of picking each element can be user-defined...
Definition: Choice.java:44
ca.uqac.lif.synthia.collection.Swap
Mutator that receives a list and selects two elements to be swapped.
Definition: Swap.java:34
ca.uqac.lif.synthia.collection.ComposeList
Definition: ComposeList.java:44
ca.uqac.lif.synthia.collection.DeleteElement
Mutator that receives a list and selects an element to delete from it.
Definition: DeleteElement.java:33