Initial Commit
This commit is contained in:
1
.python-version
Normal file
1
.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.11
|
||||
6
main.py
Normal file
6
main.py
Normal file
@@ -0,0 +1,6 @@
|
||||
def main():
|
||||
print("Hello from optim-meta!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
38
mopso.py
Normal file
38
mopso.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from .particle import Particle
|
||||
|
||||
class MOPSO():
|
||||
def __init__(self, n, t, w, c1, c2, a_max, surrogate=False):
|
||||
# Constants
|
||||
self.n = n # Number of particles
|
||||
self.t = t # Number of iterations
|
||||
self.w = w # Inertia (for exploration)
|
||||
self.c1 = c1 # Individual trust
|
||||
self.c2 = c2 # Social trust
|
||||
self.a_max = a_max # Archive size
|
||||
|
||||
self.surrogate = surrogate # Using AI calculation
|
||||
|
||||
self.particles = [] # Particles of the simulation
|
||||
# Fonctions objectifs
|
||||
# Limites variables de decision
|
||||
|
||||
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
|
||||
66
particle.py
Normal file
66
particle.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import random as rd
|
||||
|
||||
class Particle():
|
||||
def __init__(self, nb_vehicles:int=10, delta_t:int=60, sim_duration:int=4320, a_min=-100, a_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.sim_duration = sim_duration # max duration and number of updates (multiplied by delta_time)
|
||||
|
||||
self.socs= self.generate_state_of_charges() # States of charge (initial, requested)
|
||||
self.times = self.generate_times() # Times (arrived, leaving)
|
||||
|
||||
# Minima and maxima of a position value
|
||||
self.a_min = a_min
|
||||
self.a_max = a_max
|
||||
|
||||
# Limitation of the velocity
|
||||
self.alpha = alpha
|
||||
self.r1 = [rd.randrange(0,101,1)/100 for _ in range(self.nb_vehicles)] # Variable trust of oneself
|
||||
self.r2 = [rd.randrange(0,101,1)/100 for _ in range(self.nb_vehicles)] # Variable trust of other particles
|
||||
|
||||
# Particle attributes
|
||||
self.x = self.generate_position() # Position Vector (correspond to one solution for the problem)
|
||||
self.v = self.generate_velocity() # Velocity
|
||||
self.p_best = self.x # Best known position (starting with initial position x)
|
||||
|
||||
def update_position(self):
|
||||
for i in range(self.nb_vehicles):
|
||||
new_pos_i = self.x[i] + self.v[i]
|
||||
self.x[i] = new_pos_i
|
||||
|
||||
def update_velocity(self, leader, c1, c2, w=0.4):
|
||||
for i in range(self.nb_vehicles):
|
||||
new_vel_i = w * self.v[i] + (self.p_best - self.x[i]) * c1 * self.r1[i] + (leader - self.x[i]) * c2 * self.r2[i]
|
||||
self.v[i] = new_vel_i
|
||||
|
||||
def generate_state_of_charges(self):
|
||||
socs = []
|
||||
# We ensure soc_req is greater than what the soc_init is (percentage transformed into floats)
|
||||
for _ in range(self.nb_vehicles):
|
||||
soc_init = rd.randrange(0,100,1)
|
||||
soc_req = rd.randrange(soc_init+1, 101,1)
|
||||
socs.append((soc_init/100, soc_req/100))
|
||||
return socs
|
||||
|
||||
def generate_times(self):
|
||||
times = []
|
||||
for _ in range(self.nb_vehicles):
|
||||
# Minumun, we have one tick of charging during simulation
|
||||
t_arrived = rd.randrange(0, (self.sim_duration - self.delta_time) +1, self.delta_time)
|
||||
t_leaving = rd.randrange(t_arrived + self.delta_time, self.sim_duration+1, self.delta_time)
|
||||
times.append((t_arrived,t_leaving))
|
||||
return times
|
||||
|
||||
def generate_position(self):
|
||||
pos = []
|
||||
for _ in range(self.nb_vehicles):
|
||||
pos.append(rd.randrange(self.a_min, self.a_max +1, 1))
|
||||
return pos
|
||||
|
||||
def generate_velocity(self):
|
||||
vel = []
|
||||
vel_coeff = self.a_max - self.a_min
|
||||
for _ in range(self.nb_vehicles):
|
||||
vel.append(rd.randrange(-vel_coeff, vel_coeff +1, 1) * self.alpha)
|
||||
return vel
|
||||
7
pyproject.toml
Normal file
7
pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "optim-meta"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = []
|
||||
Reference in New Issue
Block a user