def test7_MNproportion(): ############################################################################ ############################## Test executions ############################# ############################################################################ ################################### Set 1 ################################## for proportion in xrange(ND): op1Time = [] op1Drivers = [] op1Heuristic = [] for iteration in xrange(ITERATIONS): s = State(nPassengers=proportion, nMaxDrivers=ND-proportion, initialDistribution="allOneFirst") c = Carmageddon(s, "km", "1") to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() op1Time.append(tf - to) op1Drivers.append(finalState.getNumDrivers()) op1Heuristic.append(c.printableHeuristic(finalState)) op1AvrTime = reduce(lambda x, y: x + y, op1Time)/len(op1Time) op1AvrDrivers = reduce(lambda x, y: x + y, op1Drivers)/len(op1Drivers) op1AvrHeuristic = reduce(lambda x, y: x + y, op1Heuristic)/len(op1Heuristic) # print "\t\t Average_results_of_npassengers= ",proportion print "\t Average_time: \t\t ", op1AvrTime
def test4_TemporalEvolution(operatorSet="1", initDistrib="allOneFirst"): print "Starting test at: ", datetime.now() N = NP incn = NP results = [] for i in range(5): M = N/2 nP = M nD = N-M s = State(nPassengers=nP, nMaxDrivers=nD, initialDistribution=initDistrib) c = Carmageddon(s, "km", operatorSet) to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() results.append((N, M, (tf - to), c.printableHeuristic(finalState), finalState.getNumDrivers())) N += incn print "Iteration ", i, " takes ", results[-1][2], " with N ", N, " M ", M, " optimizit to " , results[-1][3], " and ", results[-1][3], " drivers" print "Test finishes at ", datetime.now() for i in results: print "With N = %d M = %d it takes %s seconds resulting in %d heuristic value and %d drivers" % i return results
def test3_SimulatedAnnealingParams(): # lK = [1,5,25,125] # ll = [0.1,0.01,0.001,0.0001] # for kParameter in lK: # for lParameter in ll: # print "Other params" # lcost = [] # for i in xrange(ITERATIONS): # s = State(nPassengers=20, nMaxDrivers=20, initialDistribution="allOneFirst") # c = Carmageddon(s, "km_an", "1") # finalState = c.run('simulatedAnnealing',kParameter,lParameter,2000) # lcost.append(c.printableHeuristic(finalState)) # print "Average solution cost for params k = "+str(kParameter)+", lambda = "+str(lParameter)+" :" # print "\t",reduce(lambda x, y: x + y, lcost)/len(lcost) # print "" lIt = [2000] kParameter = 5 lParameter = 0.01 for nIter in lIt: lcost2 = [] ltime = [] for it in xrange(ITERATIONS): t = datetime.now() s = State(nPassengers=20, nMaxDrivers=20, initialDistribution="allOneFirst") c = Carmageddon(s, "km_an", "1") finalState = c.run('simulatedAnnealing',kParameter,lParameter,nIter) lcost2.append(c.printableHeuristic(finalState)) ltime.append(datetime.now() -t) print "Average solution cost for params k = "+str(kParameter)+", lambda = "+str(lParameter)+"with "+str(nIter)+" iterationr :" print "\t",reduce(lambda x, y: x + y, lcost2)/len(lcost2) print "Average time:" print "\t",reduce(lambda x, y: x + y, ltime)/len(ltime) print ""
def test6_HCvsSA(operatorSet="1", initDistrib="allOneFirst", k=1, lam=0.001, lim=100): ############################################################################ ############################## Test executions ############################# ############################################################################ ################################ HC - km ############################### i = 0 hcKmTime = [] hcKmDrivers = [] hcKmDistance = [] hcKmHeuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " HC - KM" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution=initDistrib) c = Carmageddon(s, "km", operatorSet) to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() hcKmTime.append(tf - to) # Discard outlayers if finalState.isGood(): hcKmDrivers.append(finalState.getNumDrivers()) hcKmDistance.append(finalState.getKm()) hcKmHeuristic.append(c.printableHeuristic(finalState)) i += 1 ################################# HC - veh ################################ i = 0 hcVehTime = [] hcVehDrivers = [] hcVehDistance = [] hcVehHeuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " HC - VEH" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution=initDistrib) c = Carmageddon(s, "veh", operatorSet) to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() hcVehTime.append(tf - to) # Discard outlayers if finalState.isGood(): hcVehDrivers.append(finalState.getNumDrivers()) hcVehDistance.append(finalState.getKm()) hcVehHeuristic.append(c.printableHeuristic(finalState)) i += 1 ################################# SA - km ################################ i = 0 saKmTime = [] saKmDrivers = [] saKmDistance = [] saKmHeuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " SA - KM" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution=initDistrib) c = Carmageddon(s, "km", operatorSet) to = datetime.now() finalState = c.run("simulatedAnnealing", k, lam, lim) tf = datetime.now() saKmTime.append(tf - to) # Discard outlayers if finalState.isGood(): saKmDrivers.append(finalState.getNumDrivers()) saKmDistance.append(finalState.getKm()) saKmHeuristic.append(c.printableHeuristic(finalState)) i += 1 ################################# SA - veh ################################ i = 0 saVehTime = [] saVehDrivers = [] saVehDistance = [] saVehHeuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " SA - VEH" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution=initDistrib) c = Carmageddon(s, "veh", operatorSet) to = datetime.now() finalState = c.run("simulatedAnnealing", k, lam, lim) tf = datetime.now() saVehTime.append(tf - to) # Discard outlayers if finalState.isGood(): saVehDrivers.append(finalState.getNumDrivers()) saVehDistance.append(finalState.getKm()) saVehHeuristic.append(c.printableHeuristic(finalState)) ## Stats hcKmAvrTime = reduce(lambda x, y: x + y, hcKmTime)/len(hcKmTime) hcKmAvrDrivers = reduce(lambda x, y: x + y, hcKmDrivers)/len(hcKmDrivers) hcKmAvrDistance = reduce(lambda x, y: x + y, hcKmDistance)/len(hcKmDistance) hcKmAvrHeuristic = reduce(lambda x, y: x + y, hcKmHeuristic)/len(hcKmHeuristic) hcVehAvrTime = reduce(lambda x, y: x + y, hcVehTime)/len(hcVehTime) hcVehAvrDrivers = reduce(lambda x, y: x + y, hcVehDrivers)/len(hcVehDrivers) hcVehAvrDistance = reduce(lambda x, y: x + y, hcVehDistance)/len(hcVehDistance) hcVehAvrHeuristic = reduce(lambda x, y: x + y, hcVehHeuristic)/len(hcVehHeuristic) saKmAvrTime = reduce(lambda x, y: x + y, saKmTime)/len(saKmTime) saKmAvrDrivers = reduce(lambda x, y: x + y, saKmDrivers)/len(saKmDrivers) saKmAvrDistance = reduce(lambda x, y: x + y, saKmDistance)/len(saKmDistance) saKmAvrHeuristic = reduce(lambda x, y: x + y, saKmHeuristic)/len(saKmHeuristic) saVehAvrTime = reduce(lambda x, y: x + y, saVehTime)/len(saVehTime) saVehAvrDrivers = reduce(lambda x, y: x + y, saVehDrivers)/len(saVehDrivers) saVehAvrDistance = reduce(lambda x, y: x + y, saVehDistance)/len(saVehDistance) saVehAvrHeuristic = reduce(lambda x, y: x + y, saVehHeuristic)/len(saVehHeuristic) print "\t\t Average results of \"HC - KM\" of operators" print "\t Average time: \t\t ", hcKmAvrTime print "\t Average drivers:\t\t ", hcKmAvrDrivers print "\t Average distance:\t\t ", hcKmAvrDistance print "\t Average heuristic:\t\t ", hcKmAvrHeuristic print "\t\t Average results of \"HC - VEH\" of operators" print "\t Average time: \t\t ", hcVehAvrTime print "\t Average drivers:\t\t ", hcVehAvrDrivers print "\t Average distance:\t\t ", hcVehAvrDistance print "\t Average heuristic:\t\t ", hcVehAvrHeuristic print "\n\n" print "\t\t Average results of \"SA - KM\" of operators" print "\t Average time: \t\t ", saKmAvrTime print "\t Average drivers:\t\t ", saKmAvrDrivers print "\t Average distance:\t\t ", saKmAvrDistance print "\t Average heuristic:\t\t ", saKmAvrHeuristic print "\t\t Average results of \"SA - VEH\" of operators" print "\t Average time: \t\t ", saVehAvrTime print "\t Average drivers:\t\t ", saVehAvrDrivers print "\t Average distance:\t\t ", saVehAvrDistance print "\t Average heuristic:\t\t ", saVehAvrHeuristic
def test1_Operators(): ############################################################################ ############################## Test executions ############################# ############################################################################ ################################### Set 1 ################################## i = 0 op1Time = [] op1Drivers = [] op1Heuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " of Set 1" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution="allOneFirst") c = Carmageddon(s, "km", "1") to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() op1Time.append(tf - to) # Discard outlayers if finalState.isGood(): op1Drivers.append(finalState.getNumDrivers()) op1Heuristic.append(c.printableHeuristic(finalState)) i += 1 ################################### Set 2 ################################### i = 0 op2Time = [] op2Drivers = [] op2Heuristic = [] for execution in range(ITERATIONS): print "Executing iteration ", i, " of Set 2" s = State(nPassengers=NP, nMaxDrivers=ND, initialDistribution="allOneFirst") c = Carmageddon(s, "km", "2") to = datetime.now() finalState = c.run("hillClimbing") tf = datetime.now() op2Time.append(tf - to) # Discard outlayers if finalState.isGood(): op2Drivers.append(finalState.getNumDrivers()) op2Heuristic.append(c.printableHeuristic(finalState)) i += 1 ## Stats op1AvrTime = reduce(lambda x, y: x + y, op1Time)/len(op1Time) op1AvrDrivers = reduce(lambda x, y: x + y, op1Drivers)/len(op1Drivers) op1AvrHeuristic = reduce(lambda x, y: x + y, op1Heuristic)/len(op1Heuristic) op2AvrTime = reduce(lambda x, y: x + y, op2Time)/len(op2Time) op2AvrDrivers = reduce(lambda x, y: x + y, op2Drivers)/len(op2Drivers) op2AvrHeuristic = reduce(lambda x, y: x + y, op2Heuristic)/len(op2Heuristic) print "\t\t Average results of set1 of operators" print "\t Average time: \t\t ", op1AvrTime print "\t Average drivers:\t\t ", op1AvrDrivers print "\t Average heuristic:\t\t ", op1AvrHeuristic print "\n\n" print "\t\t Average results of set2 of operators" print "\t Average time: \t\t ", op2AvrTime print "\t Average drivers:\t\t ", op2AvrDrivers print "\t Average heuristic:\t\t ", op2AvrHeuristic if op1AvrHeuristic == op2AvrHeuristic: print "Both operators has the same heurístic resuluts!!!" return if op1AvrHeuristic < op2AvrHeuristic: best = "1" worst = "2" else: best = "2" worst = "1" print "The operator set %s is better than operator set %s" % (best, worst) return best
alg = argv[-1] operatorSet = argv[-2] optimizer = argv[-3] if len(argv) == 7: m = int(argv[2]) n = int(argv[1]) numDrivers = n-m numPassengers = m print "There are " + str(numPassengers) + " passengers and " + str(numDrivers) + " drivers." s = State(nPassengers=numPassengers, nMaxDrivers=numDrivers, initialDistribution=argv[3]) else: s = State(cfgfile=argv[2]) c = Carmageddon(s, optimizer, operatorSet) resul = c.run(alg) logFileName = "carmageddon-%s.log" % str(datetime.now()).replace(' ','_') resul.saveToFile(logFileName) print resul print "Log file saved at carmageddon.log it can be open and parsed using the showLog.py util:\n" print "\t\tpython showLog.py %s\n" % logFileName