forked from KuMiShi/Optim_Metaheuristique
MOPSO refactor (2/2)
This commit is contained in:
7
mopso.py
7
mopso.py
@@ -45,6 +45,7 @@ class MOPSO():
|
||||
|
||||
# Evaluating particles
|
||||
self.particles[i].evaluate(self.f_weights, self.prices, self.socs, self.socs_req, self.times)
|
||||
self.particles[i].update_best()
|
||||
|
||||
# Update the archive
|
||||
self.update_archive()
|
||||
@@ -94,7 +95,11 @@ class MOPSO():
|
||||
|
||||
# True if a dominates b, else false
|
||||
def dominates(a:Particle, b:Particle):
|
||||
dominates = False
|
||||
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])
|
||||
return dominates
|
||||
|
||||
|
||||
def update_archive(self):
|
||||
|
||||
24
particle.py
24
particle.py
@@ -1,11 +1,12 @@
|
||||
import random as rd
|
||||
|
||||
class Particle():
|
||||
def __init__(self, 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, 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
|
||||
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.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)
|
||||
|
||||
# Minima and maxima of a position value
|
||||
self.x_min = x_min
|
||||
@@ -23,9 +24,8 @@ class Particle():
|
||||
self.p_best = self.x # Best known position (starting with initial position x)
|
||||
|
||||
# Evalution attributes
|
||||
self.f_memory = [0,0,0]
|
||||
self.eval = 0
|
||||
# Initial evaluation in MOPSO
|
||||
self.f_best = [(self.nb_of_ticks * self.nb_vehicles * self.x_max * 100) for _ in range(3)]# Hundred times the max grid power should be large enough to be out of scope (equivalent to inf)
|
||||
self.f_current = [0,0,0] # [f1,f2,f3]
|
||||
|
||||
|
||||
def update_position(self):
|
||||
@@ -62,6 +62,11 @@ class Particle():
|
||||
if self.x[tick][i] > 0:
|
||||
self.x[tick][i] = self.x[tick][i] * 0.9
|
||||
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):
|
||||
pos = []
|
||||
@@ -104,7 +109,16 @@ class Particle():
|
||||
|
||||
# Updating the previous evaluation
|
||||
self.f_memory = memory
|
||||
self.eval = f
|
||||
self.f_current = f
|
||||
|
||||
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])
|
||||
if current_better:
|
||||
# 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])
|
||||
if current_dominates:
|
||||
self.p_best = self.x
|
||||
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)
|
||||
def f1(self,elec_prices):
|
||||
|
||||
Reference in New Issue
Block a user