Ejemplo n.º 1
0
	def testRunCompleteTour(self):
		# Testing the presentation istance
		#antSys = AntSystem("../jssp_instances/transparencia.txt")
		antSys = AntSystem("../jssp_instances/Car5.txt")
		#antSys = AntSystem("../jssp_instances/abz5.txt", roh = 0.9)

		antSys.runCompleteTour(100)
		
		print "final solutions"
		for i in range(len(antSys.antScheds)):
			print "i: ", antSys.antScheds[i].makespan, antSys.antScheds[i].jobSched

		print "\nBest solution"
		print antSys.bestSchedule.makespan, antSys.bestSchedule.jobSched
		return
Ejemplo n.º 2
0
	def testAntTour(self):
		# Testing the presentation istance
		antSys = AntSystem("../jssp_instances/transparencia.txt")
		#antSys = AntSystem("../jssp_instances/Car5.txt")
		#antSys = AntSystem("../jssp_instances/abz5.txt")
		antSys.antTour()
		antSys.antTour()
		print "antTour:"
		
		print antSys.antScheds[0].makespan
		print antSys.antScheds[0].jobSched
		
		print antSys.antScheds[1].makespan
		print antSys.antScheds[1].jobSched
		
		return
def solve_it(input_data, method="", oporation="", solo=False, default=True):
    # Modify this code to run your optimization algorithm

    # parse the input
    lines = input_data.split('\n')

    nodeCount = int(lines[0])

    points = []
    for i in range(1, nodeCount + 1):
        line = lines[i]
        parts = line.split()
        points.append(Point(float(parts[0]), float(parts[1])))
    if len(points) > 30000 and default:
        print("greedy")
        return greedy(points)

    NPproblem_tsp = tsp.tsp(points)
    if default:
        if len(points) > 200:
            approx = NPproblem_tsp.christofides()
            return print_solution(NPproblem_tsp, approx, "simulatedAnneling")
        else:
            approx = NPproblem_tsp.gurobi_method()
            return approx

    if method == 'approximation2':
        approx = NPproblem_tsp.approximation2()
    elif method == 'christofides':
        approx = NPproblem_tsp.christofides()
    elif method == 'antSystem':
        Hormigas = AntSystem.AntSystem(points, 1, 2, 0.02, 10, nodeCount,
                                       nodeCount)
        algo = Hormigas.calcule_route()
        return verbose(algo[1], algo[0])
    elif method == 'gurobi':
        approx = NPproblem_tsp.gurobi_method()
        return approx
    else:
        approx = NPproblem_tsp.christofides()
    if solo:
        return verbose(approx[1], approx[0])
    else:
        return print_solution(NPproblem_tsp, approx, oporation)
Ejemplo n.º 4
0
	def testFindBestJobSchedule(self):
		antSys = AntSystem("../jssp_instances/abz5.txt")

		for i in range(3):
			antSys.antScheds = []
			for j in range(antSys.ants):
				antSys.antTour()
			#antSys.trailUpdate()
			antSys.findBestJobSchedule()
			
			print "Ant Schedules"
			for i in range(len(antSys.antScheds)):
				print "i: ", antSys.antScheds[i].makespan, antSys.antScheds[i].jobSched
			
			print "\nBest solution"
			print antSys.bestSchedule
			print antSys.bestSchedule.makespan, antSys.bestSchedule.jobSched
		return
Ejemplo n.º 5
0
def runAntSystem(pop_multiplier, alpha, beta, evaporation_rate, execution):
    
    dist = readDistFile(instance)

    ant_system = AntSystem(dist, len(dist) * pop_multiplier, ITERATIONS, alpha, beta, evaporation_rate, Q)
    ant_system.initPheromone()
    ant_system.initAnts()

    for t in range(ant_system.iterations):
        best_evaluation_iter = float('inf') 
        for ant in ant_system.population:
            while (len(ant.solution) < ant_system.node_count):
                ant_system.goToNextNode(ant)
            ant.solution.append(ant.solution[0])
            ant.evaluation = ant_system.calculateFO(ant.solution)

            if (ant.evaluation < best_evaluation_iter):
                best_evaluation_iter = ant.evaluation

            if (ant.evaluation < ant_system.best_evaluation):
                ant_system.best_solution = ant.solution
                ant_system.best_evaluation = ant.evaluation
        
        fileOperations(best_evaluation_iter, pop_multiplier, alpha, beta, evaporation_rate, execution, 0)

        ant_system.pheromoneUpdate()
        ant_system.restartAnts()

    fileOperations(ant_system.best_evaluation, pop_multiplier, alpha, beta, evaporation_rate, execution, 1)
Ejemplo n.º 6
0
# Options
for o, a in opts:
	if o == "-n":
		iterations = int(a)
	elif o == "-Q":
		q = float(a)
	elif o == "-a":
		aX = float(a)
	elif o == "-v":
		verbose = True
	else:
		printUsage()
		sys.exit(2)

# Running Ant System Heuristic
antSys = AntSystem(jsspFile, Q=q, antX=aX)

antSys.runCompleteTour(iterations)

if verbose:
	print "--- TRAIL MATRIX ---"
	antSys.printTrailMatrix()
	print "\nGreedy: ", antSys.greedy
	print "\n--- FINAL SOLUTIONS ---"
	for i in range(len(antSys.antScheds)):
		print antSys.antScheds[i].makespan, antSys.antScheds[i].jobSched
	print "\nBest solution built:"
	print antSys.bestSchedule.makespan, antSys.bestSchedule.jobSched
else:
	print antSys.bestSchedule.makespan, antSys.bestSchedule.jobSched
Ejemplo n.º 7
0
from sys import argv
from utils import *
from constants import *
from AntSystem import *

instanceFile = 'LAU15.txt' if len(argv) < 2 else argv[1]
dist = readDistFile(instanceFile)

ant_system = AntSystem(dist,
                       len(dist) * POP_MULTIPLIER, ITERATIONS, ALPHA, BETA,
                       EVAPORATION_RATE, Q)
ant_system.initPheromone()
ant_system.initAnts()

for t in range(ant_system.iterations):
    best_evaluation_iter = float('inf')
    for ant in ant_system.population:
        while (len(ant.solution) < ant_system.node_count):
            ant_system.goToNextNode(ant)
        ant.solution.append(ant.solution[0])
        ant.evaluation = ant_system.calculateFO(ant.solution)

        if (ant.evaluation < best_evaluation_iter):
            best_evaluation_iter = ant.evaluation

        if (ant.evaluation < ant_system.best_evaluation):
            ant_system.best_solution = ant.solution
            ant_system.best_evaluation = ant.evaluation

    print(f'Best evaluation of iteration {t}: {best_evaluation_iter}')