Adding ticks to particles

This commit is contained in:
KuMiShi
2026-01-13 17:33:37 +01:00
parent 83cf163939
commit a6f84df680
2 changed files with 60 additions and 9 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
# Scripts
main.py
# UV Environment
.python-version
.venv

View File

@@ -1,13 +1,15 @@
import random as rd import random as rd
class Particle(): 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): def __init__(self, nb_vehicles:int=10, delta_t:int=60, nb_of_ticks:int=72, a_min=-100, a_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.sim_duration = sim_duration # max duration and number of updates (multiplied by delta_time) self.nb_of_ticks = nb_of_ticks # Accounting for time evolution of the solution (multiplied by delta_t)
self.socs= self.generate_state_of_charges() # States of charge (initial, requested) self.socs= self.generate_state_of_charges() # States of charge (initial, requested)
#TODO: Move that to MOPSO (using one batch of times for multiples particles)
self.times = self.generate_times() # Times (arrived, leaving) self.times = self.generate_times() # Times (arrived, leaving)
# Minima and maxima of a position value # Minima and maxima of a position value
@@ -23,6 +25,7 @@ class Particle():
self.x = self.generate_position() # Position Vector (correspond to one solution for the problem) self.x = self.generate_position() # Position Vector (correspond to one solution for the problem)
self.v = self.generate_velocity() # Velocity self.v = self.generate_velocity() # Velocity
self.p_best = self.x # Best known position (starting with initial position x) self.p_best = self.x # Best known position (starting with initial position x)
self.eval = 0 #TODO: self.evaluate()
def update_position(self): def update_position(self):
for i in range(self.nb_vehicles): for i in range(self.nb_vehicles):
@@ -47,20 +50,62 @@ class Particle():
times = [] times = []
for _ in range(self.nb_vehicles): for _ in range(self.nb_vehicles):
# Minumun, we have one tick of charging during simulation # 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_arrived = rd.randrange(0, (self.nb_of_ticks * self.delta_t - self.delta_t) +1, self.delta_t)
t_leaving = rd.randrange(t_arrived + self.delta_time, self.sim_duration+1, self.delta_time) t_leaving = rd.randrange(t_arrived + self.delta_t, (self.nb_of_ticks*self.delta_t)+1, self.delta_t)
times.append((t_arrived,t_leaving)) times.append((t_arrived,t_leaving))
return times return times
#TODO: Modify for uses of ticks
def generate_position(self): def generate_position(self):
pos = [] pos = []
for _ in range(self.nb_vehicles): for _ in range(self.nb_of_ticks):
pos.append(rd.randrange(self.a_min, self.a_max +1, 1)) x_tick = []
for _ in range(self.nb_vehicles):
x_tick.append(rd.randrange(self.a_min, self.a_max +1, 1))
pos.append(x_tick)
return pos return pos
def generate_velocity(self): def generate_velocity(self):
vel = [] vel = []
vel_coeff = self.a_max - self.a_min vel_coeff = self.a_max - self.a_min
for _ in range(self.nb_vehicles): for _ in range(self.nb_of_ticks):
vel.append(rd.randrange(-vel_coeff, vel_coeff +1, 1) * self.alpha) v_tick = []
return vel for _ in range(self.nb_vehicles):
v_tick.append(rd.randrange(-vel_coeff, vel_coeff +1, 1) * self.alpha)
vel.append(v_tick)
return vel
# Function objective
def evaluate(self, elec_prices, max_power):
pass
# 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):
result = 0
for tick in range(self.nb_of_ticks):
grid_stress_tick = self.get_current_grid_stress(tick)
result += elec_prices[tick] * grid_stress_tick * self.delta_t
return result
#TODO: Modify for uses of ticks
# User's insatisfaction
def f2(self):
result = 0
for i in range(self.nb_vehicles):
soc_req_i = self.socs[i][1]
result += max(0, )
# Network Stress
def f3(self):
current_max = 0
for tick in range(self.nb_of_ticks):
current_max = max(current_max, self.get_current_grid_stress(tick))
return current_max
#TODO: Modify for uses of ticks
def get_current_grid_stress(self, tick:int):
assert tick < self.nb_of_ticks # Make sure the tick exist in the position x
current_grid_stress = 0
for i in range(self.nb_vehicles):
current_grid_stress += self.x[tick][i]
return current_grid_stress