forked from KuMiShi/Optim_Metaheuristique
87 lines
3.9 KiB
Python
87 lines
3.9 KiB
Python
import random as rd
|
|
from .particle import Particle
|
|
|
|
class MOPSO():
|
|
def __init__(self, f_weigths:list, A_max:float, price_mean:float, price_std:float, n:int, t:int, w:float, c1:float, c2:float, archive_size:int=10, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=72, x_min=-100, x_max=100, v_alpha=0.1, surrogate=False):
|
|
# Constants
|
|
self.n = n # Number of particles
|
|
self.t = t # Number of simulation iterations
|
|
self.w = w # Inertia (for exploration)
|
|
self.c1 = c1 # Individual trust
|
|
self.c2 = c2 # Social trust
|
|
self.archive_size = archive_size # Archive size
|
|
self.f_weigths = f_weigths # Weigths for aggregation of all function objective
|
|
|
|
self.surrogate = surrogate # Using AI calculation
|
|
|
|
# Initialisation of particle's global parameters
|
|
self.A_max = A_max # Network's power limit
|
|
self.socs, self.socs_req = self.generate_state_of_charges(nb_vehicles,nb_of_ticks)
|
|
self.times = self.generate_times(nb_vehicles, nb_of_ticks, delta_t)
|
|
self.prices = self.generates_prices(price_mean,price_std) #TODO: Use RTE France prices for random prices generation according to number of ticks
|
|
|
|
# Particles of the simulation
|
|
self.particles = [Particle(times=self.times,nb_vehicles=nb_vehicles, nb_of_ticks=nb_of_ticks, delta_t=delta_t, x_min=x_min, x_max=x_max, alpha=v_alpha) for _ in range(self.n)]
|
|
self.archive = []
|
|
|
|
def iterate(self):
|
|
nb_iter = 0
|
|
if not self.surrogate:
|
|
while nb_iter < self.t:
|
|
nb_iter += 1
|
|
# Selection of a leader
|
|
# Updating velocity and positions
|
|
# Checking boundaries
|
|
# Evaluating particles
|
|
# Update the archive
|
|
# Checking for best positions
|
|
else:
|
|
while nb_iter < self.t:
|
|
nb_iter += 1
|
|
# Selection of a leader
|
|
# Updating velocity and positions
|
|
# Checking boundaries
|
|
# Evaluating particles
|
|
# Update the archive
|
|
# Checking for best positions
|
|
|
|
# Generation of arriving and leaving times for every vehicle
|
|
def generate_times(self, nb_vehicles, nb_of_ticks, delta_t):
|
|
times = []
|
|
for _ in range(nb_vehicles):
|
|
# Minumun, we have one tick of charging/discharging during simulation
|
|
t_arrived = rd.randrange(0, (nb_of_ticks * delta_t - delta_t) +1, delta_t)
|
|
t_leaving = rd.randrange(t_arrived + delta_t, (nb_of_ticks * delta_t) +1, delta_t)
|
|
times.append((t_arrived,t_leaving))
|
|
return times
|
|
|
|
# Genrates different prices in [mean - std, mean + std] range
|
|
def generates_prices(self,nb_of_ticks:int, mean:float, std:float):
|
|
prices = []
|
|
for _ in range(nb_of_ticks):
|
|
variation = rd.randrange(-(std*10), (std * 10) +1, 1) / 10 # Random float variation
|
|
prices.append(mean + variation)
|
|
return prices
|
|
|
|
# Genrates the coordinated states of charges requested and initial (duplicated initially for other ticks)
|
|
def generate_state_of_charges(self, nb_vehicles:int, nb_of_ticks:int):
|
|
socs = []
|
|
socs_req = []
|
|
# We ensure soc_req is greater than what the soc_init is (percentage transformed into floats)
|
|
for _ in range(nb_vehicles):
|
|
soc_init = rd.randrange(0,100,1)
|
|
soc_req = rd.randrange(soc_init+1, 101,1)
|
|
|
|
# Creating states of charges for each tick in time
|
|
for _ in range(nb_of_ticks):
|
|
socs.append(soc_init/100)
|
|
|
|
# Adding the requested state of charge
|
|
socs_req.append(soc_req/100)
|
|
return socs, socs_req
|
|
|
|
# Random uniform selection for a leader
|
|
def select_leader(self):
|
|
n = len(self.archive) # Archive length
|
|
rd_pos = rd.randrange(0, n, 1)
|
|
return self.archive[rd_pos] |