Ajout demonstration final

This commit is contained in:
LéopoldClement 2021-12-02 13:44:40 +01:00
parent 5002bba5a2
commit 24965b27e7
2 changed files with 90 additions and 51 deletions

35
demo_boucle.py Normal file
View file

@ -0,0 +1,35 @@
#! /bin/python3
import socket
import time
import numpy as np
from coapthon.client.helperclient import HelperClient
from coapthon.client.superviseur import (SuperviseurGlobal,
SuperviseurLocalFiltre)
from coapthon.utils import parse_uri
from utils_learning import RequettePeriodique
host, port, path = parse_uri("coap://polaris.kokarde.fr/basic")
try:
tmp = socket.gethostbyname(host)
host = tmp
except socket.gaierror:
pass
nombreCapteur = 25
periodeRequette = 1
periodeControl = 15
clients = [HelperClient(server=(host, port)) for _ in range(nombreCapteur)]
super_g = SuperviseurGlobal(clients, SuperviseurLocalFiltre)
requests = [RequettePeriodique(client, periodeRequette, path, name="Spamer {}".format(
n)) for n, client in enumerate(clients)]
[request.start() for request in requests]
for _ in range(10):
super_g.reset()
time.sleep(periodeControl)
print(super_g.state)

View file

@ -7,11 +7,7 @@ import time
from typing import Any, Callable, Iterable, Mapping, Optional
import numpy as np
import tensorflow as tf
from tf_agents.environments import (py_environment, tf_environment,
tf_py_environment, utils, wrappers)
from tf_agents.specs import array_spec
from tf_agents.trajectories import time_step as ts
from coapthon.client.helperclient import HelperClient
from coapthon.client.superviseur import (SuperviseurGlobal,
@ -46,62 +42,70 @@ class RequettePeriodique(threading.Thread):
else:
raise ValueError
try :
import tensorflow as tf
from tf_agents.environments import (py_environment, tf_environment,
tf_py_environment, utils, wrappers)
from tf_agents.specs import array_spec
from tf_agents.trajectories import time_step as ts
class MaquetteCoapEnv(py_environment.PyEnvironment):
def __init__(self, clients: Iterable[HelperClient], superviseur_local_type: type, superviseur_global_type: type, request_path: str, args_reward: Iterable[Any] = (),
control_period: float = 30, request_period: Iterable[float] = None) -> None:
class MaquetteCoapEnv(py_environment.PyEnvironment):
def __init__(self, clients: Iterable[HelperClient], superviseur_local_type: type, superviseur_global_type: type, request_path: str, args_reward: Iterable[Any] = (),
control_period: float = 30, request_period: Iterable[float] = None) -> None:
self.clients = clients
self.super_g = superviseur_global_type(clients, superviseur_local_type)
self.clients = clients
self.super_g = superviseur_global_type(clients, superviseur_local_type)
self._action_spec = array_spec.BoundedArraySpec(
shape=(len(clients),), dtype=np.float32, minimum=-10, maximum=10, name='action')
self._observation_spec = array_spec.BoundedArraySpec(
shape=(superviseur_global_type.nombre_mesure, len(clients)), dtype=np.float32, minimum=0, name='observation')
self._episode_ended = False
self._current_time_step = np.zeros(
(3, len(self.clients)), dtype=np.float32)
self.control_period = control_period
self._action_spec = array_spec.BoundedArraySpec(
shape=(len(clients),), dtype=np.float32, minimum=-10, maximum=10, name='action')
self._observation_spec = array_spec.BoundedArraySpec(
shape=(superviseur_global_type.nombre_mesure, len(clients)), dtype=np.float32, minimum=0, name='observation')
self._episode_ended = False
self._current_time_step = np.zeros(
(3, len(self.clients)), dtype=np.float32)
self.control_period = control_period
self._args_reward = args_reward
self._args_reward = args_reward
if request_period is None:
request_period = [5 for client in clients]
if request_period is None:
request_period = [5 for client in clients]
self.requests = [RequettePeriodique(client, request_period[n], request_path, name="Spamer {}".format(
n)) for n, client in enumerate(clients)]
[request.start() for request in self.requests]
self.requests = [RequettePeriodique(client, request_period[n], request_path, name="Spamer {}".format(
n)) for n, client in enumerate(clients)]
[request.start() for request in self.requests]
@property
def request_period(self):
return [request.period for request in self.requests]
@property
def request_period(self):
return [request.period for request in self.requests]
def action_spec(self) -> array_spec.BoundedArraySpec:
return self._action_spec
def action_spec(self) -> array_spec.BoundedArraySpec:
return self._action_spec
def observation_spec(self) -> array_spec.BoundedArraySpec:
return self._observation_spec
def observation_spec(self) -> array_spec.BoundedArraySpec:
return self._observation_spec
def _reset(self) -> None:
etat = np.zeros(
(3, len(self.clients)), dtype=np.float32)
self._current_time_step = etat
self.super_g.reset_rto()
return ts.transition(etat, reward=0)
def _reset(self) -> None:
etat = np.zeros(
(3, len(self.clients)), dtype=np.float32)
self._current_time_step = etat
self.super_g.reset_rto()
return ts.transition(etat, reward=0)
def _step(self, action: Iterable[float]):
self.super_g.application_action(action)
self.super_g.reset()
def _step(self, action: Iterable[float]):
self.super_g.application_action(action)
self.super_g.reset()
time.sleep(self.control_period)
time.sleep(self.control_period)
etat = self.super_g.state
if self._args_reward == ():
recompense = self.super_g.qualite(5*[1], 1000, 1, 1)
else:
recompense = self.super_g.qualite(5*[1], *self._args_reward)
self._current_time_step = etat
if self.super_g.failed:
return ts.termination(etat, -10000)
else:
return ts.transition(etat, reward=recompense)
etat = self.super_g.state
if self._args_reward == ():
recompense = self.super_g.qualite(5*[1], 1000, 1, 1)
else:
recompense = self.super_g.qualite(5*[1], *self._args_reward)
self._current_time_step = etat
if self.super_g.failed:
return ts.termination(etat, -10000)
else:
return ts.transition(etat, reward=recompense)
except ImportError :
print("Pas de fonctionalité d'apprentissage")