Ejemplo n.º 1
0
void testGraph() {
    // ====== usage for an GeometricalGraph ======
    const int pointNum = 20;
    const int coordRange = 10;
    RangeRand plrr(-coordRange, coordRange);
    RangeRand vrr(0, pointNum - 1);

    // generate a pointList
    GeometricalGraph::PointList pointList;
    for (int i = 0; i < pointNum; ++i) {
        pointList.push_back(GeometricalGraph::Point(plrr(), plrr()));
    }

    GeometricalGraph gg(pointList);



    // ====== usage for an UndDirectedGraph ======
    UndirectedGraph<double> ug(gg);
    ug.getShortestPath();    // can be leave out
    ug.getDistSeqTable();
    ug.printShortestDist(cout);
    ug.printDistSeqTable(cout);

    int vertex = ug.nthClosestVertex(vrr(), vrr());
    vertex = ug.findVertexWithinRadius(vrr(), coordRange);
    int vnum = ug.findVertexNumWithinRadius(vrr(), coordRange);
    TopologicalGraph<double>::Distance dd = ug.distance(vrr(), vrr());



    // ====== usage for an DirectedGraph ======
    const unsigned nodeNum = 10;
    const unsigned arcNum = 20;
    const int minVertexIndex = 1;
    RangeRand alrr(minVertexIndex, nodeNum + minVertexIndex - 1);
    RangeRand drr(1, 255);

    // generate an arcList for an directed graph with unsigned distance randomly
    TopologicalGraph<>::ArcList arcList;
    for (int i = 0; i < arcNum; ++i) {
        int startVertex = alrr();
        int endVertex;
        do {
            endVertex = alrr();
        } while (endVertex == startVertex);
        arcList.push_back(TopologicalGraph<>::Arc(startVertex, endVertex, drr()));
    }

    DirectedGraph<> dg(arcList, nodeNum, minVertexIndex);
    dg.getShortestPath();    // can be leave out
    dg.getDistSeqTable();
    dg.printShortestDist(cout);
    dg.printDistSeqTable(cout);

    vertex = dg.nthClosestVertex(alrr(), alrr());
    vertex = dg.findVertexWithinRadius(alrr(), drr());
    vnum = dg.findVertexNumWithinRadius(alrr(), drr());
    TopologicalGraph<>::Distance ud = dg.distance(alrr(), alrr());
}
int solve_pmedcap1( ofstream &csvFile, int group, int instanceNum )
{
    ostringstream fname;
    fname << "../Instances/pmedcap" << group << "(" << instanceNum << ").txt";
    ifstream ifs( fname.str() );

    unsigned problemNum, optima;
    unsigned vertexNum, medianNum, medianCap;

    int nodeSeqNum;
    GeometricalGraph::Coord x, y;
    int demand;

    GeometricalGraph::PointList pl;
    Problem::DemandList dl;

    ifs >> problemNum >> optima;
    ifs >> vertexNum >> medianNum >> medianCap;

    while (vertexNum--) {
        ifs >> nodeSeqNum >> x >> y >> demand;
        pl.push_back( GeometricalGraph::Point( x, y ) );
        dl.push_back( demand );
    }

    ifs.close();


    GeometricalGraph gg( pl );
    //UndirectedGraph<double> dug( gg );
    //dug.getDistSeqTable();
    UndirectedGraph<DistType, ACCURACY> uug( gg );
    uug.getDistSeqTable();

    // for each instance, run some times for judging average performance
    const int runTime = 4;
    const int maxIterCountBase = 3200;
    const int tabuTenureAssign = uug.vertexNum * medianNum / 5;
    const int tabuTenureOpenMedian = uug.vertexNum / 4;
    const int tabuTenureCloseMedian = medianNum / 3;
    const int maxNoImproveCount = uug.vertexNum * medianNum * 4;
    const int randomWalkStep = uug.vertexNum * 2;
    const DistType demandDistributionDamping = (uug.DistMultiplication * (gg.getMinCoverRect().right - gg.getMinCoverRect().left) / 4);
    for (int i = 1; i <= runTime; i++) {
        {
            Problem cpmp( uug, dl, medianNum, medianCap );
            cpmp.solve( maxIterCountBase, maxNoImproveCount, tabuTenureAssign,
                        tabuTenureOpenMedian, tabuTenureCloseMedian, demandDistributionDamping, randomWalkStep );
            cpmp.printOptima( cout );
            if (!cpmp.check()) {
                csvFile << "[LogicError] ";
            }
            cpmp.appendResultToSheet( fname.str(), csvFile );
        }
        ;
        {
            Problem cpmp( uug, dl, medianNum, medianCap );
            cpmp.solve_ShiftSwapTabuRelocateTabu( maxIterCountBase, maxNoImproveCount, tabuTenureAssign,
                                                  tabuTenureOpenMedian, tabuTenureCloseMedian, demandDistributionDamping );
            cpmp.printOptima( cout );
            if (!cpmp.check()) {
                csvFile << "[LogicError] ";
            }
            cpmp.appendResultToSheet( fname.str(), csvFile );
        }
        ;
        {
            Problem cpmp( uug, dl, medianNum, medianCap );
            cpmp.solve_ShiftSwapTabuRelocate( maxIterCountBase, maxNoImproveCount, tabuTenureAssign, demandDistributionDamping );
            cpmp.printOptima( cout );
            if (!cpmp.check()) {
                csvFile << "[LogicError] ";
            }
            cpmp.appendResultToSheet( fname.str(), csvFile );
        }
    }

    return 0;
}