예제 #1
0
void JoinedLandscape::genJoinedKeys( Landscape &LJoin )
{
	Keymap tempKeymap(LJoin.GetN(), itsK_two, 0); // create a keymap to add to the other
	unsigned int T_one = pow (2, itsK + 1);
	unsigned int T_two = pow (2, itsK_two );
	vector < vector < string > > tempKeys ( LJoin.GetN() , vector <string>() );
	unsigned int ii, jj, ll;
	for( ii = 0 ; ii < LJoin.GetN() ; ii++ )
	{
		for( jj = 0 ; jj < T_one ; jj++ )
		{
			for( ll = 0 ; ll < T_two ; ll++ )
			{
			// Generate 2^k_2 partial keys and add them to the end of the tempKeymap
			tempKeys[ii].push_back( LJoin.getKey( ii, jj) + tempKeymap.Get( ii, ll) );
			}
		}
   	}
	LJoin.ReplaceKeys(tempKeys);
}
예제 #2
0
/****************************** START OF JOIN ***************************/
JoinedLandscape::JoinedLandscape( Landscape & LOne, Landscape & LTwo, int k_input ) // relate two landscapes payoff structures so that decisions must be made with respect to both
/*    one approach would be to pass references to the two smaller landscapes then use the vectors to create a new one
 *    map < string, Payoff > d
 *    d is the final vector (i: 0 -> 2^n)
 *    vector< vector< Payoff > > payoffs --- a partial contribution for each possible configuration where payoffs[1][i] is the ith config for the 1th choice
 *    vector < vector < string > > key  ---  shows which set of choices matter for a given choice
 *    the best way to accomplish this might be to have one separate array for the cross-landscape choices that matter
 */
{
	cout << "Landscape initialized... working";
	n = LOne.GetN();
	k = k_input;
	string configuration, configuration2, numbuf;  // list of positions, 0 means not considered, 1 means not chosen, 2 means chosen
	int random_choice = 0, i = 0, j = 0, jj = 0, success = 0, MM = 0;
	int T = pow(2,(k+1));
	// buildStructure(T, numbuf, crosskey);
	// fillPartialPayoffs(T);
	// T = pow(2, n);
	// sumPayoffs(n, d, payoffs, T, numbuf);
	bool choices[20] = {0}; 
	string binaryConfig;
	int choices_counter = 0, L = 0, numbufLength = 0, decision = 0, X = 0, Z = 0, cc = 0, kk = 0, ii = 0, ll = 0;
	float randomTemp;
	while (decision < n ){  // i need to change this so that it creates two distinct sets of vectors
		cc = 0;
		while (cc < 21)
			{
			choices[cc] = 0;
			cc++;
			}
		ii = 0; kk = 0; L = 0; choices_counter = 0; X = 0; Z = 0;  numbufLength = 0; binaryConfig.clear(); numbuf.clear();
		while (choices_counter < k) // this loop selects k random decisions
			{
			random_choice = rand() % (n); // random integer from 0 to n-1 as index
			if (choices[random_choice] != 1)
				{
				choices[random_choice] = 1;
				binaryConfig.push_back('0');
				choices_counter++;
				}
			}
		// now that there is an array showing which choices matter, we need keys for each of the 2^(k+1) choices for those values
		key.resize(decision+1);
		key_two.resize(decision+1);
		buildJoined( LOne, k, decision, key, binaryConfig, choices );
		buildJoined( LTwo, k, decision, key_two, binaryConfig, choices );
		decision++;
	}
	fillPartialPayoffs( pow(2, (LOne.GetK()+k+1) ));
	i = 0; int mmm = 0;
/*	while (i < n){ // test loop
 *		mmm = 0;
 *		while (mmm < pow(2, LOne.k + k + 1)){
 *			cout << " " << payoffs[i][mmm].GetFitness() << " ";
 *			mmm++;
 *		}
 *		cout << endl;
 *		i++;
 *	}
 */
	i = 0;  // counter that represents each decision (d_i)
	success = 0;
	int divisor;
	T = pow(2, n);
	string temp, temp2;

/* need to replace with a function ****************** */
	i=0, jj=0, kk=0, ii=0, numbufLength=0, success=0;
	while (i < pow(2,(2*n)))
		{
		configuration.clear();
		ii=0;
		while ( ii < 2*n )
		{
			configuration.push_back('0');
			ii++;
		}
		numbuf.clear();
		binary(i, numbuf);
		numbufLength = numbuf.length();
		configuration.replace(((2*n)-numbufLength),numbufLength,numbuf);
		d.insert( pair<string,Payoff> (configuration, Payoff(configuration, 0)));
		jj=0;
		divisor = 0;
		while (jj < n)
			{
			kk=0;
			while (kk < pow(2, LOne.GetK() + k+ 1))  // this loop goes through each decision kk, goes to the payoff map, and adds up all the vectors with configs that match
				{
				temp = payoffs[jj][kk].GetConfig();
				temp2 = configuration;
				success = maskTest(temp, temp2, (2*n));
				if ( success ) {
					d[configuration].SetFitness( d[configuration].GetFitness() + payoffs[jj][kk].GetFitness() );
					divisor++;
					}
				kk++;
				}
			jj++;
			}
		// d[configuration]=payoffSum;
		d[configuration].SetFitness( d[configuration].GetFitness()/ (double) (n) ); // the number of vectors that matched should be equal to the total number (N)
		i++;
		}
/* here ends *************************** */
/* need to replace with a function ****************** */
	i=0, jj=0, kk=0, ii=0, numbufLength=0, success=0;
	while (i < pow(2,(2*n)))
		{
		configuration.clear();
		ii=0;
		while ( ii < 2*n )
		{
			configuration.push_back('0');
			ii++;
		}
		numbuf.clear();
		binary(i, numbuf);
		numbufLength = numbuf.length();
		configuration.replace(((2*n)-numbufLength),numbufLength,numbuf);
		d_two.insert( pair<string,Payoff> (configuration, Payoff(configuration, 0)));
		jj=0;
		divisor = 0;
		while (jj < n)
			{
			kk=0;
			while (kk < pow(2, LTwo.GetK() + k+ 1))  // this loop goes through each decision kk, goes to the payoff map, and adds up all the vectors with configs that match
				{
				temp = payoffs[jj][kk].GetConfig();
				temp2 = configuration;
				success = maskTest(temp, temp2, (2*n));
				if ( success ) {
					d_two[configuration].SetFitness( d_two[configuration].GetFitness() + payoffs_two[jj][kk].GetFitness() );
					divisor++;
					}
				kk++;
				}
			jj++;
			}
		// d[configuration]=payoffSum;
		d_two[configuration].SetFitness( d_two[configuration].GetFitness()/ (double) (n) );
		// the number of vectors that matched should be equal to the total number (N)
		i++;
		}
/* here ends *************************** */

	i=0; MM=0;
	int MM_two =0;
/*	map<string,Payoff>::iterator IT;
	for ( IT = d.begin() ; IT != d.end() ; IT++)
		{
		success = (*IT).second.CompareNeighbors(d, 2*n, (*IT).first, 0, n);
		if (!success) {
			MM++;
			}
		}
	itsNumPeaks = MM;
	i=0; MM_two=0;
	for ( IT = d_two.begin() ; IT != d_two.end() ; IT++)
		{
		success = (*IT).second.CompareNeighbors(d_two, 2*n, (*IT).first, 0, n);
		if (!success) {
			MM_two++;
			}
		}
	itsNumPeaks2 = MM_two;
*/	cout << " landscape complete!" << endl;
}
예제 #3
0
/****************************** START OF JOIN ***************************/
JoinedLandscape::JoinedLandscape( Landscape & LOne, Landscape & LTwo, int k_input ) // relate two landscapes payoff structures so that decisions must be made with respect to both
/*    one approach would be to pass references to the two smaller landscapes then use the vectors to create a new one
 *    vector < Payoff > d
 *    d is the final vector (i: 0 -> 2^n)
 *    vector< vector< Payoff > > payoffs --- a partial contribution for each possible configuration where payoffs[1][i] is the ith config for the 1th choice
 *    vector < vector < string > > key  ---  shows which set of choices matter for a given choice
 *    the best way to accomplish this might be to have one separate array for the cross-landscape choices that matter
 */
{
	n = LOne.GetN();
	k = k_input;
	string configuration, configuration2, numbuf;  // list of positions, 0 means not considered, 1 means not chosen, 2 means chosen
	int random_choice = 0, i = 0, j = 0, jj = 0, success = 0, MM = 0;
	int T = pow(2,(k+1));
	// buildStructure(T, numbuf, crosskey);
	// fillPartialPayoffs(T);
	// T = pow(2, n);
	// sumPayoffs(n, d, payoffs, T, numbuf);
	bool choices[13] = {0}; 
	string binaryConfig;
	int choices_counter = 0, L = 0, numbufLength = 0, decision = 0, X = 0, Z = 0, cc = 0, kk = 0, ii = 0, ll = 0;
	float randomTemp;
	while (decision < n ){  // i need to change this so that it creates two distinct sets of vectors
		cc = 0;
		while (cc < 14)
			{
			choices[cc] = 0;
			cc++;
			}
		ii = 0; kk = 0; L = 0; choices_counter = 0; X = 0; Z = 0;  numbufLength = 0; binaryConfig.clear(); numbuf.clear();
		while (choices_counter < k) // this loop selects k random decisions
			{
			random_choice = rand() % (n); // random integer from 0 to n-1 as index
			if (choices[random_choice] != 1)
				{
				choices[random_choice] = 1;
				binaryConfig.push_back('0');
				choices_counter++;
				}
			}
		// now that there is an array showing which choices matter, we need keys for each of the 2^(k+1) choices for those values
		key.resize(decision+1);
		key_two.resize(decision+1);
		buildJoined( LOne, k, decision, key, binaryConfig, choices );
		buildJoined( LTwo, k, decision, key_two, binaryConfig, choices );
		decision++;
	}
	fillPartialPayoffs( pow(2, (LOne.GetK()+k+1) ));
	i = 0; int mmm = 0;
/*	while (i < n){ // test loop
 *		mmm = 0;
 *		while (mmm < pow(2, LOne.k + k + 1)){
 *			cout << " " << payoffs[i][mmm].GetFitness() << " ";
 *			mmm++;
 *		}
 *		cout << endl;
 *		i++;
 *	}
 */
	i = 0;  // counter that represents each decision (d_i)
	success = 0;
	int divisor;
	T = pow(2, n);
	string temp, temp2;

/* need to replace with a function ****************** */
	i=0, jj=0, kk=0, ii=0, numbufLength=0, success=0;
	while (i < pow(2,(2*n)))
		{
		configuration.clear();
		ii=0;
		while ( ii < 2*n )
		{
			configuration.push_back('0');
			ii++;
		}
		numbuf.clear();
		binary(i, numbuf);
		numbufLength = numbuf.length();
		configuration.replace(((2*n)-numbufLength),numbufLength,numbuf);
		d.push_back(Payoff(configuration, 0));
		jj=0;
		divisor = 0;
		while (jj < n)
			{
			kk=0;
			while (kk < pow(2, LOne.GetK() + k+ 1))  // this loop goes through each decision kk, goes to the payoff map, and adds up all the vectors with configs that match
				{
				temp = payoffs[jj][kk].GetConfig();
				temp2 = configuration;
				success = maskTest(temp, temp2, (2*n));
				if ( success ) {
					d[i].SetFitness( d[i].GetFitness() + payoffs[jj][kk].GetFitness() );
					divisor++;
					}
				kk++;
				}
			jj++;
			}
		// d[configuration]=payoffSum;
		d[i].SetFitness( d[i].GetFitness()/ (double) (n) ); // the number of vectors that matched should be equal to the total number (N)
		i++;
		}
/* here ends *************************** */
/* need to replace with a function ****************** */
	i=0, jj=0, kk=0, ii=0, numbufLength=0, success=0;
	while (i < pow(2,(2*n)))
		{
		configuration.clear();
		ii=0;
		while ( ii < 2*n )
		{
			configuration.push_back('0');
			ii++;
		}
		numbuf.clear();
		binary(i, numbuf);
		numbufLength = numbuf.length();
		configuration.replace(((2*n)-numbufLength),numbufLength,numbuf);
		d_two.push_back(Payoff(configuration, 0));
		jj=0;
		divisor = 0;
		while (jj < n)
			{
			kk=0;
			while (kk < pow(2, LTwo.GetK() + k+ 1))  // this loop goes through each decision kk, goes to the payoff map, and adds up all the vectors with configs that match
				{
				temp = payoffs[jj][kk].GetConfig();
				temp2 = configuration;
				success = maskTest(temp, temp2, (2*n));
				if ( success ) {
					d_two[i].SetFitness( d_two[i].GetFitness() + payoffs_two[jj][kk].GetFitness() );
					divisor++;
					}
				kk++;
				}
			jj++;
			}
		// d[configuration]=payoffSum;
		d_two[i].SetFitness( d_two[i].GetFitness()/ (double) (n) ); // the number of vectors that matched should be equal to the total number (N)
		i++;
		}
/* here ends *************************** */
/*	while (i < pow(2,(2*n))) // demostrates that the mask isn't working to differentiate, and the sums aren't being totalled right
	{
		cout << d[i].GetConfig() << " " << d[i].GetFitness() << endl;
		i++;
 	}
 */	i=0; MM=0;
	Multicounter Counter;
	while (i < pow(2, (2*n)))
		{
/* now that we have key/value pairs, and each decision d_i has a value, in order to get the total for the decision configuration, i need to add up all the values for each decision. CRAP, what is stored in the key is the configuration that tells what the relevant other decisions are, but i still would need a vector of the actual decision possibilities (EDIT: this is now the d vector), then i need to use each of those possibilities to get a number from the key vector
 how to determine peaks? 	
*/
		success = d[i].CompareNeighbors(d, 2*n, i, 0, 2*n);
		if (!success) {
			cout << d[i].GetConfig() << ":" << d[i].GetFitness() << endl;
			MM++;
			}
		i++;
		}
	itsNumPeaks = MM;
}