Exemple #1
0
string Environment::solveEnvironment(int typeOfAlgorithm)
{
    int points = 0;
    string solution = "";
    string newSolution = "";

    for(int i=0; i< size/2; i++)
    {
        if(typeOfAlgorithm == 1) newSolution = agent->widthSearch(i+1);
        if(typeOfAlgorithm == 2) newSolution = agent->depthSearch(i+1);
        if(typeOfAlgorithm == 3) newSolution = agent->aStarSearch(i+1);
        
        if(evalSolution(newSolution)>points)
        {
            solution = newSolution;
            points = evalSolution(newSolution);
        }
    }
    return solution; 
}
int Solver::randomAlgorithm(void)
{
	int nbItem = data.nbVars / data.nbConstraints;
	std::vector<int> marks;
	std::vector<int> w;

	w.resize(data.nbConstraints);
	marks.resize(data.nbVars);

	for (int i = 0; i < data.nbConstraints; i++)
		w[i] = 0;

	for (int i = 0; i < data.nbVars; i++)
		marks[i] = -1;

	solution.fctObj = 0;

	bool stop = false;
	srand(time(NULL));
	while (!stop)
	{
		int pos = rand() % data.nbVars;

		if (marks[pos] == -1)
		{
			solution.values.push_back(std::make_pair(data.values[0][pos], pos));

			for (int i = 0; i < data.nbConstraints; i++)
			{
				if ((w[i] + data.values[i + 1][pos]) > data.maxCapacity[i])
					stop = true;
				else
					w[i] += data.values[i + 1][pos];
			}
		}
	}

	evalSolution(solution);

	std::cout << "Solution: ";
	for (unsigned int i = 0; i < solution.values.size(); i++)
		std::cout << solution.values[i].first << " ";
	std::cout << std::endl;

	for (unsigned int i = 0; i < w.size(); i++)
		std::cout << "Constraint " << (i + 1) << ": " << w[i] << " < " << data.maxCapacity[i] << std::endl;

	std::cout << "Optimal: " << solution.fctObj << std::endl;

	return solution.fctObj;
}
Exemple #3
0
int main (int argc, char const *argv[])
{
	if (argc < 2){
		std::cout << "Usage:\n./pso <filename> <pop_size>" << std::endl;
		return 0;
	}
	
	int pop_size = 2000;
	const std::string filename = argv[1];
	if (argc > 2) pop_size = int(atoi(argv[2]));
	int iterations = 500;
	if (argc > 3) iterations = int(atoi(argv[3]));
	double alpha = 0.75;
	if (argc > 4) alpha = double(atof(argv[4]));
	double beta = 0.1;
	if (argc > 5) alpha = double(atof(argv[5]));
	
	std::vector<double> optHistory(iterations);
	
	srand((unsigned)time(NULL));
	std::vector<double>* objCost = readObjFunArray(filename.c_str());
	const int n_vars = (int)(sqrt(objCost->size()));
	
	
	
	std::clock_t start;
	start = std::clock();
	// create X
	std::vector<std::vector<int> > X(pop_size, std::vector<int>(n_vars));
	// these vector will store V
	std::vector<std::vector<swap> > V(pop_size, std::vector<swap>(0));
	// these vectors will store best individual positions
	std::vector<std::vector<int> > P(pop_size, std::vector<int>(n_vars));
	// this vector will store global best position
	std::vector<int> Pg(n_vars);
	// this  will store global best obj function value
	double fevalsPg = 0;
	// this vector will store current obj function values 
	std::vector<double> fevals(pop_size);
	// this vector will store individual best obj function values
	std::vector<double> fevalsP(pop_size);
	int n_swaps = 0;
	int first = 0;
	int second = 0;

	// generate random V == swap sequences
	// also generate random initial positions
	for (int i=0; i<pop_size; i++){
		for (int j=0; j<n_vars; j++){
			X[i][j] = j;  // fill X with nodes from 0 to n_vars-1
		}
		std::random_shuffle (X[i].begin(), X[i].end());  // shuffle
		P[i] = X[i];  // deep copy
		//for (int j=0; j<n_vars; j++){std::cout << X[i][j] << " ";}
		//std::cout << std::endl;
		n_swaps = rand() % (n_vars) + 1;
		for (int j=0; j<n_swaps; j++){
			first = rand() % n_vars;
			second = rand() % n_vars;
			V[i].push_back(swap (first, second));
			//std::cout << V[i][j].first << " " <<  V[i][j].second << std::endl;
		}
		//std::cout << V[i].size() << std::endl;
	}
	
	//evaluate global best
	for (int i=0; i<pop_size; i++){
		fevals[i] = evalSolution(applySwaps(X[i], V[i]), objCost, n_vars);
		fevalsP[i] = evalSolution(applySwaps(X[i], V[i]), objCost, n_vars);
		//std::cout << evals[i] << std::endl;
	}
	
	int globBestIndex = distance(fevals.begin(), min_element(fevals.begin(), fevals.end()));
	// std::cout << globBestIndex << std::endl;
	// save global best position and value
	Pg = applySwaps(X[globBestIndex], V[globBestIndex]);
	fevalsPg = fevals[globBestIndex];
	//start pso

	bool terminate = false;
	/*
	std::vector<int> pp1 = {1,2,3,4,5,6,7,8};
	std::vector<int> pp2 = {3,2,1,6,8,5,7,4};
	std::vector<swap> pp3 = diff(pp1, pp2);
	printarr1(pp1);
	printarr1(pp2);
	printarr2(pp3);*/
	
	for (int k=0; k<iterations && !terminate; k++){
	    for (int i=0; i<pop_size; i++){
			//3.1 calculate difference between P_i and X_i
			// A = P_i - X_i, where A is a basic sequence.
			std::vector<swap> A = diff(P[i], applySwaps(X[i], V[i]));
			std::vector<swap> B = diff(Pg, applySwaps(X[i], V[i]));
			
			// now lets compute A*alpha and B*beta
			for (int j=A.size()-1; j>=0; j--){
				double p1 = ((double)rand()/(double)RAND_MAX);
				if (p1>alpha){
					A.erase(A.begin() + j);
				}
			}
			for (int j=B.size()-1; j>=0; j--){
				double p2 = ((double)rand()/(double)RAND_MAX);
				if (p2>beta){
					B.erase(B.begin() + j);
				}
			}
			std::vector<swap> newV = V[i];
			// concatenate all the swaps in a single sequence
			newV.insert(newV.end(), A.begin(), A.end());
			newV.insert(newV.end(), B.begin(), B.end());
			// transform the swap sequence in Basic sequence form
			V[i] = toBasicSequence(newV, n_vars);
			// update position with formula X_i = X_i + V_i
			// X[i] = applySwaps(X[i], newV);
			
			// V[i].clear();
			
			// update fevals
			fevals[i] = evalSolution(applySwaps(X[i], V[i]), objCost, n_vars);
			// possibly update P
			if (fevals[i] < fevalsP[i]){
				fevalsP[i] = fevals[i];
				P[i] = applySwaps(X[i], V[i]);
			}
		}
		// possibly update Pg
		globBestIndex = distance(fevals.begin(), min_element(fevals.begin(), fevals.end()));
		Pg = applySwaps(X[globBestIndex], V[globBestIndex]);
		
		if (fevals[globBestIndex] < fevalsPg){
			fevalsPg = fevals[globBestIndex];
			Pg = applySwaps(X[globBestIndex], V[globBestIndex]);
		}
		optHistory[k] = fevalsPg;
	}
	double elapsedtime = (std::clock() - start) / (double)(CLOCKS_PER_SEC);
	//std::cout << "Time: " << elapsedtime << std::endl;
	//std::cout << "best: " << fevalsPg << std::endl;
	saveSolution(elapsedtime, fevalsPg, optHistory);
	//printarr1(shiftToFirst(Pg));
}