Exemplo n.º 1
0
PolynomialCurve<T>::
PolynomialCurve(const typename FittingCurve<T>::Points& points,
                const size_t order)
    : m_coefficients(order + 1)
{
    findSolution(points);
}
Exemplo n.º 2
0
void
PolynomialCurve<T>::
setPointsAndOrder(const typename FittingCurve<T>::Points& points,
                  const size_t order)
{
    m_coefficients.resize(order + 1);
    findSolution(points);
}
Exemplo n.º 3
0
    int Solver::solveProblemIteration()
    {
        if (currentProblem == 0)
            throw std::domain_error("Select a problem and call startSolveProblem() first.");

        findSolution();

        return swarm.getBestValue();
    }
Exemplo n.º 4
0
Rpn::Operand Derivative::calculate(FunctionCalculator *calculator, QList<Rpn::Operand> actualArguments)
{
	m_calculator = calculator;
	m_functionName = actualArguments.at(0).value.value<QString>();
	Number point = actualArguments.at(1).value.value<Number>();

	Rpn::Operand result;
	result.type = Rpn::OperandNumber;
	result.value = QVariant::fromValue(findSolution(point));
	return result;
}
Exemplo n.º 5
0
int main(int argc,char*argv[])
{
	if(argc<2){
		printf("need input file");
		return 1;
	}
	sodokuFileName=argv[1];
	printf("welcome to sokoku reslover\n");

	findSolution();
	
	return 0;
}
Exemplo n.º 6
0
int main(){
    int i = 1;
    //randomPuzzle();

    while(1){
        if(findSolution('0',0,i++))break;
    }
    i=0;
    while(sol[i] != '\0')
        printf("%c",sol[i++]);
    printf("\n");
    printPuzzle();
    
    return 0;
}
Exemplo n.º 7
0
/* recursively determine solution */
static int findSolution(Queenscreen *qs, int row, int col) 
{
  if(row == qs->BOARDSIZE)
    return 1;
  
  while(col < qs->BOARDSIZE) {
    if(!conflicts(qs, row, col)) {
      qs->board[row][col] = 1;

      if(findSolution(qs, row+1, 0))
        return 1;

      qs->board[row][col] = 0;
    }

    ++col;
  }

  return 0;
}
Exemplo n.º 8
0
    void solveSudoku(vector<vector<char>>& board) {
    	memset(row, false, sizeof(row));
    	memset(col, false, sizeof(col));
    	memset(blk, false, sizeof(blk));

    	for(int i = 0; i < 9; i++)
    		for (int j = 0; j < 9; j++)
    		{
    			if(board[i][j] != '.'){
    				int val = board[i][j] - '0';
    				row[i][val] = true;
    				col[j][val] = true;
    				int t = (i/3) * 3 + (j/3);
    				blk[t][val] = true;
    			}
    		} 

    	cout << findSolution(board) <<endl;

    	return;   
    }
Exemplo n.º 9
0
    bool findSolution(vector<vector<char>> & board){
    	int r = -1;
    	int c = -1;
    	for(int i = 0; i < 9; i++){
    		for(int j = 0; j < 9; j++){
    			if(board[i][j] == '.'){
    				r = i;
    				c = j;
    				// cout << i << j<< endl;
    				break;
    			}
    		}
    		if(r != -1 && c != -1)
    			break;
    	}
    	if(r == -1 && c == -1){
    		return true;
    	}

    	int t = (r/3) * 3 + (c/3);

    	for(int val = 1; val <= 9; val++){
    		if(!row[r][val] && !col[c][val] && !blk[t][val]){
    			row[r][val] = true;
    			col[c][val] = true;
    			blk[t][val] = true;
    			board[r][c] = '0' + val;
    			if(findSolution(board));
    				return true;
    			board[r][c] = '.';
    			row[r][val] = false;
    			col[c][val] = false;
    			blk[t][val] = false;

     		}
    	}

    	return false;
    }
Exemplo n.º 10
0
    void Solver::solveProblem(std::shared_ptr<KnapsackProblem> problem)
    {
        currentProblem = problem;

        std::cout << "==============================\n";
        std::cout << "Solving problem: " << std::endl;
        std::cout << "Nr. of elements: " << currentProblem->n << std::endl;
        std::cout << "Nr. of constraints: " << currentProblem->m << std::endl;
        std::cout << "==============================\n";

        swarm.initializeSwarm(parameters.getNumberOfParticles());
        initializeParticles();

        for (int i = 0; i < parameters.getIterations(); ++i)
            findSolution();

        int finalSolution = stopSolveProblem();

        std::cout << "==============================\n";
        std::cout << "Solution value: " << finalSolution << std::endl;
        std::cout << "==============================\n";
    }
