Пример #1
0
void TestTime::BFVRPsol(vector<vector<double> > &adjac, vector<double> &pref, unsigned int &limit, vector<City*> &bfvrp) {
    vector<City> cities {};
    convertData(cities, static_cast<int>(adjac.size()), adjac, pref);
    BruteForce<City, vector<vector<double> > > BF (limit, cities, 0, &adjac, &City::getPreference, getEdgeCost);
    bfvrp = BF.solve(true); // optime = false para retornar assim que achar uma solucao maxima (nao necessariamente a mais rapida das maximas)
    BF.freeHeap();
}
Пример #2
0
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    BruteForce w;
    w.show();

    return a.exec();
}
Пример #3
0
clock_t TestTime::runTimeTSPBF() {
    vector<City> cities {};
    clock_t ini {clock()};
    convertData(cities, numberCities, adjacency, preference);
    BruteForce<City, vector<vector<double> > > BF (notimeLimit, cities, 0, &adjacency, &City::getPreference, getEdgeCost);
    vector<City*> sol = BF.solve(true); // optime = false para retornar assim que achar uma solucao maxima (nao necessariamente a mais rapida das maximas)
    clock_t fim {clock()};
    BF.freeHeap();
    return fim - ini;
}
Пример #4
0
clock_t TestTime::runTimeBFG() {
    vector<City> cities {};
    clock_t ini {clock()};
    convertData(cities, numberCities, adjacency, preference);
    BruteForce<City, vector<vector<double> > > BF (timeLimit, cities, 0, &adjacency, &City::getPreference, getEdgeCost);
    vector<City*> sol=BF.greedySolve();
    clock_t fim {clock()};
    BF.freeHeap();
    return fim - ini;
}
Пример #5
0
void TestTime::BFTSPsol(vector<vector<double> > &adjac, vector<double> &pref, vector<City*> &bftsp) {
    vector<City> cities {};
    convertData(cities, static_cast<int>(adjac.size()), adjac, pref);
    unsigned int nolimit{0};
    for (unsigned int i = 0; i < adjac.size(); i++) {
        for (unsigned int j = 0; j < adjac.size(); j++) {
            nolimit += adjac[i][j];
        }
    }
    BruteForce<City, vector<vector<double> > > BF (nolimit, cities, 0, &adjac, &City::getPreference, getEdgeCost);
    bftsp = BF.solve(true); // optime = false para retornar assim que achar uma solucao maxima (nao necessariamente a mais rapida das maximas)
    BF.freeHeap();
}
Пример #6
0
int main(int argc, char ** argv)
{

	const char *prefix = "";
	int	num = -1, syncsteps = 0, bignum = 0;
	long	maxsteps = 5000;
	bool	debug = false, parsed = false, lazy = false;
	enum { BRUTEFORCE, RANDOMWALK, MONTECARLO, STUNEL, GDMIN }	alg = STUNEL;

	char	*fmeasured = 0;
	const char *tprefix = ".";

	MinChi	*min = 0;

	float	alpha = 0.1,beta = 5e-3,gamma = 500;
	long gdmin_set_size = -1;

	MPI_Init(&argc,&argv);

	cout << "# ";
	for (int i=0; i<argc; i++) cout << argv[i] << " ";
	cout << endl;

	int	opt;
	while ((opt = getopt(argc,argv,"n:m:b:da:l:g:s:qy:t:Lp:z:N")) != EOF) switch (opt) {
		case 'n': num = atoi(optarg); break;
		case 'N': bignum = 1; break;
		case 'm': fmeasured = optarg; break;
		case 'l': alpha = atof(optarg); break;
		case 'b': beta = atof(optarg); break;
		case 'g': gamma = atof(optarg); break;
		case 's': maxsteps = atol(optarg); break;
		case 'd': debug = true; break;
		case 'a': if (strcasecmp(optarg,"bruteforce") == 0) alg = BRUTEFORCE;
				  else if (strcasecmp(optarg,"random_walk")==0) alg = RANDOMWALK;
				  else if (strcasecmp(optarg,"montecarlo")==0) alg = MONTECARLO;
				  else if (strcasecmp(optarg,"stunnel")==0) alg = STUNEL;
                  else if (strcasecmp(optarg, "gdmin") == 0) alg = GDMIN;
				  else { usage(argv[0]); return 1; }
			  break;
		case 'q': parsed = true; break;
		case 'y': syncsteps = atoi(optarg); break;
		case 't': tprefix = optarg; break;
		case 'L': lazy = true; break;
		case 'p': prefix = optarg; break;
		case 'z': gdmin_set_size = atol(optarg); break;
		default: usage(argv[0]); return 1;
	}

	if (num<=0 || !fmeasured) { usage(argv[0]); return 1; }
	/* not anymore: assert(num < 100); /* XXX: hardcoded %02d elsewhere */

/* maximal step length (alltogether, not per dimension) */ 
	alpha /= sqrt(2. + num);

/* MC scaling, 5e-3 step up accepted with 10% */
	beta = - log(.1)/beta; 	
	assert(gdmin_set_size<0 || (gdmin_set_size > 0 && alg == GDMIN));

	Curve	measured;

	vector<C12Map>	maps;
	maps.resize(num);

	if (measured.load(fmeasured)) return 1;

	for (int i=0; i<num; i++) {
		char	buf[PATH_MAX];

/* used to align generated curves */		
		maps[i].setMeasured(measured);

		if (lazy) {
			if (num < 100 && !bignum)
				snprintf(buf,sizeof buf,"%s%02d.pdb",prefix,i+1);
			else 
				snprintf(buf,sizeof buf,"%s%02d/%04d.pdb",prefix,(i+1)/100,i+1);

			maps[i].setQMax(measured.getQMax());
			maps[i].setSize(measured.getSize());
			if (maps[i].setLazy(buf,fmeasured)) return 1;
		}
		else if (parsed) {
			snprintf(buf, sizeof buf, "%s%02d.c12",prefix,i+1);
			if (maps[i].restore(buf)) return 1;
			maps[i].alignScale(measured);
		}
		else {
			snprintf(buf,sizeof buf,"%s%02d/%02d_%%.2f_%%.3f.dat",prefix,i+1,i+1);
			if (maps[i].load(buf)) return 1;
			maps[i].alignScale(measured);
		}
	}

	switch (alg) {
		case BRUTEFORCE: {
			BruteForce *b =	new BruteForce(measured,maps);
			b->setStep(.01);
			min = b;
			break;
		}
		case RANDOMWALK: {
			RandomWalk *r = new RandomWalk(measured,maps);
			r->setParam(alpha);
			r->setMaxSteps(maxsteps);
			min = r;
			break;
		}
		case MONTECARLO: {
			MonteCarlo *m = new MonteCarlo(measured,maps);
			m->setParam(alpha,beta);
			m->setMaxSteps(maxsteps);
			min = m;
			break;
		}
		case STUNEL: {
			STunel *t = new STunel(measured,maps);
			t->setParam(alpha,beta,gamma);
			t->setMaxSteps(maxsteps);
			min = t;
			break;
		}
		case GDMIN: {
			GdMin *g = new GdMin(measured, maps);
			g->setParam(alpha);
			g->setMaxSteps(maxsteps);
			g->setSetSize(gdmin_set_size);
			min = g;
			break;
		}
		default:
			cerr << "algorithm " << alg << " not implemented" << endl;
			return 1;
	}

	min->setSyncSteps(syncsteps);


	int	rank;

	MPI_Comm_rank(MPI_COMM_WORLD,&rank);

	if (debug) {
		char	buf[PATH_MAX];

		mkdir(tprefix,0755);
		snprintf(buf,PATH_MAX,"%s/ensamble-fit_%d.trc",tprefix,rank);
		if (min->openTrace(buf) == NULL) return 1;
	}

	min->minimize(debug);

	MPI_Finalize();

}
InstanceSolution KnapsackAlgorithm::solveByForce(const InstanceProblem & problem) {
	BruteForce algorithm;
	return algorithm.solve(problem);
}
Пример #8
0
int main() {
	//=== initialize stuff
	create_Cities_and_Paths();

	loadInterface();
	initGraphCities();

	//=== select cities
	MenuLooper(citySelection);
	if(cities2visit.size()<3)
	{
		cout<<"\nNEED TO SELECT MORE THAN 2 CITIES TO VISIT! (press enter to end)"<<endl;
		getchar();
		return 0;
	}

	cout<<"Insert travel limit time (the press enter again to find solution):"<<endl;
	int limitTime = readPositiveInteger(1,99999);
	//for some reason need to press enter again here
	//cout<<"\n"<<limitTime;
	cout<<"Started calculating better path\n";
	create_2Use_edges();
	//=== find solution
	if(cities2visit.size()<10)
	{
		BruteForce<City, vector<vector<double> > > BF ((double)limitTime, cities2visit, 0, &edges2Visit, &City::getPreference, getEdgeCost);
		vector<City*> sol = BF.solve(true);

		if (sol.empty()) cout<<"No Solution was found."<<endl;
		else
		{
			cout<<"Optimal solution was found."<<endl;
			gv->setVertexColor(cities2visit[0].index,"green");

			stringstream ss;
			ss << edges[sol[sol.size()-1]->index][cities2visit[0].index];
			gv->addEdge(sol.size(),sol[sol.size()-1]->index,cities2visit[0].index,EdgeType::UNDIRECTED);
			gv->setEdgeLabel(sol.size(),ss.str());
			ss.str("");

			ss << edges[sol[0]->index][cities2visit[0].index];
			gv->addEdge(sol.size()+1,sol[0]->index,cities2visit[0].index,EdgeType::UNDIRECTED);
			gv->setEdgeLabel(sol.size()+1,ss.str());

			for(unsigned int i = 0; i<sol.size();i++)
			{
				gv->setVertexColor(sol[i]->index,"green");
				if(i>0)
					{
					ss.str("");//stringstream ss;
					ss << edges[sol[i-1]->index][sol[i]->index];
					gv->addEdge(i-1,sol[i-1]->index,sol[i]->index,EdgeType::UNDIRECTED);
					gv->setEdgeLabel(i-1,ss.str());
					}
			}
			gv->rearrange();
		}

		BF.freeHeap();
	}
	else {
		vector<unsigned int> sol;
		//!! !! !! these vars must be initialized with 0!!!
		double vrp_gain=0;
		double vrp_cost=0;
		vector<double> in=cities2VisitPreferences();

		GreedyApproxVRP(edges2Visit,in ,((double)limitTime),sol, vrp_gain, vrp_cost);
		if (sol.size()<=1) cout<<"No Solution was found."<<endl;
		else
		{
			cout<<"A solution was found."<<endl;
			cout<<"Preference sum equal to:"<<vrp_gain<<endl;
			cout<<"Total Cost (time that will be spent):"<<vrp_cost<< endl;
			for(unsigned int i = 0; i<sol.size();i++)
			{
				gv->setVertexColor(cities2visit[sol[i]].index ,"green");
				if(i>0)
					{
					stringstream ss;
					ss << edges[cities2visit[sol[i-1]].index][cities2visit[sol[i]].index];
					gv->addEdge(i-1,cities2visit[sol[i-1]].index,cities2visit[sol[i]].index,EdgeType::UNDIRECTED);
					gv->setEdgeLabel(i-1,ss.str());
					}
			}
			stringstream ss;
			ss << edges[cities2visit[sol[0]].index][cities2visit[sol.size()-1].index];
			gv->addEdge(sol.size(),cities2visit[sol[0]].index,cities2visit[sol[sol.size()-1]].index,EdgeType::UNDIRECTED);
			gv->setEdgeLabel(sol.size(),ss.str());
			gv->rearrange();
		}

	}

	cout<<"Press Enter to quit"<<endl;
	std::string info;
	getline( cin, info);
	delete gv;
	return 0;
}