Ajout demonstration final
This commit is contained in:
parent
5002bba5a2
commit
24965b27e7
2 changed files with 90 additions and 51 deletions
35
demo_boucle.py
Normal file
35
demo_boucle.py
Normal 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)
|
|
@ -7,11 +7,7 @@ import time
|
||||||
from typing import Any, Callable, Iterable, Mapping, Optional
|
from typing import Any, Callable, Iterable, Mapping, Optional
|
||||||
|
|
||||||
import numpy as np
|
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.helperclient import HelperClient
|
||||||
from coapthon.client.superviseur import (SuperviseurGlobal,
|
from coapthon.client.superviseur import (SuperviseurGlobal,
|
||||||
|
@ -46,62 +42,70 @@ class RequettePeriodique(threading.Thread):
|
||||||
else:
|
else:
|
||||||
raise ValueError
|
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):
|
self.clients = clients
|
||||||
def __init__(self, clients: Iterable[HelperClient], superviseur_local_type: type, superviseur_global_type: type, request_path: str, args_reward: Iterable[Any] = (),
|
self.super_g = superviseur_global_type(clients, superviseur_local_type)
|
||||||
control_period: float = 30, request_period: Iterable[float] = None) -> None:
|
|
||||||
|
|
||||||
self.clients = clients
|
self._action_spec = array_spec.BoundedArraySpec(
|
||||||
self.super_g = superviseur_global_type(clients, superviseur_local_type)
|
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(
|
self._args_reward = args_reward
|
||||||
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
|
if request_period is None:
|
||||||
|
request_period = [5 for client in clients]
|
||||||
|
|
||||||
if request_period is None:
|
self.requests = [RequettePeriodique(client, request_period[n], request_path, name="Spamer {}".format(
|
||||||
request_period = [5 for client in clients]
|
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(
|
@property
|
||||||
n)) for n, client in enumerate(clients)]
|
def request_period(self):
|
||||||
[request.start() for request in self.requests]
|
return [request.period for request in self.requests]
|
||||||
|
|
||||||
@property
|
def action_spec(self) -> array_spec.BoundedArraySpec:
|
||||||
def request_period(self):
|
return self._action_spec
|
||||||
return [request.period for request in self.requests]
|
|
||||||
|
|
||||||
def action_spec(self) -> array_spec.BoundedArraySpec:
|
def observation_spec(self) -> array_spec.BoundedArraySpec:
|
||||||
return self._action_spec
|
return self._observation_spec
|
||||||
|
|
||||||
def observation_spec(self) -> array_spec.BoundedArraySpec:
|
def _reset(self) -> None:
|
||||||
return self._observation_spec
|
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:
|
def _step(self, action: Iterable[float]):
|
||||||
etat = np.zeros(
|
self.super_g.application_action(action)
|
||||||
(3, len(self.clients)), dtype=np.float32)
|
self.super_g.reset()
|
||||||
self._current_time_step = etat
|
|
||||||
self.super_g.reset_rto()
|
|
||||||
return ts.transition(etat, reward=0)
|
|
||||||
|
|
||||||
def _step(self, action: Iterable[float]):
|
time.sleep(self.control_period)
|
||||||
self.super_g.application_action(action)
|
|
||||||
self.super_g.reset()
|
|
||||||
|
|
||||||
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
|
except ImportError :
|
||||||
if self._args_reward == ():
|
print("Pas de fonctionalité d'apprentissage")
|
||||||
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)
|
|
Loading…
Reference in a new issue