File: ExpressionMutator.h

package info (click to toggle)
primrose 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,300 kB
  • ctags: 3,385
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 95; perl: 67
file content (263 lines) | stat: -rw-r--r-- 7,201 bytes parent folder | download | duplicates (18)
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
/*
 * Modification History 
 *
 * 2001-September-9   Jason Rohrer
 * Created.
 *
 * 2001-September-13   Jason Rohrer
 * Made an error message more verbose.  
 */
 
 
#ifndef EXPRESSION_MUTATOR_INCLUDED
#define EXPRESSION_MUTATOR_INCLUDED 

#include "minorGems/util/random/RandomSource.h"

#include "Expression.h" 

#include "ConstantExpression.h"
#include "InvertExpression.h"
#include "ProductExpression.h"
#include "PowerExpression.h"
#include "SumExpression.h"
#include "NegateExpression.h"
#include "SinExpression.h"
#include "FixedConstantExpression.h"
#include "MultiConstantArgumentExpression.h"


 
/**
 * Utility class for mutating expressions. 
 *
 * Note that the number and position of constant expressions
 * within the expression will not change during mutation.  Thus,
 * the structure of the expression remains constant while the operations
 * being performed at each node are mutated.
 *
 * @author Jason Rohrer 
 */
class ExpressionMutator { 
	
	public:



		/**
		 * Mutates an expression.
		 *
		 * Note that this function's implementation is recursive.
		 *
		 * @param inExpression the expression to mutate.  Will be destroyed
		 *   by this function (or if not destroyed, then passed back through
		 *   the return value).  Only the return value, not inExpression,
		 *   can be accessed safely after this function returns.
		 * @param inMutationProb the probability of mutations at each
		 *   node in inExpression.
		 * @param inMaxMutation the fraction of a fixed constant's value
		 *   by which it can be mutated.  For example, if a fixed constant
		 *   has the value 100, and inMaxMutation is 0.5, then the mutated
		 *   constant will be a random value in the range [50, 150].
		 * @param inRandSource the source of random numbers to use.
		 *   Must be destroyed by caller.
		 *
		 * @return a new expression that is a mutated version of inExpression.
		 *   (Note that the returned expression may be inExpression).
		 */
		static Expression *mutateExpression( Expression *inExpression,
											 double inMutationProb,
											 double inMaxMutation,
											 RandomSource *inRandSource );


		
	};



inline Expression *ExpressionMutator::mutateExpression(
	Expression *inExpression,
	double inMutationProb,
	double inMaxMutation,
	RandomSource *inRandSource ) {
		
	long expressionID = inExpression->getID();
	
	// first, deal with constant case
	if( expressionID == ConstantExpression::staticGetID() ) {
		// mutate the constant's value
		ConstantExpression *c = (ConstantExpression *)inExpression;
		double value = c->getValue();

		double maxMutationAmount = inMaxMutation * value;

		// a value in [-1, 1]
		double mutator = ( inRandSource->getRandomDouble() * 2.0 ) - 1.0;

		value = value + mutator * maxMutationAmount;

		c->setValue( value );
		
		return c;
		}
	else if( expressionID == FixedConstantExpression::staticGetID() ) {
		// mutate the fixed constant's value
		FixedConstantExpression *c = (FixedConstantExpression *)inExpression;
		double value = c->getValue();

		double maxMutationAmount = inMaxMutation * value;

		// a value in [-1, 1]
		double mutator = ( inRandSource->getRandomDouble() * 2.0 ) - 1.0;

		value = value + mutator * maxMutationAmount;

		delete c;

		return new FixedConstantExpression( value );
		}
	// next deal with the multi-argument case
	else if( expressionID == MultiConstantArgumentExpression::staticGetID() ) {
		MultiConstantArgumentExpression *m =
			(MultiConstantArgumentExpression*)inExpression;

		Expression *wrappedExpression =
			m->extractWrappedExpression();


		wrappedExpression = mutateExpression( wrappedExpression,
											  inMutationProb,
											  inMaxMutation,
											  inRandSource );

		m->setWrappedExpression( wrappedExpression );

		return m;
		}
	else {
		// the general expression case
		Expression *newExpression = inExpression;
		
		if( inRandSource->getRandomDouble() <= inMutationProb ) {
			// we need to mutate this node

			long newID;
			if( expressionID == InvertExpression::staticGetID()
				|| expressionID == NegateExpression::staticGetID()
				|| expressionID == SinExpression::staticGetID() ) {
				// a unary expression

				// pick another expression type at random
				newID = expressionID;
				while( newID == expressionID ) {
					int randValue = inRandSource->getRandomBoundedInt( 0, 2 );
					switch( randValue ) {
						case 0:
							newID = InvertExpression::staticGetID();
							break;
						case 1:
							newID = NegateExpression::staticGetID();
							break;
						case 2:
							newID = SinExpression::staticGetID();
							break;
						default:
							// we should never hit this
							// note that this newID will cause a printed
							// error below
							newID = -1;
							break;
						}
					}
				}
			else {
				// a binary expression

				// pick another expression type at random
				newID = expressionID;
				while( newID == expressionID ) {
					int randValue = inRandSource->getRandomBoundedInt( 0, 2 );
					switch( randValue ) {
						case 0:
							newID = PowerExpression::staticGetID();
							break;
						case 1:
							newID = ProductExpression::staticGetID();
							break;
						case 2:
							newID = SumExpression::staticGetID();
							break;
						default:
							// we should never hit this
							// note that this newID will cause a printed
							// error below
							newID = -1;
							break;
						}
					}
				}
			
			// now build an expression based on newID
			if( newID == InvertExpression::staticGetID() ) {
				newExpression = new InvertExpression( NULL );
				}
			else if( newID == NegateExpression::staticGetID() ) {
				newExpression = new NegateExpression( NULL );
				}
			else if( newID == PowerExpression::staticGetID() ) {
				newExpression = new PowerExpression( NULL, NULL );
				}
			else if( newID == ProductExpression::staticGetID() ) {
				newExpression = new ProductExpression( NULL, NULL );
				}
			else if( newID == SinExpression::staticGetID() ) {
				newExpression = new SinExpression( NULL );
				}
			else if( newID == SumExpression::staticGetID() ) {
				newExpression = new SumExpression( NULL, NULL );
				}
			else {
				printf( "Error in ExpressionMutator:  ID does not " );
				printf( "match a known expression type: %d\n", newID );
				}


			// at this point, we have constructed a new expression node
			// with the same number of arguments as the old  node

			// we need to fill in the arguments from inExpression
			
			for( int i=0; i<inExpression->getNumArguments(); i++ ) {
				// use extractArgument so that the argument
				// won't be destroyed when inExpression is destroyed
				Expression *argument = inExpression->extractArgument( i );
				
				newExpression->setArgument( i, argument );
				}

			// destroy the old node
			delete inExpression;
			}

		// mutate the sub nodes
		for( int i=0; i<newExpression->getNumArguments(); i++ ) {
			Expression *argument = newExpression->extractArgument( i );
			
			// mutate the argument
			argument = mutateExpression( argument,
										 inMutationProb,
										 inMaxMutation,
										 inRandSource );
			
			newExpression->setArgument( i, argument );
			}

		return newExpression;
		}

	}


	
#endif