int main() { // Initialize hardware if (!elev_init()) { printf(__FILE__ ": Unable to initialize elevator hardware\n"); return 1; } printf("Best Elevator Software, version: 2.27.14\n" ); initialize(); lastFloorReached = N_FLOORS+1; while (1) { queueIsEmpty = emptyQueue(); obstructionSignal = elev_get_obstruction_signal(); stoppedSignal = elev_get_stop_signal(); thisFloor = elev_get_floor_sensor_signal(); duration = clock()-start; if ( timerActive && duration > seconds ) { timerActive = 0; duration = 0; stateMachine( TIMEROUT ); } if ( obstructionSignal != lastObstructionSignal ) { if ( obstructionSignal == 1 ) stateMachine( OBSTRON ); if ( obstructionSignal == 0 ) stateMachine( OBSTROFF ); lastObstructionSignal = obstructionSignal; } if ( stoppedSignal != lastStoppedSignal ) { if ( stoppedSignal == 1 ) stateMachine( STOP ); lastStoppedSignal = stoppedSignal; } if ( thisFloor != -1 && ( lastFloorReached != thisFloor ) ) { if ( thisFloor-lastFloorReached < 0 ) direction = DOWN; if ( thisFloor-lastFloorReached > 0 ) direction = UP; lastFloorReached = thisFloor; elev_set_floor_indicator( thisFloor ); } if ( order() && directionToGo( thisFloor, getNextOrder( thisFloor ) ) == 0 ) stateMachine( NEWORDERCURRENTFLOOR ); if ( thisFloor != -1 && directionToGo( thisFloor , getNextOrder( thisFloor ) ) == 0 && !timerActive ) stateMachine( FLOORREACHED ); if( queueIsEmpty == 1 && order() ) stateMachine( NEWORDEREMPTYQUEUE ); } return 0; }
TSP Anneal::anneal(TSP Tsp, double coolingRate) { double temperature = pow((double)tsp.getDimension(), 2) * 1000.0; double absoluteTemperature = 0.00001; this->tsp = Tsp; dimension = tsp.getDimension(); Greedy greedy; currentOrder = greedy.greedy(tsp).getSolution(); //currentOrder.resize(dimension); //for (int i = 0; i < dimension; i++) currentOrder[i] = i; vector<int> bestOrder = currentOrder; int iteration = -1; double deltaDistance = 0; double distance = getTotalDistance(currentOrder); double bestDistance = distance; uniform_real_distribution<double> distribution(0.0, 1.0); default_random_engine generator; while (temperature > absoluteTemperature) { nextOrder = getNextOrder(currentOrder); deltaDistance = getTotalDistance(nextOrder) - distance; if ((deltaDistance < 0) || (distance > 0 && exp(-deltaDistance / temperature) > distribution(generator))) { currentOrder = nextOrder; distance = deltaDistance + distance; } if (distance < bestDistance) bestOrder = currentOrder; else if (distance > bestDistance * 1.5) { currentOrder = bestOrder; distance = bestDistance; } temperature *= coolingRate; iteration++; } tsp.updateSolution(bestOrder); return tsp; }