def optimize(): state = [0, 3, 2, 1, 0, 3, 2, 3, 1, 2, 2, 2, 3, 2, 1, 3, 0, 0, 0, 1, 3, 2, 0, 2, 0, 1, 0, 2, 2, 1, 2, 2, 0, 3] annealer = Annealer(energy, move) schedule = annealer.auto(state, minutes=0.1) state, e = annealer.anneal(state, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=6) print(state) # the "final" solution
def optimize(): state = [ 0, 3, 2, 1, 0, 3, 2, 3, 1, 2, 2, 2, 3, 2, 1, 3, 0, 0, 0, 1, 3, 2, 0, 2, 0, 1, 0, 2, 2, 1, 2, 2, 0, 3 ] annealer = Annealer(energy, move) schedule = annealer.auto(state, minutes=0.1) state, e = annealer.anneal(state, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=6) print(state) # the "final" solution
system[1] = cvalues[index] elif component == 2: index = random.randrange(0, len(rvalues)) system[2] = rvalues[index] elif component == 3: index = random.randrange(0, len(rvalues)) system[3] = rvalues[index] if __name__ == '__main__': # set up simulated annealing algorithm units=('F', 'F', u'Ω', u'Ω') # units of the values in the system initial_system = [cvalues[0], cvalues[0], rvalues[0], rvalues[0]] annealer = Annealer(energy, move) schedule = annealer.auto(initial_system, minutes=0.1) # run simulated annealing algorithm and compute properties of the final system final_system, error = annealer.anneal(initial_system, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=100) final_frf = frf(final_system) final_f0 = f0(final_system) final_q = q(final_system) final_vals = [metric_prefix(*s) for s in zip(final_system, units)] print 'Soln: (%s), Remaining Energy: %s' % (', '.join(final_vals), error) # calculate data for graphs freqs = range(1000000) # response from 0 Hz to 1 MHz response = [dB(final_frf(f)) for f in freqs] natural = final_f0, dB(final_frf(final_f0)) target = target_freq, dB(final_frf(target_freq))
def run(schedule=None): state = [] def reserve_move(state): """ Select random watershed then Add watershed (if not already in state) OR remove it. * This is the Marxan technique as well """ huc = hucs[int(random.random() * num_hucs)] #huc = hucs[random.randint(0,num_hucs-1)] if huc in state: state.remove(huc) else: state.append(huc) def reserve_energy(state): """ The "Objective Function"... Calculates the 'energy' of the reserve. Should incorporate costs of reserve and penalties for not meeting species targets. Note: This example is extremely simplistic compared to the Marxan objective function (see Appendix B in Marxan manual) but at least we have access to it! """ # Initialize variables energy = 0 totals = {} for fish in species: totals[fish] = 0 # Get total cost and habitat in current state for huc in state: watershed = watersheds[huc] # Sum up total habitat for each fish for fish in species: if energy == 0: # reset for new calcs ie first watershed totals[fish] = watershed[fish] else: # add for additional watersheds totals[fish] += watershed[fish] # Incorporate Cost of including watershed energy += watershed['watershed_cost'] # incorporate penalties for missing species targets for fish in species: pct = totals[fish] / targets[fish] if pct < 1.0: # if missed target, ie total < target if pct < 0.1: # Avoid zerodivision errors # Limits the final to 10x specified penalty pct = 0.1 penalty = int(penalties[fish] / pct) energy += penalty return energy annealer = Annealer(reserve_energy, reserve_move) if schedule is None: print('----\nAutomatically determining optimal temperature schedule') schedule = annealer.auto(state, minutes=6) try: schedule['steps'] = NUMITER except: pass # just keep the auto one print( '---\nAnnealing from %.2f to %.2f over %i steps:' % (schedule['tmax'], schedule['tmin'], schedule['steps'])) state, e = annealer.anneal(state, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=6) print("Reserve cost = %r" % reserve_energy(state)) state.sort() for watershed in state: print("\t", watershed, watersheds[watershed]) return state, reserve_energy(state), schedule
def run(schedule=None): state = [] def reserve_move(state): """ Select random watershed then Add watershed (if not already in state) OR remove it. * This is the Marxan technique as well """ huc = random.choice(hucs) if huc in state: state.remove(huc) else: state.append(huc) def reserve_energy(state): """ The "Objective Function"... Calculates the 'energy' of the reserve. Should incorporate costs of reserve and penalties for not meeting species targets. Note: This example is extremely simplistic compared to the Marxan objective function (see Appendix B in Marxan manual) but at least we have access to it! """ # Initialize variables energy = 0 totals = {} for fish in species: totals[fish] = 0 # Get total cost and habitat in current state for huc in state: watershed = watersheds[huc] # Sum up total habitat for each fish for fish in species: if energy == 0: # reset for new calcs ie first watershed totals[fish] = float(watershed[fish]) else: # add for additional watersheds totals[fish] += float(watershed[fish]) # Incorporate Cost of including watershed energy += watershed["total_cost"] # incorporate penalties for missing species targets for fish in species: if run_target[fish] > 0: pct = totals[fish] / run_target[fish] else: pct = 1 if pct < 1.0: # if missed target, ie total < target if pct < 0.1: # Avoid zerodivision errors # Limits the final to 10x specified penalty pct = 0.1 penalty = int(penalties[fish] / pct) energy += penalty return energy annealer = Annealer(reserve_energy, reserve_move) if schedule is None: print "----\nAutomatically determining optimal temperature schedule" schedule = annealer.auto(state, minutes=6) try: schedule["steps"] = NUMITER except: pass # just keep the auto one print "---\nAnnealing from %.2f to %.2f over %i steps:" % (schedule["tmax"], schedule["tmin"], schedule["steps"]) state, e = annealer.anneal(state, schedule["tmax"], schedule["tmin"], schedule["steps"], updates=6) print "Reserve cost = %r" % reserve_energy(state) state.sort() for watershed in state: print "\t", watershed, watersheds[watershed] return state, reserve_energy(state), schedule
system[1] = cvalues[index] elif component == 2: index = random.randrange(0, len(rvalues)) system[2] = rvalues[index] elif component == 3: index = random.randrange(0, len(rvalues)) system[3] = rvalues[index] if __name__ == '__main__': # set up simulated annealing algorithm units = ('F', 'F', u'Ω', u'Ω') # units of the values in the system initial_system = [cvalues[0], cvalues[0], rvalues[0], rvalues[0]] annealer = Annealer(energy, move) schedule = annealer.auto(initial_system, minutes=0.1) # run simulated annealing algorithm and compute properties of the final system final_system, error = annealer.anneal(initial_system, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=100) final_frf = frf(final_system) final_f0 = f0(final_system) final_q = q(final_system) final_vals = [metric_prefix(*s) for s in zip(final_system, units)] print 'Soln: (%s), Remaining Energy: %s' % (', '.join(final_vals), error) # calculate data for graphs
# Valime ühe klaasi suurima võimaliku pikkuse (700 mm) ja muudame käesolevat väärtust vastavalt sellele, n-ö kontrollitult juhuslikult def move(state): i = random.randint(0, 5) if state[i] < 700 and state[i] > 0: state[i] = random.randint(state[i] - 1, state[i] + 1) else: state[i] = random.randint(0, 700) import time start_time = time.time() annealer = Annealer(energy, move) schedule = annealer.auto(state, minutes=5) state, e = annealer.anneal(state, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=1000) state, e = annealer.anneal(state, 10000, 0.1, 100000, updates=30) # Parim klaaside kombinatsioon for i in range(len(state)): print(klaasid[i]) print(state[i]) # Minimeeritava funktsiooni väärtus, mis vastab parimale klaasi kombinatsioonile print('Minimeeritava funktsiooni väärtus:') print(energy(state))
def run(schedule=None): state = [] def reserve_move(state): """ 1st Random choice: Select watershed then Add watershed (if not already in state) OR remove it. This is the Marxan method """ huc = hucs[random.randint(num_hucs)] if huc in state: state.remove(huc) return ('r', huc) else: state.append(huc) return ('a', huc) def reserve_energy(state): """ The "Objective Function"... Calculates the 'energy' of the reserve. Should incorporate costs of reserve and penalties for not meeting species targets. Note: This example is extremely simplistic compared to the Marxan objective function (see Appendix B in Marxan manual) but at least we have access to it! """ energy = 0 totals = {} for huc in state: watershed = watersheds[huc] # Sum up total habitat for each fish for fish in species: print fish if energy == 0: # reset for new calcs totals[fish] = watershed[fish] print fish, totals else: totals[fish] += watershed[fish] print fish, totals # Incorporate Cost of including watershed energy += watershed['total_cost'] # incorporate penalties for missing species targets for fish in species: print totals print targets pct = totals[fish] / targets[fish] if pct < 1.0: # if missed target, ie total < target if pct < 0.1: # Avoid zerodivision errors # Limits the final to 10x specified penalty pct = 0.1 penalty = int(penalties[fish] / pct) energy += penalty return energy annealer = Annealer(reserve_energy, reserve_move) if schedule is None: # Automatically chosen temperature schedule schedule = annealer.auto(state, minutes=0.3) try: schedule['steps'] = NUMITER except: pass # just keep the auto one print '---\nAnnealing from %.2f to %.2f over %i steps:' % (schedule['tmax'], schedule['tmin'], schedule['steps']) state, e = annealer.anneal(state, schedule['tmax'], schedule['tmin'], schedule['steps'], updates=6) print "Reserve cost = %r" % reserve_energy(state) state.sort() for watershed in state: print "\t", watershed, watersheds[watershed] return state, reserve_energy(state), schedule