Пример #1
0
def genetic_algorithm():
    print("Genetic Algorithm\n")
    population = Population(SIZE, generateRandomAllocation(), MUTATION_RATE,
                            ELITISM)
    for i in range(20):
        print("\t{}/{}".format(i, 20), end='\r')
        population.nextGeneration()

    return population.currentGroup[0][0]
Пример #2
0
def hill_climbing_1():
    print("Hill Climbing Basic\n")
    current = generateRandomAllocation()
    current_value = current.value()
    while (1):
        neighbour, neighbour_value = getBetterNeighbour(current)
        print("Current: ", current_value)
        print("Neighbour: ", neighbour_value)
        if neighbour_value >= current_value:
            return current
        current = neighbour
        current_value = neighbour_value
Пример #3
0
def run_trials(out_avg_file):
    solution_sets = []
    best_s_sets = []

    total_cpu_time = time()

    for i in range(trials):
        (solution, best_s) = TabuSearch(generateRandomAllocation(), k, max_iter)
        solution_sets.append(solution)
        best_s_sets.append(best_s)
        print("trial complete")

    total_cpu_time = time() - total_cpu_time

    with open(out_avg_file, 'w') as out_file:
        for i in range(max_iter):
            total_cost_best = 0

            for sol in range(trials):
                total_cost_best += solution_sets[sol][i]

            avg_cost_best = total_cost_best / trials

            out_file.write("%d, %f\n" % (i + 1, avg_cost_best))

    # Avg and Std deviation
    best_costs = numpy.zeros([trials, 1])
    for sol in range(trials):
        best_costs[sol] = solution_sets[sol][max_iter - 1]

    avg_cost = numpy.mean(best_costs)
    std_cost = numpy.std(best_costs)

    print("Average best cost: %f, Std Dev: %f" % (avg_cost, std_cost))

    # Avg CPU Time
    avg_cpu_time = total_cpu_time / trials
    print("Average CPU time: %f" % avg_cpu_time)

    with open("best_solutions_TS.txt", 'w') as output:
        output.write('TS = [')
        for best_cost in best_costs:
            output.write(str(best_cost[0]) + ' ')
        output.write('];')
Пример #4
0
def simulated_annealing():
    print("Simulated Annealing\n")
    current = generateRandomAllocation()
    T = 20
    for i in range(1, MAX_ATTEMPS):
        if T < 0:
            return current
        neighbour, neighbour_value = getRandomNeighbour(current)
        current_value = current.value()
        diff = neighbour_value - current_value
        probability = math.exp(-diff / T)
        print("Current Value", current_value, "----- Temperature", T,
              "----- Probability", probability)
        if diff < 0:
            current = neighbour
        elif probability > random.uniform(0, 1):
            current = neighbour
        T = T - ((1 / math.log(1 + i)) * 0.1)
    return current
Пример #5
0
def RandomSearch(maxiter, m=7, n=50):
    sCur = np.zeros([maxiter, m, n])
    sBest = np.zeros([maxiter, m, n])
    solution = np.zeros(maxiter)
    bestCost = 500.0

    for i in range(maxiter):
        sCur[i,:,:] = np.array(generateRandomAllocation())
        curCost = cost(sCur[i,:,:])

        if curCost < bestCost:
            sBest[i,:,:] = sCur[i,:,:]
            bestCost = curCost
        else:
            sBest[i,:,:] = sBest[i-1,:,:]

        solution[i] = bestCost

    return solution, sBest[maxiter - 1,:,:]
Пример #6
0
import DDS
import numpy
from time import time
from allocation import generateRandomAllocation

min_x = 0.0
max_x = 10
min_s = [min_x for i in range(7)]
max_s = [max_x for i in range(7)]
Z = []
trials = 30
max_iter = 1500

while len(Z) != trials:
    Z.append(generateRandomAllocation())


def run_trials(out_avg_file):
    solution_sets = []
    best_s_sets = []

    total_cpu_time = time()

    for i in range(trials):
        (solution, best_s) = DDS.DDS(Z[i], min_s, max_s, max_iter, 1.0)
        solution_sets.append(solution)
        best_s_sets.append(best_s)

    total_cpu_time = time() - total_cpu_time

    with open(out_avg_file, 'w') as out_file:
Пример #7
0
import time
from cost import cost
from allocation import generateRandomAllocation

# Timing the execution of 10,000 random allocations + cost evaluations
numEvals = 10000
start_time = time.time()
for i in range(numEvals):
    cost(generateRandomAllocation())

print("%d evals take %s seconds." % (numEvals, (time.time() - start_time)))
Пример #8
0
from allocation import generateRandomAllocation
from cost import cost
from SA import neighbor

s_values = []
cost_values = []

#Set initial values
s = generateRandomAllocation()
cost_s = cost(s)

s_values.append(s)
cost_values.append(cost_s)
best_s = s
best_cost = cost_s

# Find other s_values and calculate their costs
while len(s_values) != 100:
    new_s = neighbor(s)
    cost_s = cost(new_s)
    if cost_s < best_cost:
        best_s = new_s
        best_cost = cost_s
    s_values.append(new_s)
    cost_values.append(cost_s)

neighbor_total_cost = 0

for (new_s, cost_s) in zip(s_values, cost_values):
    if new_s != best_s:
        neighbor_total_cost += (cost_s - best_cost)