Exemplo n.º 11
0
int findSolution(char prev,int solind,int limit){
    if(isFinalStates()){
        sol[solind] = '\0';
        return 1;
    }
    else if(solind < limit){
        int i;
        for(i=0;i<4;i++){
            if(direction[i] != findOppositeDir(prev)){
                if(mvblank(direction[i])){
                    sol[solind] = direction[i];
                    if(findSolution(sol[solind],solind+1,limit)){
                         mvblank(findOppositeDir(direction[i]));
                         return 1;
                    }
                    mvblank(findOppositeDir(direction[i]));
                }
            }
            
        }
    }

    return 0;
}
Exemplo n.º 12
0
/** driver for finding solution */
static void go(Queenscreen *qs) { while(!findSolution(qs, 0, random()%qs->BOARDSIZE)); }
Exemplo n.º 13
0
void
PolynomialCurve<T>::
setPoints(const typename FittingCurve<T>::Points& points)
{
    findSolution(points);
}
int main(void) {
  const unsigned n = 5;
  findSolution(n);
  print(n);
  return 0;
}
Exemplo n.º 15
0
/**
 * Given a set of values, times how long it takes to find if a specified target
 * value can be made by summing a subset of the given set of values.
 * 
 * Command line args:
 * argv[1] - a file of numbers that are in our set of values. The first line of
 *           the file is the number of values in the file.
 * argv[2] - a file of 30 numbers that are the targets that we try to reach
 *           with a subset of the values in argv[1]
 * 
 * Compile: gcc proj1.c -fopenmp -o proj1
 * Run: ./proj1 set_I_filename targets_filename
 *
 * Authors: Gaurav Luthria <*****@*****.**>
 *          Rajan Patel <*****@*****.**>
 *          Michael Bishoff <*****@*****.**>
 */
