Ejemplo n.º 1
0
//Test Polynomial class
void test_Polynomial()
{
	GAParams::SetNumVars( 8 );

	Monomial a("1001 0110" );
	Monomial b("1111 0000" );
	Monomial c("0000 0000" );

	// p(x) = 0;
	Polynomial p;
	p.AddTerm(c);
	std::cout << p.ToString( true ) << std::endl;
	std::cout << "  has max degree = " << (int) p.MaxDegree() << std::endl;

	// p(x) = a  = x1*x4*x6*x7
	p.AddTerm(a);
	// p(x) = a + b = x1*x4*x6*x7 + x1*x2*x3*x4
	p.AddTerm(b);
	// p(x) = a + b + c = x1*x4*x6*x7 + x1*x2*x3*x4 + 1

	std::cout << p.ToString( true ) << std::endl;
	std::cout << "  has max degree = " << (int) p.MaxDegree() << std::endl;

	NTuple d("1101 0110" );

	std::cout << p.Evaluate(d) << std::endl;
}
Ejemplo n.º 2
0
void test_poly_mutate()
{
	GAParams::SetNumVars( 8 );

	Monomial a("1000 0000" );
	Monomial c("0000 0000" );
	c.mCoeff = true;

	// p(x) = 1;
	Polynomial p;
	p.AddTerm(c);
	std::cout << p.ToString( true ) << std::endl;

	p.AddTerm(a);
	std::cout << p.ToString( true ) << std::endl;

	// Attempt to add a duplicate term
	// a.Reset(1);
	// p.AddTerm(a);

	PolyIter p_iter = p.mPoly.find( c );
	if( p_iter != p.mPoly.end( ) )
	{
		p.mPoly.erase( p_iter );
	}

	std::cout << p.ToString( true ) << std::endl;
    
}
		void Verify(string input_p1, string input_p2, Polynomial& expectedOutput)
		{
			Polynomial& p1 = parser.ConvertToPolynomial(input_p1);
			Polynomial& p2 = parser.ConvertToPolynomial(input_p2);
			result = p1 - p2;

			Assert::AreEqual(expectedOutput.ToString(), result.ToString());
		}
Ejemplo n.º 4
0
void test_rand_poly()
{
	// Establish the problems size
	GAParams::SetNumVars( 4 );

	// Establish the maximum support for any monomial
	Monomial::SetMaxSupport( 3 );

	size_t max_terms = 8;
	Polynomial p;

	int i;
	for( i = 25; i > 0; --i)
	{
		p.GenerateRandomPolynomial( max_terms );

		std::cout << "p = " << p.ToString( true ) << std::endl;
	}

}
Ejemplo n.º 5
0
// Test the DynSysModel class
void test_DynSysModel()
{
	// Make a 4 variable model
	GAParams::SetNumVars( 4 );

	DynSysModel m;

	// Create monomial x1, x2, x3, x4, x1*x2, x3*x4, x1*x2*x3*x4

	Monomial c0(   "0000" );	c0.mCoeff = false;
	Monomial c1(   "0000" );	c1.mCoeff = true;
	Monomial x4(   "0001" );
	Monomial x3(   "0010" );
	Monomial x2(   "0100" );
	Monomial x1(   "1000" );
	Monomial x34(  "0011" );
	Monomial x12(  "1100" );
	Monomial x14(  "1001" );
	Monomial x1234("1111" );

	Polynomial f;
	f.AddTerm( c0 );
	std::cout << "f = " << f.ToString( true ) << std::endl;

	Polynomial g;
	g.AddTerm( c1 );
	std::cout << "g = " << g.ToString( true ) << std::endl;

	// f1(x) = x1 + x2 + x4;
	Polynomial f1;
	f1.AddTerm( x1 );	f1.AddTerm( x2 );	f1.AddTerm( x4 );
	std::cout << "f1 = " << f1.ToString( true ) << std::endl;

	Polynomial::mMaxSupport = 2;
	m.SetFunction( 1, f );
	m.SetFunction( 2, g );
	m.SetFunction( 3, f );
	m.SetFunction( 4, f1 );

	ComplexityMatrix cmplx_mat;
	ComplexityMatrixRow row;
	cmplx_mat.assign( 4, row );

	m.SetPolyComplexities( );
	double s;
	s = m[1].mComplexityScore;
	s = m[2].mComplexityScore;
	s = m[3].mComplexityScore;
	s = m[4].mComplexityScore;


/*
	// f2(x) = x1*x2*x3*x4;
	Polynomial f2;
	f2.AddTerm( x1234 );
	std::cout << "f2 = " << f2.ToString( true ) << std::endl;

	// f3(x) = x1*x2 + x3*x4;
	Polynomial f3;
	f3.AddTerm( x12 );
	f3.AddTerm( x34 );
	std::cout << "f3 = " << f3.ToString( true ) << std::endl;

	// f3'(x) = x1*x4;
	Polynomial f3p;
	f3p.AddTerm( x14 );
	std::cout << "f3p = " << f3p.ToString( true ) << std::endl;

	// f4(x) = x3 + x4;
	Polynomial f4;
	f4.AddTerm( x3 );	f4.AddTerm( x4 );
	std::cout << "f4 = " << f4.ToString( true ) << std::endl;

	// Assign the functions to the model
	m.SetFunction( 1, f1 ); 	m.SetFunction( 2, f2 );
	m.SetFunction( 3, f3p ); 	m.SetFunction( 4, f4 );
*/

	TimeSeries t2;
//	t2.push_back( NTuple("1101" ) );
//	t2.push_back( NTuple("1011" ) );
	t2.push_back( NTuple("1100" ) );
	t2.push_back( NTuple("0010" ) );
	t2.push_back( NTuple("0001" ) );
	t2.push_back( NTuple("1001" ) );
	t2.push_back( NTuple("0001" ) );

	// Better to pass a reference to the time series for the result
	// TimeSeries t3 = m.Iterate( NTuple( "1111" ), 6 );

	// Test iteration for only the k'th variable
	size_t k = 3;
	TimeSeries t4;
	size_t h = m.Iterate( t2, k, t4 );

	TimeSeriesIter iter = t4.begin();
	while( iter != t4.end() )
	{
		std::cout << *iter++ << std::endl;
	}
}