Esempio n. 1
0
    # ae '(0, 1): 10' is point 0's link to point 1 with a distance of 10
    for i in range(0, mapSize):
        for k in range(i + 1, mapSize):
            path = (i, k)
            path_dist = randrange(1, 11)
            pathsList[path] = path_dist

    # don't over-clutter the screen with paths and distances
    if mapSize <= 10:
        print('Paths:\t' + str(pathsList ))

    problem = TSP(mapSize, pathsList)

    t = time.time()
    hill_climbing = hill_climbing(problem)
    hc_time = time.time() - t

    print('Hill climbing:\t' + str(hill_climbing)
          + '\n\tvalue: ' + str(problem.value(hill_climbing))
          + '\n\ttime: ' + str(hc_time)
          )

    t = time.time()
    sim_annealing = simulated_annealing(problem, exp_schedule(k=20, lam=0.005, limit=1000))
    sa_time = time.time() - t

    print('Simulated annealing\t: ' + str(sim_annealing )
          + '\n\tvalue: ' + str(problem.value(sim_annealing ))
          + '\n\ttime: ' + str(sa_time)
          )
Esempio n. 2
0
    def value(self, x):
        return math.fabs(x * math.sin(x))


if __name__ == '__main__':

    # Formulate a problem with a 2D hill function and a single maximum value.
    maximum = 30
    initial = randrange(0, maximum)
    p = AbsVariant(initial, maximum, delta=1.0)
    print('Initial                      x: ' + str(p.initial) + '\t\tvalue: ' +
          str(p.value(initial)))

    # Solve the problem using hill-climbing.
    startTime = time.time()
    hill_solution = hill_climbing(p)
    endTime = time.time() - startTime
    print('Hill-climbing solution       x: ' + str(hill_solution) +
          '\tvalue: ' + str(p.value(hill_solution)) + '\t\ttime: ' +
          str(endTime))

    # Solve the problem using simulated annealing.
    startTime = time.time()
    annealing_solution = simulated_annealing(
        p, exp_schedule(k=20, lam=0.005, limit=1000))
    endTime = time.time() - startTime
    print('Simulated annealing solution x: ' + str(annealing_solution) +
          '\tvalue: ' + str(p.value(annealing_solution)) + '\t\ttime: ' +
          str(endTime))
Esempio n. 3
0
            if distance not in TSPstate:
                TSPaction.append(distance)
        return TSPaction

if __name__ == '__main__':

    #change the number of cities it travels
    randomCity = 50
    randomPath = {}

#indicates path and distances that have been randomized
    for cityNumber in range(0, randomCity):
        for space in range(cityNumber + 1, randomCity):
            local = (cityNumber, space)
            newPathDistance = randrange(1, 9)
            randomPath[local] = newPathDistance

    TSPproblem = TSP(randomCity, randomPath)

#implementations
    hillClimbingImp = hill_climbing(TSPproblem)
    simulatedAnnealingImp = simulated_annealing(TSPproblem, exp_schedule(k=10, lam=0.005, limit=100))

#prints hill climbing and simulated annealing results
    print('Hill climbing:\t' + str(hillClimbingImp)
          + '\n\tvalue: ' + str(TSPproblem.value(hillClimbingImp))
          )

    print('Simulated annealing\t: ' + str(simulatedAnnealingImp)
          + '\n\tvalue: ' + str(TSPproblem.value(simulatedAnnealingImp))
          )