int main(int argc, char* argv[]) {
  // Check if command line args are valid
  if (argc != 3) {
    printf("ERROR: Command line arguments are <set_I_filename> ");
    printf("<targets_filename>\n");
    return 1;
  }
  
  // Initializes I, the set of values, and the list of targets that we use
  // to see if a subset of I sums to the value of a target
  int* I = readSet(argv[1]);
  int* targets = readTargets(argv[2]);
  
  // Determines the number of columns needed in the Q table
  numCols = getNumColumns(I);

  // Allocates space for the Q table
  int** Q = malloc(sizeof(*Q)*numRows);
  for (int i = 0; i < numRows; i++) {
    Q[i] = malloc(sizeof(*(Q[i]))*numCols);
  }
  
  double totalTime = 0;

  for (int i = 0; i < NUM_TARGETS; i++) {
    clearQMatrix(Q);
    
    double startTime = omp_get_wtime();
    //clock_t startTime = clock();
    int solutionExists = findSolution(I, Q, targets[i]);
    double endTime = omp_get_wtime();
    //clock_t endTime = clock();
    
    totalTime += (double) (endTime - startTime);
    
    // Uncomment this to see the reconstructed solution!
    /*
    printf("%d: ", targets[i]);
    // If there's a solution, print it
    if (solutionExists) {
      // Uncomment this to see the Q table!
      // printPretty(I, Q);
      reconstructSolution(I, Q, targets[i]);
      
    } else {
      printf("NO SOUTION EXISTS! ");
    }
    */
  }

  printf("Average Time: %f\n", (totalTime / NUM_TARGETS));
  printf("Total Time: %f\n", totalTime);
  
  
  // Freeeee
  free(I);

  for (int i = 0; i < numRows; i++) {
    free(Q[i]);
  }
  free(Q);
  
  return 0;
}
size_t CRowSolution<T>::moveNoncanonicalSolutions(const T *pSolution, size_t startIndex, CSolutionStorage *pSolutionStorage, size_t *pSolIdx)
{
	// For improved solution find the canonical solution:
	CSolutionPerm *pSolPerm = solutionPerm();
	uchar *pCanonFlags = pSolPerm->canonFlags();
	PERMUT_ELEMENT_TYPE *pPerm = pSolPerm->GetData();
    size_t len, nextSolutionIdx, lastCanonIdx = SIZE_MAX;
	size_t from;
	bool found = false;
	size_t i, curCanon = solutionIndex();
	const bool firstCall = !pSolutionStorage || pSolutionStorage->empty();
	if (!firstCall) {
		// Current solution was moved
		// Since we keep solutionIndex() as the previous index, we need to use "++curCanon" here
		from = findStartingIndex(++curCanon, pCanonFlags);
		i = findSolution(pSolution, from, startIndex, pSolPerm, lastCanonIdx, &nextSolutionIdx);
		found = i < startIndex;
		if (!found) { 
			// Solution was not found BEFORE the new interval of the current solution group
			// Let's try to find it AFTER this interval
			if (!lenOrbitOfSolution()) {
				// The length of the current solution group was not defined yet
				// When we are here, it could happend only when this group was merged with the next one
				// during previous call of current function
				curCanon--;
				setLenOrbitOfSolution(curCanon - findStartingIndex(curCanon, pCanonFlags));
				
				// In that case startIndex is not define. At this point startIndex should be 0 and
				//  solutionIndex() was not changed when we was here for thr first time
				*(pCanonFlags + curCanon) = 0;
				startIndex = curCanon - lenOrbitOfSolution() + 1;
			} else
				curCanon = startIndex + lenOrbitOfSolution() - 1;

			lastCanonIdx = SIZE_MAX;
		}
	}

	if (!found) {
		// We did not found the improved solution
		i = findSolution(pSolution, curCanon, numSolutions(), pSolPerm, lastCanonIdx, &nextSolutionIdx);
	}

	if (i < numSolutions() && pCanonFlags[i] != 0xff) {
		if (pSolutionStorage)
			pSolutionStorage->push_back(firstSolution() + pSolPerm->GetAt(i) * solutionSize());

		// We found solution and it was not marked as deleted
		bool changeSolutionIndex;
		if (!(changeSolutionIndex = firstCall)) {
			if (found) {
				// The whole orbit needs to be moved after startIndex + lenOrbitOfSolution position
				if (lastCanonIdx == SIZE_MAX) {
					// The orbit to be moved is the current one
					changeSolutionIndex = true;
					nextSolutionIdx = findCanonIndex(curCanon + 1, pCanonFlags);
				}
				else {
					// Going forward till next canonical solution
					curCanon = findCanonIndex(i, pCanonFlags);
				}

				lastCanonIdx = startIndex + lenOrbitOfSolution() - 1;
			}
			else {
				if (lastCanonIdx == SIZE_MAX)   // startIndex already in that orbit
					return startIndex;            // nothing to do

				curCanon = findCanonIndex(startIndex + lenOrbitOfSolution(), pCanonFlags);
				if (lastCanonIdx == curCanon) {
					// Two consequitive orbits need to be merged
#if PRINT_SOLUTIONS
					*(pCanonFlags + curCanon) = 0;
#endif
					return startIndex;
				}
			}
		}

		// Move all isomorphic solutions of the current solution into the new group:
		if (lastCanonIdx != SIZE_MAX) {
			// Find index of the first solution from current solution group
            from = findStartingIndex(curCanon, pCanonFlags);
			len = curCanon - from++;

			PERMUT_ELEMENT_TYPE buffer[256], *pTmp = buffer;
			if (len > countof(buffer))
				pTmp = new PERMUT_ELEMENT_TYPE[len];

			startIndex = lastCanonIdx - len + 1;
			lastCanonIdx -= curCanon++;

			// Moving lastCanonIdx elements starting from curCanon
			// Move the permutation's part
			memcpy(pTmp, pPerm + from, len * sizeof(pPerm[0]));
			memcpy(pPerm + from, pPerm + curCanon, lastCanonIdx * sizeof(pPerm[0]));
			memcpy(pPerm + startIndex, pTmp, len * sizeof(pPerm[0]));
			
			// Move the canonical flags's part
			memcpy(pCanonFlags + from, pCanonFlags + curCanon, lastCanonIdx * sizeof(pCanonFlags[0]));
			memset(pCanonFlags + startIndex, 0, len * sizeof(pCanonFlags[0]));
			
			if (firstCall)
				setLenOrbitOfSolution(len);
			else
			if (i < startIndex)		// When we add orbit which is LESS than the canonical solution of tested 
				startIndex -= len;	// solution associated on previous step, we need to adjust startIndex		

			// If our solution index was moved: 
			if (pSolIdx && *pSolIdx >= curCanon  && *pSolIdx < curCanon + lastCanonIdx)
				*pSolIdx -= len;  // adjust solution index

			// Move current solution index to the solution which precedes next canonical one
			if (changeSolutionIndex)
				setSolutionIndex(nextSolutionIdx - len - 1);

			if (pTmp != buffer)
				delete[] pTmp;
		}
	} else {
		// New group for current solution and its group was not found OR
		// this group is equivalent to already deleted solution
		// It means that it was removed (not costructed) on previous levels
		// and we don't need to use this group
		removeNoncanonicalSolutions(startIndex);
		return (size_t)-1;
	}

	return startIndex;
}