Ejemplo n.º 1
0
void
generateBacktrack(IntVector& current_set, unsigned n)
{
	unsigned csum = std::accumulate(current_set.begin(), current_set.end(), 0);
	if (csum > n)
	{
		//discard this solution
		return;
	}
	
	
	if (csum == n)		//if solution, print it
	{
		processSolution(current_set);
	}
	else
	{
		IntVector candidates = getCandidates(current_set, n, csum);
		for (unsigned i =0; i < candidates.size(); ++i)
		{
			current_set.push_back(candidates.at(i));
			generateBacktrack(current_set, n);
			current_set.pop_back();
		}
	}
}
Ejemplo n.º 2
0
void DBaseBatchPlanner::writeSolutionsToFile(FILE *f)
{
	//hacking reaches new lows
	numOfGrasps = 0;
	FILE *temp = mResultFile;
	mResultFile = f;
	for (int i=0; i<mPlanner->getListSize();i++){
		processSolution(mPlanner->getGrasp(i));
	}
	mResultFile = temp;

}
Ejemplo n.º 3
0
void
generateBacktrack(IntVector& current_set, unsigned n)
{
	if (current_set.size() == n)		//if solution, print it
	{
		processSolution(current_set);
	}
	else
	{
		IntVector candidates;
		candidates.push_back(0);
		candidates.push_back(1);
		for (unsigned i =0; i < candidates.size(); ++i)
		{
			current_set.push_back(candidates.at(i));
			generateBacktrack(current_set, n);
			current_set.pop_back();
		}
	}
}
Ejemplo n.º 4
0
void
backtrackingIterative(unsigned n)
{
	IntVector current_set, temp;
	std::stack<IntVector> solution;
	
	solution.push(current_set);
	
	while (!solution.empty())
	{
		if (solution.top().size() == n)
		{
			//std::cout<<"Found a subset\n";
			processSolution(solution.top());
			solution.pop();
			
			if (solution.empty())
			{
				break;
			}
		}
		else
		{
			temp = solution.top();
			temp.push_back(0);
			solution.push(temp);
			//std::cout<<"Pushed 1: "; printVector(temp);
			continue;
		}
		
		temp = solution.top();
		solution.pop();
		temp.push_back(1);
		solution.push(temp);
		//std::cout<<"Pushed 2: "; printVector(temp);
	}
}
Ejemplo n.º 5
0
void DBaseBatchPlanner::plannerUpdate()
{
	static int lastSolution = 0;
	bool newSol = false;

	//let's print solutions as they are discovered, so we don't lose all if there's a crash
	for (int i=lastSolution; i<mPlanner->getListSize();i++){
		processSolution(mPlanner->getGrasp(i));
		newSol = true;
	}

	if (newSol) {
		//if we seg-fault we don't want to lose what we have already written
		fflush(mResultFile);
		if (mType == DEXTEROUS) {
			//we don't need already saved solutions
			mPlanner->clearSolutions();
			lastSolution = 0;
		} else if (mType == GRIPPER) {
			//in this case we do need them as they will be avoided by future searches
			lastSolution = mPlanner->getListSize();
		}
	}
}