Esempio n. 1
0
int main()
{
// Estrutura List
 Route route;
// Ler ponto de um arquivo

 std::ifstream ifs ("points.dat", std::ifstream::in);

 point aux(0,0);

 while (ifs >> aux.real() >> aux.imag()) { 
  route.push_back(aux);
 }

 ifs.close();

// List teste

// Algoritmo
// Step 1: Calcular as poupanças sij=ci0+c0j-cij para i,j=1,...mn i!=j. Criar n rotas de veículos
// (0,i,0) para i=1,...,n. Ordenar as economias num modo não crescente.
 int n=route.size();
 double s[n][n-1];
 point origin=*route.begin();
 Route Aux;
 Subroute Sb; 

 for (Route::iterator i=++route.begin(); i!=route.end(); ++i) {
  Aux.push_back(*i);
  Sb.push_back(Aux);
  for ( Route::iterator j=i; j!=route.end(); ++j) {
   if( i!=j) {
     s[i][j]( std::abs(*i-origin)+std::abs(origin-*j)-std::abs(*i-*j) );
   }
  }
  Aux.clear();
 }

 std::cout << Sb.size() << std::endl;

 s.sort();

 for (std::list<double>::iterator i=s.begin(); i!=s.end(); ++i) 
	std::cout << *i << std::endl;

// Step 2: Iniciar do topo da lista de economias, executando o seguinte. Dada a economia sij,
// determine se há duas rotas, uma contendo arco ou aresta (0,j) e a outra contendo o arco ou aresta
// (i,0), que pode ser mescladas. Se então, combine essas duas rotas deletando (0,j) e (i,0) e
// introduzindo (i,j).

 
 // Mesclar rotas
 for(Subroute::iterator I=Sb.end(); I!=Sb.begin(); --I) {
  
 }

 return 0;
}
Esempio n. 2
0
		void simplifyRouteTest()
		{
			int row, column;
			cin >> row >> column;
			vector<vector<int> > board(HEIGHT, vector<int>(WIDTH, 0));
			inputBoard(board);

			const int up = 0;
			const int right = 1;
			const int down = 2;
			const int left = 3;

			Route route;

			//vector<int> routes = { up, up, up, up, up, down, up, up, up, up, up, up, up
			//	, up, up, up, up, up, up, up, up, right };

			vector<int> routes = { down, down, down, down, down, up, left, down, right, down, down, up, up, up, up,
				up, up, up, up, up, up, up };

			route.push_back(routes);
			debug(route);
			vector<vector<int>> board2 = board;
			moveByRoute(board, route, row, column);
			debug(board);
			cout << endl;
			Route simplifid;
			simplifyRoutePerfectly(route, row, column, simplifid);
			//_simplifyRouteNonMoving(board, route, row, column, simplifid);
			cout << endl;
			debug(simplifid);
			moveByRoute(board2, simplifid, row, column);
			debug(board2);
		}
Esempio n. 3
0
Data GarbageCentral::getRoute(vector<GarbageDeposit*> to_pick) {

	if (to_pick.size() <= 1)
		throw RouteMissingData();

	auto map = graph.dfs();
	auto map_it = begin(map);

	Route route;
	vector<GarbageDeposit*> missing_deposits;
	GarbageDeposit* gd = new GarbageDeposit();
	vector<GarbageDeposit*>::iterator gd1_it;

	while ( (gd1_it = find(begin(to_pick), end(to_pick), map_it->getPointer())) == end(to_pick) ) {
		map_it++;
	}

	*gd = **gd1_it;
	map_it++;



	while (to_pick.size() > 1) {

		if ( find(to_pick.begin(), to_pick.end(), map_it->getPointer()) != to_pick.end() ) {

			try {
				Section section = getShortestPath(gd, map_it->getPointer());
				updatePicks(to_pick, section);
				route.push_back(section);
				*gd = *section.first[section.first.size() - 1];
			} catch (NoOptimalPath &e) {
				missing_deposits.push_back(map_it->getPointer());

				for (unsigned i = 0; i < to_pick.size(); i++) {
					if ( *to_pick[i] == *(map_it->getPointer()) ) {
						to_pick.erase(begin(to_pick) + i);
						break;
					}
				}
			}
		}

		map_it++;
	}

	delete gd;
	return make_pair(route, missing_deposits);
}
Esempio n. 4
0
		void calcRoutes_4(int lookAheadCount)
		{
			//route, length
			std::queue<std::tuple<Route, int>> que;
			que.emplace(Route(), 0);

			for (int i = 0; i < LookAheadMax; i++)
			{
				_routes[i].clear();
			}

			//bfsでルートを全列挙
			while (!que.empty())
			{
				auto tup = que.front(); que.pop();
				int length = std::get<1>(tup);
				Route route = std::get<0>(tup);

				_routes[length].push_back(route);

				//debug(route);

				if (length == lookAheadCount)
				{
					continue;
				}

				int pre = route.top();
				for (int i = 0; i < 4; i++)
				{
					if ((i + 2) % 4 == pre) continue;//行って戻るのは禁止

					Route nextRoute = route;
					nextRoute.push_back(i);
					que.emplace(nextRoute, length + 1);

				}
			}
		}
