void no_recursive_search_reduce(dgrafo & g, Tour & t, Tour & best_tour, bool pt = 0) {

	tour_pila_t pila;
	pila.push(t);
	while (!pila.empty()){
		Tour actual = pila.top();
		pila.pop();
		if (g.size() == actual.size()) {
			myVector vecinos = g.get_edges(*(actual.firt_city().first));
			int p = g.peso(*(actual.last_city().first), *(actual.firt_city().first));
			city temp(actual.firt_city().first, p);
			actual.insert(temp);
			if (pt){ actual.print();}
			if (best_tour.ranking() > actual.ranking()){
				best_tour = actual;}
			//actual.remove_last_city();
		}else{
			myVector vecinos = g.get_edges(*(actual.last_city().first));
			for (int i=vecinos.size()-1;i>=1;i--){
				if (actual.feasible(*(vecinos[i].first))) {
					actual.insert(vecinos[i]);
					pila.push(actual);
					actual.remove_last_city();
				}
			}
		}
	}
}
예제 #2
0
파일: tsp.cpp 프로젝트: danwa223/TDDD86
int main(int argc, char *argv[]) {

    QApplication a(argc, argv);

	// define 4 points forming a square
	Point p(100.0, 100.0);
	Point q(500.0, 100.0);
	Point r(500.0, 500.0);
	Point s(100.0, 500.0);
	// Set up a Tour with those four points
	// The constructor should link p->q->r->s->p
	Tour squareTour(p, q, r, s);
	squareTour.show();

    string filename = "tsp10.txt";
    ifstream input;
    input.open(filename);

    // get dimensions
    int width;
    int height;
    input >> width;
    input >> height;

    // setup graphical window
    QGraphicsView *view = new QGraphicsView();
    QGraphicsScene *scene = new QGraphicsScene();
    view->setScene(scene);
    view->scale(1, -1); //screen y-axis is inverted
    view->setSceneRect(0, 0, width, height);
    view->show();

    // run insertion heuristic
    Tour tour;
    double x;
    double y;
    while (input >> x >> y) {
        Point p(x, y);
		//tour.insertNearest(p);
        tour.insertSmallest(p);
        //uncomment the 4 lines below to animate
		tour.draw(scene);
		std::chrono::milliseconds dura(50);
		std::this_thread::sleep_for(dura);
		a.processEvents();
	}
    input.close();

    // print tour to standard output
    cout << "Tour distance: " << std::fixed << std::setprecision(4)
         << std::showpoint << tour.distance() << endl;
    cout << "Number of points: " << tour.size() << endl;
	tour.show();

    // draw tour
    tour.draw(scene);
	return a.exec(); // start Qt event loop
}
void no_recursive_search_parallel(dgrafo & g, Tour & t, Tour & best_tour) {

	tour_pila_t pila;
	pila.push(t);
	omp_lock_t writelock;
	omp_init_lock(&writelock);
	int count = 0;
	#pragma omp parallel
	{
	#pragma omp for 
		for (int u = 0; u<5;u++){

			Tour actual;
			if(!pila.empty()){
				omp_set_lock(&writelock);
				count++;
				omp_unset_lock(&writelock);
				//cout <<"Hola"<<endl;
				actual = pila.top();
				pila.pop();

				if (g.size() == actual.size()) {
					myVector vecinos = g.get_edges(*(actual.firt_city().first));
					int p = g.peso(*(actual.last_city().first), *(actual.firt_city().first));
					city temp(actual.firt_city().first, p);
					actual.insert(temp);
					omp_set_lock(&writelock);
					if (best_tour.ranking() > actual.ranking()){
						best_tour = actual;}
					omp_unset_lock(&writelock);
					//actual.remove_last_city();
				}else{
					myVector vecinos = g.get_edges(*(actual.last_city().first));
					for (int i=vecinos.size()-1;i>=1;i--){
					omp_set_lock(&writelock);
						if (actual.feasible(*(vecinos[i].first))) {
							actual.insert(vecinos[i]);
							pila.push(actual);
							actual.remove_last_city();
						}
					omp_unset_lock(&writelock);
					}
					
				}
			}
		}
		
	}
	cout<<"Contador: "<<count<<endl;
	omp_destroy_lock(&writelock);
}
예제 #4
0
int main(int argc, char** argv)
{
    vector<string> args(0);
    vector<string> values(0);

    char* test = "node1=13&nodet2=34&node3=u&node4=u&edge1-2=1&edge2-1=1&edge2-3=1&edge3-2=1&edge3-4=1&edge4-3=1&edge4-1=1&edge1-4=1&edge1-3=2&edge3-1=2&edge4-2=2&edge2-4=2&start=3&goal=3";
    //strcpy(test,"http://localhost/cgi-bin/little?node1=u&node2=u&node3=u&node4=u&edge1-2=1&edge2-1=1&edge2-3=1&edge3-2=1&edge3-4=1&edge4-3=1&edge4-1=1&edge1-4=1&edge1-3=2&edge3-1=2&edge4-2=2&edge2-4=2&start=2&goal=2");
    char* tachatte =  getenv("QUERY_STRING");

    string query_string(test);
    parseQueryString(query_string,args,values);

    /*
    for(int i=0;i<args.size();i++)
    {
            cout << args[i] << ":" << values[i] << endl;
    }
    */

    ofstream file("zobite.txt",ios::out | ios::trunc);


    Graph g(false,true);

    bool boule = createGraph(g,args,values);


    Tour response;

    int start = atoi(values[values.size()-1].c_str());

    if(boule)
        response = g.little(start);

    boule = boule && response.isCircuit() && ((int)g.getOrder()==(int)response.size());



    


    cout << "Content-Type:text/xml";
    cout << "<little>";
    string status;
    (boule) ? status="OK" : status="Aucune tournee trouvee";
    cout << "<status>" << status << "</status>";
    cout << "<cost>" << response.getCost() << "</cost>";
    cout << "<tour>";

    int* tab = response.toTab();

    for(int i=0;i<response.length();i++)
    {
        cout << "<id>" << tab[i] << "</id>";
    }

    cout << "</tour>";
    cout << "</little>";

    

    file.close();

    return 0;
}