示例#1
0
void twoOpt() {
    if (done_twoOpt) return;
    cur_energy = cur.getDist();
    int start, finish, temp;
    finish = -1;
    start = 0;

    while (finish < start) {
        start = cur.getDist();
        for (int r = 0; r < NUM_COLORS - 1; r++) {
            for (int c = r + 1; c < NUM_COLORS; c++) {
                if (!cur.swapIsBeneficial(r, c))  continue;
                cur.twoOptSwap(r, c);
                cur_energy = cur.getUpdatedDistWhenTwoOptSwap(cur_energy, r, c);
                if (ANIMATE) {
                cur.draw();
                update();
            }
            }

        }
        finish = cur.getDist();

        if (finish < start)
            best_tour.updateColors(cur.colors);
    }
    cout << "Final distance after 2-opt: " << best_tour.getDist() << endl;
    done_twoOpt = true;
}
示例#2
0
int main(int argc, char * argv[])
{
    srand ( time(NULL) );
    CSDLManagerLite::getInstance()->initializeSDL(WIDTH, HEIGHT, TITLE);

    cout << "Initial distance: " << cur.getDist() << endl;
    cout << "Colors: " << NUM_COLORS << endl;

    int a, b;
    double cur_energy, new_energy;

    while (!CSDLInputManagerLite::getInstance() -> isExit())
    {
        CSDLManagerLite::getInstance()->delay(1000);
        twoOpt();
        best_tour.draw();
        update();
    }
    CSDLManagerLite::getInstance()->clean();
}