# op.printschedule(s) # print(op.schedulecost(s)) domain = [(0, 9)] * (len(op.people) * 2) # s = op.randomoptimize(domain, op.schedulecost) # s = op.hillclimb(domain, op.schedulecost) # s = op.annealingoptimize(domain, op.schedulecost) # s = op.geneticoptimize(domain, op.schedulecost) # print(domain,s) # print(op.schedulecost(s)) # op.printschedule(s) result = {'rand': [], 'hill': [], 'anne': [], 'gene': []} for i in range(10): s1 = op.randomoptimize(domain, op.schedulecost) result['rand'].append(op.schedulecost(s1)) s2 = op.hillclimb(domain, op.schedulecost) result['hill'].append(op.schedulecost(s2)) s3 = op.annealingoptimize(domain, op.schedulecost) result['anne'].append(op.schedulecost(s3)) s4 = op.geneticoptimize(domain, op.schedulecost) result['gene'].append(op.schedulecost(s4)) print(result) print('rand', sum(result['rand']) / 10, min(result['rand'])) print('hill', sum(result['hill']) / 10, min(result['hill'])) print('anne', sum(result['anne']) / 10, min(result['anne'])) print('gene', sum(result['gene']) / 10, min(result['gene']))
import optimization reload(optimization) domain=[(0,9)]*(len(optimization.people)*2) s=optimization.hillclimb(domain,optimization.schedulecost) print optimization.schedulecost(s)
import time import random import math import optimization def randomoptimize(domain, costf): best = 999999999 bestr = None for i in range(1000): # 無作為解の生成 r = [random.randint(domain[i][0], domain[i][1]) for i in range(len(domain))] # コストの取得 cost = costf(r) # 最良解と比較 if cost < best: best = cost bestr = r return bestr # 書籍にはrと書かれていた if __name__ == '__main__': domain = [(0,9)]*(len(optimization.people)*2) # 書籍には(0.8)と書かれていた s = randomoptimize(domain, optimization.schedulecost) optimization.printschedule(s) cost = optimization.schedulecost(s) print('cost is %4s' % (cost))
def test_cal_schedule_cost(): s = [1, 4, 3, 2, 7, 3, 6, 3, 2, 4, 5, 3] print optimization.schedulecost(s)
import random import optimization # search for best solution by creating random # solutions and checking their cost. def randomsearch(domain, costf): best = 999999999 bestsol = None # random number of iterations. for i in range(1000): # create a random solution. sol = [ random.randint(domain[i][0], domain[i][1]) for i in range(len(domain)) ] # calculate the cost. cost = costf(sol) if best > cost: best = cost bestsol = sol return sol # for each person there are 9 outbound and inbound flights. domain = [(0, 8)] * (len(optimization.dataset) * 2) sol = randomsearch(domain, optimization.schedulecost) print(optimization.schedulecost(sol))
print(u'1. Исходные данные:') print('----------------------------------') optimization.printpeople() print('') print(u'Аэропорт встречи: ') print(optimization.destination) print('') print('') print(u'2. Оптимизация:') print('----------------------------------') domain = [(0,8)]*(len(optimization.people)*2) print(u'2.1 Алгоритм случайного поиска...') srand = optimization.randomopt(domain, optimization.schedulecost); rand = optimization.schedulecost(srand) print('') print(u'2.2 Алгоритм спуска с горы...') shill = optimization.hilldownopt(domain, optimization.schedulecost); hill = optimization.schedulecost(shill) print('') print(u'2.3 Алгоритм имитации отжига...') sanneal = optimization.annealingopt(domain, optimization.schedulecost); anneal = optimization.schedulecost(sanneal) print('') print(u'2.4 Генетический алгоритм...') sgen = optimization.geneticopt(domain, optimization.schedulecost); gen = optimization.schedulecost(sgen)
def test_find_optimized_schedule(optimization_method): domain = [(0, 8)] * (len(optimization.people) * 2) r = optimization_method(domain, optimization.schedulecost) optimization.printschedule(r) print optimization.schedulecost(r)
#coding:utf-8 import optimization #s = [1,4,3,2,7,3,6,3,2,4,5,3] #print optimization.printschedule(s) #print optimization.schedulecost(s) domain = [(0, 9)] * len(optimization.people) * 2 #s = optimization.randomoptimize(domain,optimization.schedulecost) #s = optimization.hillclimb(domain,optimization.schedulecost) #s = optimization.annealingoptimize(domain,optimization.schedulecost) s = optimization.geneticoptimize(domain, optimization.schedulecost) print optimization.schedulecost(s) print optimization.printschedule(s) ''' 这两种及大多数优化方法都假设:大多数问题,最优解应该接近于其他的最优解。 但某些特殊情况不一定有效。比如存在陡峭的突变的最优解。 '''
import optimization import dorm import socialnetwork # Group Travel Problem print('<----Group Travel---->') s = [1, 4, 3, 2, 7, 3, 6, 3, 2, 4, 5, 3] optimization.printschedule(s) cost = optimization.schedulecost(s) print('Present cost:%f' % cost) print('\n<----Random Searching---->') domain = [(0, 9)] * (len(optimization.people) * 2) print(s) s = optimization.randomoptimize(domain, optimization.schedulecost) cost = optimization.schedulecost(s) print('Present cost:%f' % cost) optimization.printschedule(s) print('\n<----Hill Climbing---->') s = optimization.hillclimb(domain, optimization.schedulecost) print(s) cost = optimization.schedulecost(s) print('Present cost:%f' % cost) optimization.printschedule(s) print('\n<----Simulated Annealing---->') s = optimization.annealingoptimize(domain, optimization.schedulecost) print(s) cost = optimization.schedulecost(s)
from pprint import pprint import optimization domain = [(0, 9)] * (len(optimization.people) * 2) # random search s = optimization.randomoptimize(domain, optimization.schedulecost) print(optimization.schedulecost(s)) pprint(optimization.printschedule(s)) # hillclimb s = optimization.hillclimb(domain, optimization.schedulecost) print(optimization.schedulecost(s)) optimization.printschedule(s) # simulated annealing s = optimization.annealingoptimize(domain, optimization.schedulecost) print(optimization.schedulecost(s)) pprint(optimization.printschedule(s)) # genetic algorithms s = optimization.geneticoptimize(domain, optimization.schedulecost) # print(optimization.schedulecost(s)) pprint(optimization.printschedule(s))
import optimization as op import time s = [1,4,3,2,7,3,6,3,2,4,5,3] domain = [(0,9)]*len(s) print 'fix:', start = time.clock() print op.schedulecost(s) op.printschedule(s) print 'running time',time.clock()-start print print 'random optimization:', start = time.clock() sol = op.randomoptimize(domain) print op.schedulecost(sol) op.printschedule(sol) print 'running time',time.clock()-start print print 'hill climb:', start = time.clock() sol = op.hillclimb(domain) print op.schedulecost(sol) op.printschedule(sol) print 'running time',time.clock()-start print print 'annealing optimization:', start = time.clock()
# for i in range(100): # # domain: the range of each value in the solution, the range is decided on how many choices of each pair of two places can be choosed # domain=[(0,3)]*(len(optimization.people)*2) # # run the optimization # s=optimization.annealingoptimize(domain,optimization.schedulecost) # # print the cost of final solution # print('The schedule cost is ',optimization.schedulecost(s)) # # print(optimization.schedulecost(s)) # print() # # print the final solution # optimization.printschedule(s) # domain: the range of each value in the solution, the range is decided on how many choices of each pair of two places can be choosed domain = [(0, 3)] * (len(optimization.people) * 2) # run the optimization s = optimization.annealingoptimize(domain, optimization.schedulecost) # print the cost of final solution print('The schedule cost is ', optimization.schedulecost(s)) # print(optimization.schedulecost(s)) print() # print the final solution optimization.printschedule(s)
def test_schedulecost(s): sys.stderr.write("testing schedulecost...\n") print optimization.schedulecost(s) sys.stderr.write("??? expecting 5285 according to textbook???...\n")
def test_annealing(domain): sys.stderr.write("testing annealing optimization...\n") s = optimization.annealingoptimize(domain, optimization.schedulecost) print optimization.schedulecost(s) optimization.printschedule(s)
def test_hillclimb(domain): sys.stderr.write("testing hillclimb optimization...\n") s = optimization.hillclimb(domain, optimization.schedulecost) print optimization.schedulecost(s) optimization.printschedule(s)
def test_randomoptimize(domain): sys.stderr.write("testing random optimization...\n") s = optimization.randomoptimize(domain, optimization.schedulecost) print optimization.schedulecost(s) optimization.printschedule(s)