Пример #1
0
VImage 
VImage::new_from_image( std::vector<double> pixel )
{
	VImage onepx = VImage::black( 1, 1, 
		VImage::option()->set( "bands", bands() ) ); 

	onepx = onepx.linear( to_vectorv( 1, 1.0 ), pixel ).cast( format() );

	VImage big = onepx.embed( 0, 0, width(), height(), 
		VImage::option()->set( "extend", VIPS_EXTEND_COPY ) ); 

	big = big.copy( 
		VImage::option()->
			set( "interpretation", interpretation() )->
			set( "xres", xres() )->
			set( "yres", yres() )->
			set( "xoffset", xres() )->
			set( "yoffset", yres() ) ); 

	return( big ); 
}
Пример #2
0
/**
 * Here we evaluate the fitness of an individual.
 */
double CNFFitnessEvaluator::evaluate( void *individual )
{
    // We store the last evaluated individual in a
    // cache.  This way, if the new individual is a copy
    // of the last one (wich usually happens after a number
    // of generations, when the population converges), we will
    // save a lot of time by not having to re-evaluate everything.
    // Here we are checking wether this individual is equal to
    // the last one.
	if ( previous != NULL )
	{
	    // Copy the individual to a buffer.
		memcpy( current, individual, arraySize );
		// zero-out the remaining bits of the last byte, just to be sure that 
		// the unused bits won't make the memcmp call fail
		int lastBits = stringSize % INT_BITSIZE;
		if ( lastBits > 0 )
		{
			int pattern = ( 1 << lastBits ) - 1;
			current[arraySize - 1] &= pattern;
		}

        // Compare the current individual with the last one in the cache
        // If they are the same we return the stored evaluation.
		if ( memcmp( current, previous, arraySize ) == 0 )
		{		    
			// cout << "Cache hit!" << endl;
			return lastEval;
		}
	}
	else
	{
		current = new int[arraySize];
		previous = new int[arraySize];
	}

	
    // Copy the current individual to the cache.
    // We zero-out the remaining bits of the last byte, just to be sure
    // that the unused bits won't make the comparision fail.
	memcpy( previous, individual, arraySize );
	int lastBits = stringSize % INT_BITSIZE;
	if ( lastBits > 0 )
	{
		int pattern = ( 1 << lastBits ) - 1;
		current[arraySize - 1] &= pattern;
	}

    // Now let's evaluate this individual.  We need an interpretation
    // object that will wrap the actual bits.
	Interpretation interpretation( stringSize );
	interpretation.assign( individual, stringSize );
	
	// Get the number of satisfied clauses.
	int nSatisfied = 0;
	for ( int i = 0; i < formula.getNumClauses(); i++ )
	{
		if ( formula.getClauseValue( i, interpretation ) == true )
		{
			nSatisfied++;
		}
	}

    // If this individual has all clauses satisfied, we set a flag
    // indicating that it is the result.
	if ( nSatisfied == formula.getNumClauses() )
	{
		bLastIsResult = true;
	}
	else
	{
		bLastIsResult = false;
	}
	
	// Save the number of satisfied clauses in the cache.
	lastEval = nSatisfied;
	
	// The number of satisfied clauses is this individual's fitness.
	double fitness = nSatisfied;
	return fitness;
}
Пример #3
0
int testDualEncoding( int argc, char **argv )
{
	Random::s_randomize();
	CNFFormula formula;
	SATLIBParser parser;

	bool res = parser.open( "tests/satlib2.txt" );
	if ( res == false )
	{
		cout << "Mal, sap�o" << endl;
		return 1;
	}

	formula = parser.getFormula();
	DualEncodingSAT2CSP *enc = DualEncodingSAT2CSP::create();

	enc->setFormula( &formula );

	CSPProblem *problem = enc->getProblem();

	int numVars = problem->getNumVars();
	int numConstraints = problem->getConstraintList()->getNumConstraints();

	cout << "Problem has " << numVars << " variables, " << " and " << numConstraints << " constraints." << endl;

	Interpretation interpretation( formula.getNumVars() );
	CSPInterpretation cspInterpretation( problem->getNumVars() );

	AntSolver solver;

	solver.setAlpha( 2 );
	solver.setBeta( 10 );
	solver.setMaxPheromone( 10 );
	solver.setMinPheromone( 1 );
	solver.setNumAnts( 8 );
	solver.setProblem( problem );
	solver.setRo( 0.99 );

	res = solver.solve( 100 );
	cout << res << endl;

	if ( res == false )
	{
		cout << "Unable to solve with ant solver!!!" << endl;
		return 1;
	}

	cout << "Solved with ant solver" << endl;

	cspInterpretation = solver.getResult();

	int i;
	for ( i = 0; i < problem->getNumVars(); i++ )
	{
		int var1value = cspInterpretation.getVariableValue( i );
		BasicCSPVariableValue* value1 = (BasicCSPVariableValue *)problem->getDomainList()->getDomain( i )->getValue( var1value );

		cout << "Var " << i << " = " << value1->getValue() << endl;
	}

	res = enc->getSATInterpretation( cspInterpretation, interpretation );
	if ( res == false )
	{
		cout << "Unable to retrieve the SAT interpretaiton" << endl;
		return 2;
	}

	if ( formula.getNumSatisfiedClauses( interpretation ) != formula.getNumClauses() )
	{
		cout << "Formula not satisfied!" << endl;
		return 3;
	}

	return 0;
}