void InternalGraph::assignPath(AntPath * pt)
{
    GraphComponent * gc;
    const std::vector<PathElement *> & path = pt->getPath();
    for (int i = 0; i < path.size(); ++ i)
    {
        if (path[i]->request == 0) continue;
        gc = vertices[path[i]->request-1];
        if (gc->getType() == GraphComponent::VMACHINE)
        {
            curNodesRes[path[i]->resource] -= gc->getRequired();
            curNodesRam[path[i]->resource] -= gc->getRequiredRam();
        }
        else if (gc->getType() == GraphComponent::STORAGE) curStoresRes[path[i]->resource] -= gc->getRequired();
    }
}
unsigned int InternalGraph::selectVertex(AntPath* pt, unsigned int cur, std::set<unsigned int> & available, bool& s, std::map<Element *, std::set<Link *> >& chan)
{
    // choose request
    unsigned int vertex = 0;
    unsigned int size = available.size();
    std::vector<double> roulette(size);
    std::vector<unsigned int> rouletteIndex(size);
    double value = 0, sum = 0;
    unsigned int index = 0;
    std::set<unsigned int>::iterator itEnd = available.end();
    for (std::set<unsigned int>::iterator i = available.begin(); i != itEnd; i ++, index ++)
    {
        value = pow(arcs[cur][*i]->pher, pherDeg)*pow(arcs[cur][*i]->heur, heurDeg);
        sum += value;
        roulette[index] = sum;
        rouletteIndex[index] = *i;
    }

    double choose = rand()/(double)RAND_MAX * sum;
    if (ZERO(sum))
    {
        std::set<unsigned int>::iterator sel = available.begin();
        vertex = *sel;
        available.erase(sel);
    }
    else
    {
        for (unsigned int i = 0; i < size; ++ i)
        {
            if (choose < roulette[i])
            {
                vertex = rouletteIndex[i];
                unsigned int res = available.erase(rouletteIndex[i]);
                assert(res == 1);
                break;
            }
        }
    }

// choose resource
    GraphComponent * gc = vertices[vertex-1];
    unsigned int res = gc->chooseResource(curNodesRam, pherDeg, heurDeg);
    if (res >= gc->getResNum())
    {
        // failed to choose a resource
        s = false;
        return vertex;
    }

    if (gc->getType() == GraphComponent::VMACHINE)
    {
        curNodesRes[res] -= gc->getRequired();
        curNodesRam[res] -= gc->getRequiredRam();
        updateInternalHeuristic(res, GraphComponent::VMACHINE);
        std::map<Element *, std::set<Link *> >::iterator iter = chan.find(gc->getPointer());
        std::set<Link *> * chanPtr = (iter != chan.end()) ? &(iter->second) : NULL;
        pt->addElement(new PathElement(vertex, gc->getPointer(), res, physNodes[res], chanPtr));
    }
    else if (gc->getType() == GraphComponent::STORAGE)
    {
        curStoresRes[res] -= gc->getRequired();
        updateInternalHeuristic(res, GraphComponent::STORAGE);
        std::map<Element *, std::set<Link *> >::iterator iter = chan.find(gc->getPointer());
        std::set<Link *> * chanPtr = (iter != chan.end()) ? &(iter->second) : NULL;
        pt->addElement(new PathElement(vertex, gc->getPointer(), res, physStores[res], chanPtr));
    }

    s = true;
    return vertex;
}