def run_anneal(init_function,move_operator,objective_function,max_iterations,start_temp,alpha): if start_temp is None or alpha is None: usage(); print "missing --cooling start_temp:alpha for annealing" sys.exit(1) from sa import anneal iterations,score,best=anneal(init_function,move_operator,objective_function,max_iterations,start_temp,alpha) return iterations,score,best
def test_codec(): #run a simple test: code challenge N=300 data=loadtxt('example.txt') J=coo_matrix((data[:,2],(data[:,0],data[:,1])),shape=(N,N),dtype='int32').toarray() J=(J+J.T)/2. cc=CC(J) Emin,Config=anneal(cc,tempscales=linspace(10,0.6,51),nms=4000,nrun=30) assert_(Emin==-3858 and cc.get_cost(Config)==Emin)
def run_sa(iterations, dist, start, alpha, draw=True, every=100): init_function = lambda: init_random_tour(len(dist[0])) objective_function = lambda tour: -tour_length(dist, tour) move_operator = reversed_sections iterations,score,best = sa.anneal(init_function, move_operator, \ objective_function, iterations, \ start, alpha, draw, every) print iterations, score, best return best
def run_sa(iterations, dist, start, alpha, draw = True, every = 100): init_function = lambda: init_random_tour(len(dist[0])) objective_function = lambda tour: -tour_length(dist, tour) move_operator = reversed_sections iterations,score,best = sa.anneal(init_function, move_operator, \ objective_function, iterations, \ start, alpha, draw, every) print iterations, score, best return best
def solve_anneal(aff): objective_function=lambda tour: tour_sum_affinities(aff,tour) init_function=lambda: tsp.init_random_tour(sqrt(len(aff))) move_operator=tsp.reversed_sections #suggested parameters for simulated annealing: start_temp = 10.0 max_iterations = 5000 alpha = .001**(1/max_iterations) from sa import anneal iterations,score,best=anneal(init_function,move_operator,objective_function,max_iterations,start_temp,alpha) return iterations,score,best
def test_simple_anneal(): ''' check we perform the same as a hillclimb when score keeps improving ''' def move_operator(i): yield i+1 def init_function(): return 1 num_evaluations,best_score,best=sa.anneal(init_function,move_operator,objective_function,max_evaluations,1,0.9) assert num_evaluations == max_evaluations assert best == max_evaluations assert best_score == max_evaluations
print("Working with {} engines\n".format(len(engines))) print("Initial state") sa.print_state(state, current=True) eg_thrust = 50 eg_turn = 10 eg_heat = 10 eg_capacity = 20 eg_mass = 20 print(f"Trial th{eg_thrust},tn{eg_turn},h{eg_heat},c{eg_capacity},m{eg_mass}:") sa.print_state( sa.anneal(copy.deepcopy(state), iterations, engines, eg_thrust, eg_turn, eg_heat, eg_capacity, eg_mass, verbose=False)) eg_thrust = 30 eg_turn = 15 eg_heat = 20 eg_capacity = 5 eg_mass = 20 print(f"Trial th{eg_thrust},tn{eg_turn},h{eg_heat},c{eg_capacity},m{eg_mass}:") sa.print_state( sa.anneal(copy.deepcopy(state), iterations, engines,