Example #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();
}
Example #2
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;
}
Example #3
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();
}
InstanceSolution KnapsackAlgorithm::solveByForce(const InstanceProblem & problem) {
	BruteForce algorithm;
	return algorithm.solve(problem);
}
Example #5
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;
}