def func(x, ivc): """ This function calculate scores for parameters of swarms and need curve :param x: population of swarms :return: array of score """ # global c # c += 1 global time_func_0, time_func_1 # time_func_0 = time.clock() result = [] for i in range(len(x)): # ivc_1 = get_ivc("3_win") time_func_0 = time.clock() create_cir('test', x[i]) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3) analysis = spice.CreateCVC(circuit, input_data, 100) # spice.SaveFile(analysis, "test.csv") # plot(i+c) ivc_2 = [analysis.input_dummy, analysis.VCurrent] result.append(compare_ivc(ivc, ivc_2)) time_func_1 = time.clock() return result
def f(x): global ivc _x = copy.copy(x) # for i in range(len(x)): # _x[i] = x[i] * (1e-6 - 1e-12) + 1e-12 if i < 5 else x[i] * 10e6 create_cir('test', _x) circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3, SNR=10**6) analysis = spice.CreateCVC(circuit, input_data, 100) # spice.SaveFile(analysis, "test.csv") ivc_2 = [analysis.input_dummy, analysis.VCurrent] return compare_ivc(ivc, ivc_2)
def func(x, ivc): """ This function calculate scores for parameters of swarms and need curve :param x: population of swarms :return: array of score """ global time_func_0, time_func_1 # time_func_0 = time.clock() result = [] for i in range(len(x)): time_func_0 = time.clock() create_cir('test', x[i]) circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3, SNR=10**6) analysis = spice.CreateCVC(circuit, input_data, 100) ivc_2 = [analysis.input_dummy, analysis.VCurrent] result.append(compare_ivc(ivc, ivc_2)) time_func_1 = time.clock() return result
def func_x(x): """ This function calculate scores for parameters of swarms and need curve :param x: population of swarms :return: array of score """ global _x, _y, ivc_1 _x = x # _y = y result = [] for i in range(30): z = [] z.extend(x[i]) z.extend(_y[i]) create_cir('test', z) # logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3) analysis = spice.CreateCVC(circuit, input_data, 100) ivc_2 = [analysis.input_dummy, analysis.VCurrent] result.append(compare_ivc(ivc_1, ivc_2)) return result
def func_x(x, y=None, ivc=None): """ This function calculate scores for parameters of swarms and need curve :param x: population of swarms :return: array of score """ global time_func_0, time_func_1, _x, _y _x = x # time_func_0 = time.clock() result = [] for i in range(len(x)): time_func_0 = time.clock() create_cir('test', x[i][:]*y[i][:]) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3) analysis = spice.CreateCVC(circuit, input_data, 100) ivc_2 = [analysis.input_dummy, analysis.VCurrent] result.append(compare_ivc(ivc, ivc_2)) time_func_1 = time.clock() return result
options = {'c1': 0.8, 'c2': 0.6, 'w': 0.6} logger = Logging.setup_logging() for i in range(215): array_of_resistor = np.random.randint(0, 10**6, 8) array_of_capacitor = np.random.randint(0, 6, 5) + 6 init_pos[i][:5] = 1/(10 ** array_of_capacitor[:]) init_pos[i][5:] = array_of_resistor[:] * cases[i][5:] for j in range(5): init_pos[i][j] = init_pos[i][j] if cases[i][j] == 1 else 10 ** -12 best_cost = 1 for i in range(5): clamp = gen_velocity_clamps(12-i, 0) print(clamp) optimizer = ps.single.GlobalBestPSO(n_particles=215, dimensions=13, bounds=bounds, options=options, velocity_clamp=clamp, init_pos=init_pos) cost, pos = optimizer.optimize(func, iters=12, ivc=ivc_1) if cost < best_cost: best_cost = cost best_pos = pos print("\n", best_cost, best_pos) create_cir('test', pos) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3, SNR=10**6) analysis = spice.CreateCVC(circuit, input_data, 100) spice.SaveFile(analysis, "test.csv") plot('result', '3_win')
_y = gen_init_pos(30,array_of_resistor, array_of_capacitor) _x[:] = 1 _x[5] = 0 while True: if iter == 0: optimizer_disc = ps.discrete.binary.BinaryPSO(n_particles=30, dimensions=13, options=options) else: optimizer_disc = ps.discrete.binary.BinaryPSO(n_particles=30, dimensions=13, options=options, init_pos=_x) cost, pos_d = optimizer_disc.optimize(func_x, iters=1, ivc=ivc_1, y=_y) optimizer_cont = ps.single.GlobalBestPSO(n_particles=30, dimensions=13, options=options, bounds=bounds, init_pos=_y, #velocity_clamp=clamp ) cost, pos_c = optimizer_cont.optimize(func_y, iters=1, ivc=ivc_1, x=_x) iter += 1 print(cost, pos_d, pos_c) create_cir('test', pos_d * pos_c) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3) analysis = spice.CreateCVC(circuit, input_data, 100) spice.SaveFile(analysis, "test.csv") plot('result'+str(iter), '3_win') none_error += 1 if (cost-prev_cost) ** 2 < 10 ** -12 else 0 if none_error > 5 or iter > 1000: break # time_opt_1 = time.clock() # # print(time_func_1 - time_func_0) # print("all optimization") # print((time_opt_1 - time_opt_0)/60)
_y[i] = np.random.randint(0, 10**5, 8) _x[i] = np.random.uniform(10**-12, 10**-6, 5) while True: optimizer_x = ps.single.GlobalBestPSO(n_particles=30, dimensions=5, options=options, init_pos=_x, bounds=bounds_x) cost, pos_x = optimizer_x.optimize(func_x, iters=1) optimizer_y = pyswarms.discrete.binary.BinaryPSO(n_particles=30, dimensions=8, options=options, init_pos=_y) # bounds=bounds_y) cost, pos_y = optimizer_y.optimize(func_y, iters=1) iter += 1 print(cost, pos_x, pos_y) z = [] z.extend(pos_x) z.extend(pos_y) create_cir('test', z) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3) analysis = spice.CreateCVC(circuit, input_data, 100) spice.SaveFile(analysis, "test.csv") plot('result'+str(iter), '3_win') none_error += 1 if (cost-prev_cost) ** 2 < 10 ** -12 else 0 if none_error > 5 or iter > 1000: break # time_opt_1 = time.clock() # # print(time_func_1 - time_func_0) # print("all optimization") # print((time_opt_1 - time_opt_0)/60) # y = []
clamp = gen_velocity_clamps(cases) logger = Logging.setup_logging() for step in range(2): init_pos = gen_init_pos(cases, array_of_resistor, array_of_capacitor, step) optimizer = ps.single.GlobalBestPSO(n_particles=2 * len(cases), dimensions=13, bounds=bounds, options=options, velocity_clamp=clamp, init_pos=init_pos) cost, pos = optimizer.optimize(func, iters=28, ivc=ivc_1) costs.append(cost) poses.append(pos) # clamps.append(clamp) minpos = poses[np.argmin(costs)] # minclamp = clamps[np.argmin(costs)] time_opt_1 = time.clock() print(pos, cost) print(time_func_1 - time_func_0) print("all optimization") print((time_opt_1 - time_opt_0) / 60) create_cir('test', minpos) logger = Logging.setup_logging() circuit = spice.LoadFile('test.cir') input_data = spice.Init_Data(1000, 0.3, SNR=10**6) analysis = spice.CreateCVC(circuit, input_data, 100) spice.SaveFile(analysis, "test.csv") plot('result', '3_win')