Esempio n. 5
0
inline void prepend(Route& route, const TpointRange& pointRange) {
  Path path;
  append(path, pointRange);
  route.push_back(path);
}
Esempio n. 6
0
inline void prepend(Route& route, const Segment& seg) {
  Path path;
  append(path, seg);
  route.push_back(path);
}
Esempio n. 7
0
inline void prepend(Route& route, const Point& p) {
  Path path;
  append(path, p);
  route.push_back(path);
}
Esempio n. 8
0
Pathfinding::Route Pathfinding::getPath(Entity* entity, const Vector3& goal) {
    stringstream prettyInfo;

    Vector3 start = entity->getPosition();

    double a  = phantom::Util::getTime();
    _layer.cleanPathfinding();

    Route route;

    if(_showDebug) {
        cout << endl<< endl<< endl<< endl;
    }

    if(_visualize) {
        getGraphics().clear();
    }

    // Goal space uses a "strict" heuristic. EG: we don't want to walk into a tree.
    Space* goalSpace  = _layer.getSpaceAtUsingHeuristic(goal, entity);

    // There is no "space" available at the destination. The entity probably wants
    // to walk into a tree. Returns an empty route.
    if(goalSpace == nullptr) {
        if(_showDebug) {
            cout << "Goal vector is not a space." << endl;
        }
        return route;
    }

    // Start space, first try using a strict heuristic. EG: If we start near a tree
    // we don't want to walk into that tree.
    Space* startSpace = _layer.getSpaceAtUsingHeuristic(start, entity);

    // Ok, did we find a start space with the strict heuristic? If not, it probably
    // means that our entity is stuck in a tree. Proceed with a less strict
    // heuristic. In most cases this will let the entity "leave" the solid object
    // that it's currently stuck on.
    if(startSpace == nullptr) {
        startSpace = _layer.getSpaceAtUsingHeuristic(start);
    }

    // Ok, we really can't walk anywhere. This is a rather odd case. Most likely
    // the user tried to walk outside of the BSP tree, or you've just found a bug
    // in the BSP tree.
    if(startSpace == nullptr) {
        if(_showDebug) {
            cout << "Start vector is not a space." << endl;
        }
        route.push_back(Vector3(goal));
        return route;
    }

    if(_showDebug) {
        cout << "Starting at: " << startSpace->getArea().toString();
        cout << "Ending at  : " << goalSpace->getArea().toString();
    }

    if(_visualize) {
        Box3& m = startSpace->getArea();

        getGraphics().beginPath().setFillStyle(Color(0, 0, 127, 20))
                .rect(m.origin.x+4, m.origin.y+4, m.size.x-8, m.size.y-8)
                .fill();

        Box3& n = goalSpace->getArea();
        getGraphics().beginPath().setFillStyle(Color(0, 0, 127, 20))
               .rect(n.origin.x+4, n.origin.y+4, n.size.x-8, n.size.y-8)
               .fill();
    }

    priority_queue<Space*, vector<Space*>, CompareShapesAstar> open;

    startSpace->isInOpenList = true;
    open.push(startSpace);

    if(_showBasicDebug) {
        prettyInfo << "Pathfinding overhead: " << std::fixed << (phantom::Util::getTime() - a) << " seconds. ";
    }

    int spacesScanned = 0;
    const double startTime  = phantom::Util::getTime();


    int timeout = 0;
    while(true) {
        if(open.empty()) {
            if(_showBasicDebug || _showDebug) {
                cout << "      Open list empty." << endl;
                double now = phantom::Util::getTime();

                if(_showBasicDebug) {
                    prettyInfo << "No route found, scanned "<< spacesScanned << " Tile(s) in " << std::fixed << (now - startTime) << " seconds.";
                }

            }

            break;
        }

        if(timeout++ > 10000) {
            cout << "      I give up after " << timeout << " tries. " << endl;
            double now = phantom::Util::getTime();
            cout << "A* scanned " << spacesScanned << " Tile(s) in " << std::fixed << (now - startTime) << " seconds. " << endl;

            break;
        }

        Space* current = open.top();
        open.pop();

        if(_visualize) {
            //cout << "  - Testing: " << current->getArea().toString();
            drawRect(current, Color(0, 127, 127, 10));
        }

        if(current == goalSpace) {
            if(_showDebug) {
                cout << "    **** found! This is a good sign. " << endl;
            }
            unfoldRoute(route, current, startSpace, entity);


            if(!route.empty()) {
                route.pop_front();

                Vector3 lastpos = goal - entity->getBoundingBox().size * 0.5;

                // Replace the last way-point with our mouse click coordinates:
                if(route.empty()) {
                    route.push_back(lastpos);
                } else {
                    route.back() = lastpos;
                }
            }


            double now = phantom::Util::getTime();

            if(_showBasicDebug) {
                prettyInfo << "Found route, A* scanned " << spacesScanned << " Tile(s) in " << std::fixed << (now - startTime) << " seconds. Waypoint(s): " << route.size() << ".";
            }

            break;
        }

        vector<Space*>& neighbours = _layer.getNeighbours(current, entity);

        if(_showDebug && neighbours.empty()) {
            cout << "      No neighbours found." << endl;
        }

        for(size_t i = 0; i < neighbours.size(); ++i) {
            Space* testing = neighbours[i];

            if(!testing->isInOpenList) {
                spacesScanned++;
                testing->astarParent  = current;
                testing->isInOpenList = true;
                testing->g = current->g + Services::settings()->pathfinding_g_cost;
                testing->h = calculateHeuristic(goalSpace, testing);
                testing->h = testing->h * testing->h;
                open.push(testing);
            }
        }
    }

    if(_showBasicDebug) {
        //cout << prettyInfo.str() << endl;
        Console::log(prettyInfo.str());
    }

    return route;
}