Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import numpy as np
- import pyswarms as ps
- # Define voltage functions
- def resistance_voltage(current, resistance):
- u_R = current/resistance
- return u_R
- def diode_voltage(current):
- temp_volt = 25.6*10**(-3)
- rev_current = 9.4*10**(-15)
- u_D = temp_volt * np.log(current/rev_current)
- return u_D
- # Define optimization parameters and cost function
- options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}
- bounds = (np.array([0]), np.array([50]))
- def cost_function(X):
- n_particles = X.shape[0]
- target_voltage = 5
- resistance = 10
- cost = np.array([
- target_voltage
- - resistance_voltage(X[i], resistance)
- - diode_voltage(X[i])
- for i in range(n_particles)])
- print(cost)
- return cost
- optimizer = ps.single.GlobalBestPSO(n_particles=8, dimensions=1, options=options, bounds=bounds)
- cost, curr = optimizer.optimize(cost_function, print_step=100, iters=1000, verbose=3)
- print(cost, curr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement