update without blocking errors
This commit is contained in:
60
mopso.py
60
mopso.py
@@ -1,5 +1,6 @@
|
||||
import random as rd
|
||||
from .particle import Particle
|
||||
import copy
|
||||
|
||||
class MOPSO():
|
||||
def __init__(self, f_weights:list, A_max:float, price_mean:float, price_std:float, capacities:list, 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):
|
||||
@@ -18,16 +19,27 @@ class MOPSO():
|
||||
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
|
||||
self.prices = self.generates_prices(nb_of_ticks,price_mean,price_std) #TODO: Use RTE France prices for random prices generation according to number of ticks
|
||||
self.capacities = capacities
|
||||
|
||||
# Particles of the simulation
|
||||
self.particles = [Particle(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.particles = [
|
||||
Particle(
|
||||
socs=copy.deepcopy(self.socs),
|
||||
times=self.times, # Ajouté ici
|
||||
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 = []
|
||||
self.leader = self.particles[0] # it doesnt matter as the first thing done is choosing a new leader
|
||||
|
||||
for i in range(self.n):
|
||||
self.particles[i].evaluate(self.f_weights, self.prices, self.socs, self.socs_req, self.times)
|
||||
self.particles[i].evaluate(self.prices, self.socs, self.socs_req, self.times)
|
||||
self.update_archive()
|
||||
|
||||
def iterate(self):
|
||||
@@ -78,46 +90,48 @@ class MOPSO():
|
||||
|
||||
# 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 = []
|
||||
# Structure souhaitée : socs[tick][vehicle] pour être cohérent avec self.x[tick][vehicle]
|
||||
socs = [[0.0 for _ in range(nb_vehicles)] for _ in range(nb_of_ticks)]
|
||||
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)
|
||||
for i in range(nb_vehicles):
|
||||
soc_init = rd.randrange(0, 100, 1)
|
||||
soc_req = rd.randrange(soc_init + 1, 101, 1)
|
||||
|
||||
# Remplissage de la matrice 2D
|
||||
for tick in range(nb_of_ticks):
|
||||
socs[tick][i] = soc_init / 100.0
|
||||
|
||||
socs_req.append(soc_req / 100.0)
|
||||
|
||||
# Adding the requested state of charge
|
||||
socs_req.append(soc_req/100)
|
||||
return socs, socs_req
|
||||
|
||||
# True if a dominates b, else false
|
||||
def dominates(a:Particle, b:Particle):
|
||||
dominates = (a.f_current[0] >= b.f_current[0]) and (a.f_current[1] >= b.f_current[1]) and (a.f_current[2] >= b.f_current[2])
|
||||
def dominates(self, a:Particle, b:Particle):
|
||||
dominates = (a.f_current[0] <= b.f_current[0]) and (a.f_current[1] <= b.f_current[1]) and (a.f_current[2] <= b.f_current[2])
|
||||
if dominates:
|
||||
# Not strict superiority yet
|
||||
dominates = (a.f_current[0] > b.f_current[0]) or (a.f_current[1] > b.f_current[1]) or (a.f_current[2] > b.f_current[2])
|
||||
dominates = (a.f_current[0] < b.f_current[0]) or (a.f_current[1] < b.f_current[1]) or (a.f_current[2] < b.f_current[2])
|
||||
return dominates
|
||||
|
||||
|
||||
def update_archive(self):
|
||||
candidates = self.archive + self.particles
|
||||
length = len(candidates)
|
||||
|
||||
non_dominated = []
|
||||
|
||||
for i in range(length):
|
||||
candidate_i = candidates[i]
|
||||
dominates = True
|
||||
is_dominated = False
|
||||
|
||||
for j in range(length):
|
||||
if i!=j:
|
||||
if i != j:
|
||||
candidate_j = candidates[j]
|
||||
dominates = dominates and self.dominates(candidate_i, candidate_j)
|
||||
if dominates:
|
||||
if self.dominates(candidate_j, candidate_i):
|
||||
is_dominated = True
|
||||
break
|
||||
if not is_dominated:
|
||||
non_dominated.append(candidate_i)
|
||||
|
||||
# Keeping only a certain number of solutions depending on archive_size (to avoid overloading the number of potential directions for particles)
|
||||
if len(non_dominated) > self.archive_size:
|
||||
final_non_dominated = []
|
||||
while len(final_non_dominated) < self.archive_size:
|
||||
|
||||
Reference in New Issue
Block a user