forked from KuMiShi/Optim_Metaheuristique
update without blocking errors
This commit is contained in:
36
particle.py
36
particle.py
@@ -1,12 +1,14 @@
|
|||||||
import random as rd
|
import random as rd
|
||||||
|
import copy
|
||||||
|
|
||||||
class Particle():
|
class Particle():
|
||||||
def __init__(self,socs:list, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=72, x_min=-100, x_max=100, alpha=0.1):
|
def __init__(self, socs:list, times:list, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=72, x_min=-100, x_max=100, alpha=0.1):
|
||||||
# Problem specific attributes
|
# Problem specific attributes
|
||||||
self.nb_vehicles = nb_vehicles # Number of vehicles handles for the generations of position x
|
self.nb_vehicles = nb_vehicles # Number of vehicles handles for the generations of position x
|
||||||
self.delta_t = delta_t # delta_t for update purposes
|
self.delta_t = delta_t # delta_t for update purposes
|
||||||
self.nb_of_ticks = nb_of_ticks # Accounting for time evolution of the solution (multiplied by delta_t)
|
self.nb_of_ticks = nb_of_ticks # Accounting for time evolution of the solution (multiplied by delta_t)
|
||||||
self.socs = socs # States of charges for the particle current position (self.x)
|
self.socs = socs # States of charges for the particle current position (self.x)
|
||||||
|
self.times = times
|
||||||
|
|
||||||
# Minima and maxima of a position value
|
# Minima and maxima of a position value
|
||||||
self.x_min = x_min
|
self.x_min = x_min
|
||||||
@@ -62,11 +64,8 @@ class Particle():
|
|||||||
if self.x[tick][i] > 0:
|
if self.x[tick][i] > 0:
|
||||||
self.x[tick][i] = self.x[tick][i] * 0.9
|
self.x[tick][i] = self.x[tick][i] * 0.9
|
||||||
current_power = self.get_current_grid_stress(tick)
|
current_power = self.get_current_grid_stress(tick)
|
||||||
|
|
||||||
def update_socs(self, capacities):
|
|
||||||
for tick in range(self.nb_of_ticks):
|
|
||||||
for i in range(self.nb_vehicles-1):
|
|
||||||
self.socs[tick][i+1] = self.socs[tick][i] + (self.x[tick][i] / capacities[i])
|
|
||||||
|
|
||||||
def generate_position(self):
|
def generate_position(self):
|
||||||
pos = []
|
pos = []
|
||||||
@@ -91,7 +90,7 @@ class Particle():
|
|||||||
# Function objective
|
# Function objective
|
||||||
def evaluate(self,elec_prices,socs,socs_req,times):
|
def evaluate(self,elec_prices,socs,socs_req,times):
|
||||||
f1 = self.f1(elec_prices)
|
f1 = self.f1(elec_prices)
|
||||||
f2 = self.f2(socs,socs_req,times)
|
f2 = self.f2(self.socs,socs_req,times)
|
||||||
f3 = self.f3()
|
f3 = self.f3()
|
||||||
|
|
||||||
# Keeping in memory evaluation of each objective for domination evaluation
|
# Keeping in memory evaluation of each objective for domination evaluation
|
||||||
@@ -103,13 +102,13 @@ class Particle():
|
|||||||
self.f_current = f_current
|
self.f_current = f_current
|
||||||
|
|
||||||
def update_best(self):
|
def update_best(self):
|
||||||
current_better = (self.f_current[0] >= self.f_best[0]) and (self.f_current[1] >= self.f_best[1]) and (self.f_current[2] >= self.f_best[2])
|
current_better = (self.f_current[0] <= self.f_best[0]) and (self.f_current[1] <= self.f_best[1]) and (self.f_current[2] <= self.f_best[2])
|
||||||
if current_better:
|
if current_better:
|
||||||
# Not strict superiority yet
|
# Not strict superiority yet
|
||||||
current_dominates = (self.f_current[0] > self.f_best[0]) or (self.f_current[1] > self.f_best[1]) or (self.f_current[2] > self.f_best[2])
|
current_dominates = (self.f_current[0] < self.f_best[0]) or (self.f_current[1] < self.f_best[1]) or (self.f_current[2] < self.f_best[2])
|
||||||
if current_dominates:
|
if current_dominates:
|
||||||
self.p_best = self.x
|
self.p_best = copy.deepcopy(self.x)
|
||||||
self.f_best = self.f_current
|
self.f_best = self.f_current[:]
|
||||||
|
|
||||||
# Calculate the price of the electricity consumption in the grid SUM(1_to_T)(Epsilon_t * A_t * delta_t)
|
# Calculate the price of the electricity consumption in the grid SUM(1_to_T)(Epsilon_t * A_t * delta_t)
|
||||||
def f1(self,elec_prices):
|
def f1(self,elec_prices):
|
||||||
@@ -142,5 +141,16 @@ class Particle():
|
|||||||
current_grid_stress += self.x[tick][i]
|
current_grid_stress += self.x[tick][i]
|
||||||
return current_grid_stress
|
return current_grid_stress
|
||||||
|
|
||||||
def updating_socs(self, socs, capacities):
|
def updating_socs(self, initial_socs, capacities):
|
||||||
pass
|
|
||||||
|
# Calcul de l'évolution temporelle
|
||||||
|
for tick in range(self.nb_of_ticks - 1): # On s'arrête à l'avant-dernier pour calculer le suivant
|
||||||
|
for i in range(self.nb_vehicles):
|
||||||
|
# SoC(t+1) = SoC(t) + (Puissance(t) * delta_t / Capacité)
|
||||||
|
energy_added = (self.x[tick][i] * (self.delta_t / 60))
|
||||||
|
|
||||||
|
# Mise à jour du tick suivant basé sur le tick actuel
|
||||||
|
# On utilise initial_socs comme base si c'est une liste de listes [tick][vehicule]
|
||||||
|
self.socs[tick+1][i] = self.socs[tick][i] + (energy_added / capacities[i])
|
||||||
|
|
||||||
|
self.socs[tick+1][i] = max(0.0, min(1.0, self.socs[tick+1][i]))
|
||||||
Reference in New Issue
Block a user