File: RandomExpressionFactory.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 (169 lines) | stat: -rw-r--r-- 4,143 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
/*
 * Modification History 
 *
 * 2001-August-30  	Jason Rohrer
 * Created.
 *
 * 2001-August-31  	Jason Rohrer
 * Finished implementation.
 * Fixed a compile bug.
 *
 * 2001-September-4   Jason Rohrer
 * Added support for FixedConstantExpressions.   
 */
 
 
#ifndef RANDOM_EXPRESSION_FACTORY_INCLUDED
#define RANDOM_EXPRESSION_FACTORY_INCLUDED 
 
#include "Expression.h"

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


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


/**
 * Utility class for constructing random expressions.
 *
 * @author Jason Rohrer 
 */
class RandomExpressionFactory { 
	
	public:


		
		/**
		 * Constructs a random expression factory.
		 *
		 * @param inRandSource the source for random numbers
		 *   to use while constructing expressions.
		 *   Must be destroyed by caller after this class is destroyed.
		 */
		RandomExpressionFactory( RandomSource *inRandSource );


		
		/**
		 * Recursively constructs a random expression with all parameters
		 * filled.
		 *
		 * @param inProbOfStopping the probability of stopping at each
		 *   branch of the expression.  Upon stopping on a certain branch
		 *   of the recursion, it simply returns a constant expression.
		 * @param inProbOfFixedConstant the probability of a fixed
		 *   constant expression being inserted upon stopping (as opposed
		 *   to a (mutable) constant expression).
		 * @param inMaxDepth if this is reached, the recursion
		 *   stops regardless of inProbOfStopping.
		 * @param inConstantMax the maximum value for a constant expression.
		 *
		 * @return the constructed expression.
		 */
		Expression *constructRandomExpression(
			double inProbOfStopping, double inProbOfFixedConstant,
			int inMaxDepth, double inConstantMax );


		
	protected:

		RandomSource *mRandSource;
				
	};



inline RandomExpressionFactory::RandomExpressionFactory(
	RandomSource *inRandSource )
	: mRandSource( inRandSource ) {

	}



inline Expression *RandomExpressionFactory::constructRandomExpression( 
	double inProbOfStopping,
	double inProbOfFixedConstant,
	int inMaxDepth, double inConstantMax ) {
	
	// fill in constant expressions only at the leaves
	if( inMaxDepth == 0 || 
		mRandSource->getRandomDouble() <= inProbOfStopping ) {
		// stop
		if( mRandSource->getRandomDouble() <= inProbOfFixedConstant ) {
			return new FixedConstantExpression( 
				inConstantMax * ( mRandSource->getRandomDouble() ) );
			}
		else {
			return new ConstantExpression( 
				inConstantMax * ( mRandSource->getRandomDouble() ) );
			}
		}
	else {
		// keep filling in non constant expressions randomly
		
		// FILL IN HERE
		
		// we have 6 expression types
		int randVal = mRandSource->getRandomBoundedInt( 0, 3 );
		
		Expression *outExpression;

		// pick an expression type
		
		switch( randVal ) {
			case 0:
				outExpression = new NegateExpression( NULL );
				break;
			case 1:
				outExpression = new ProductExpression( NULL, NULL );
				break;
			case 2:
				outExpression = new SinExpression( NULL );
				break;
			case 3:
				outExpression = new SumExpression( NULL, NULL );
				break;				
			default:
				// should never happen, but...
				printf( "RandomExpressionFactory: " );
				printf( "Error while generating random expression\n" );
				
				// default to a constant expression of 0
				outExpression = new ConstantExpression( 0 );
				break;
			}

		// now recursively fill in the arguments in succession
		for( int i=0; i<outExpression->getNumArguments(); i++ ) {
			
			// create a random expression as the argument
			// note that we decrement inMaxDepth here
			Expression *argument = constructRandomExpression( 
				inProbOfStopping, inProbOfFixedConstant,
				inMaxDepth - 1, inConstantMax );

			// set the argument into our expression	
			outExpression->setArgument( i, argument );
			}	
		
		// now expression is complete.
		return outExpression;
		
		}	// end of non-constant else case
	
	}	// end of constructRandomExpression()



#endif