def hooke_jeeves(x0, epsilon, step, alpha): it = 0 xB = x0 a = b = float('inf') while c(x0) > epsilon or it < 1_000: # # warunek stopu if it == 0: a = c(x0) else: a = b b = c(x0) if a == b: break it += 1 xB = x0 x0 = try_procedure(xB, step) if c(x0) < c(xB): while c(x0) >= c(xB): xB_temp = xB xB = x0 x0 = 2 * xB - xB_temp x0 = try_procedure(x0, step) else: step = alpha * step save(c(x0), 0) save(xB, 0) return xB
def hooke_jeeves(x0, epsilon, s, alpha, function): it = 0 xB = x0 a = b = float('inf') while s > epsilon or it < 1_000: if it == 0: a = c(x0, function) else: a = b b = c(x0, function) save(function, c(x0, function)) if a == b: break it += 1 xB = x0 x0 = try_procedure(xB, s, function) if c(x0, function) < c(xB, function): while c(x0, function) >= c(xB, function): xB_temp = xB xB = x0 x0 = 2 * xB - xB_temp x0 = try_procedure(x0, s) else: s = alpha * s return xB
def ivm_calculations(vector_a, Temp, _EPSILON_DOT, time, name): Temp += 273.15 _KROK_CZASOWY = time / _ITERACJE RO_0 = 10_000.0 historia_ro = [RO_0] _Z = _EPSILON_DOT * exp(Q / (R * Temp)) mianownik = pow(_Z, vector_a[12]) _SWOBODNA_DROGA_DYSLOKACJI = vector_a[0] / mianownik _B_L = _BURGERS * _SWOBODNA_DROGA_DYSLOKACJI A1 = 1 / (_B_L) A2 = vector_a[1] * pow(_EPSILON_DOT, (-vector_a[8])) * exp(-vector_a[2] / (R * Temp)) A3 = vector_a[3] * (_TAU / D) * exp(-vector_a[4] / (R * Temp)) RO_CR = -vector_a[10] + vector_a[11] * pow(_Z, vector_a[9]) T_CR = -1 index_of_a3 = 0 for i in range(_ITERACJE): if historia_ro[-1] >= RO_CR and historia_ro[-1] > 0: temp = 1.0 if T_CR == -1: T_CR = i index_of_a3 = i - T_CR last_step = A3*temp * \ pow(historia_ro[-1], vector_a[7])*historia_ro[index_of_a3] else: temp = 0.0 T_CR = -1 index_of_a3 = 0 last_step = 0.0 delta_ro = A1*_EPSILON_DOT - A2 * \ _EPSILON_DOT*historia_ro[-1] - last_step RO_0 += delta_ro * _KROK_CZASOWY historia_ro.append(RO_0) save(RO_0, name) return RO_0
def main(): search_space = s.Space(0.0, epsilon, 500) particles_vector = [ p.Particle() for _ in range(search_space.num_particles) ] search_space.particles = particles_vector search_space.set_pbest() search_space.set_gbest() iteration = 0 n_iterations = 100 val_prev = val_prev_1 = 0.0 while iteration < n_iterations or search_space.gbest_value <= epsilon: search_space.set_pbest() search_space.set_gbest() # JEŚLI PRZEZ TRZY ITERACJE NIE ZMIENI SIE WARTOŚĆ => BREAK if iteration == 0: val_prev = search_space.gbest_value else: val_prev_1 = val_prev val_prev = search_space.gbest_value #print("POŁOŻENIE: ", search_space.gbest_position, " WARTOŚĆ: ", search_space.gbest_value) if abs(search_space.gbest_value - search_space.target ) <= search_space.epsilon or val_prev_1 == val_prev: break search_space.move_particles() iteration += 1 save(search_space.gbest_value, 0) print("Solution: ", search_space.gbest_position, " value ", search_space.gbest_value) file = open("model.txt", 'a') file.write(str(search_space.gbest_position) + " \n") file.close() return search_space.gbest_position
_Z = _EPSILON_DOT * exp(Q / (R * Temp)) _BURGERS = 0.25 * pow(10, -9) _SWOBODNA_DROGA_DYSLOKACJI = vector_a[0] / pow(_Z, vector_a[12]) _B2 = _BURGERS * _BURGERS _TAU = (_KIRCHOFF * _B2 * 10.0**6) / 2 _B_L = _BURGERS * _SWOBODNA_DROGA_DYSLOKACJI A1 = 1 / (_B_L) A2 = vector_a[1] * pow(_EPSILON_DOT, (-vector_a[8])) * exp(-vector_a[2] / (R * Temp)) A3 = vector_a[3] * (_TAU / D) * exp(-vector_a[4] / (R * Temp)) RO_CR = -vector_a[10] + vector_a[11] * pow(_Z, vector_a[9]) T_CR = -1 index_of_a3 = 0 save(RO_0, vector_a[6] + vector_a[5] * _KIRCHOFF * _BURGERS * sqrt(RO_0)) for i in range(_ITERACJE): if historia_ro[-1] >= RO_CR: temp = 1.0 if T_CR == -1: T_CR = i index_of_a3 = i - T_CR else: temp = 0.0 T_CR = -1 index_of_a3 = 0 print(A1, A2, A3) delta_ro = A1 * _EPSILON_DOT - A2 * _EPSILON_DOT * historia_ro[ -1] - A3 * temp * pow(historia_ro[-1], vector_a[7]) * historia_ro[index_of_a3]
# JEŚLI PRZEZ TRZY ITERACJE NIE ZMIENI SIE WARTOŚĆ => BREAK if iteration == 0: val_prev = search_space.gbest_value else: val_prev_1 = val_prev val_prev = search_space.gbest_value #print("POŁOŻENIE: ", search_space.gbest_position, " WARTOŚĆ: ", search_space.gbest_value) if abs(search_space.gbest_value - search_space.target ) <= search_space.epsilon or val_prev_1 == val_prev: break search_space.move_particles() iteration += 1 save(search_space.gbest_value, 0) print("Solution: ", search_space.gbest_position, " value ", search_space.gbest_value) file = open("model.txt", 'a') file.write(str(search_space.gbest_position) + " \n") file.close() return search_space.gbest_position if __name__ == "__main__": vector = main() for i in range(9): T, epsilon_dot = dict_T_epsilon_dot[i] for j in range(21): save(sigma(vector, 0.05 * (j + 1), epsilon_dot, T), i + 1)
from math import log, pow from saveToFile import saveToFile as save # WARTOŚCI A1, A2, A3, RO_CT I A8 PODMIENIAM DLA KAŻDEGO PRZYPADKU A1 = 3.5 A2 = 5.0 A3 = 1 epsilon_dot = 1.0 ro_0 = 0.0 ro_cr = 0.4 a8 = 0.0 number_of_steps = 1000 historia_ro = [0.0] t_cr = (1/A2) * log((ro_0-(A1/A2))/(ro_cr-(A1/A2))) t_cr_step = int(t_cr*number_of_steps) save(ro_0) for i in range(number_of_steps): if i < t_cr_step: temp = 0.0 index = 0 else: temp = 1.0 index = i-t_cr_step delta = A1*epsilon_dot - A2*historia_ro[-1]*epsilon_dot - A3*pow(historia_ro[-1], a8)*temp*historia_ro[index] ro_0 += delta*(1/number_of_steps) historia_ro.append(ro_0) save(ro_0)