Blame view

src/core/pyros_django/agent/AgentX.py 4.58 KB
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
1
#!/usr/bin/env python3
eac3e2ab   Etienne Pallier   New AgentX
2

a7887020   Etienne Pallier   GROSSE OPTIM : pl...
3
4
5
6
import sys
##import utils.Logger as L
#import threading #, multiprocessing, os
#import time
af5d8b11   Etienne Pallier   Nouveau shell pyr...
7

a7887020   Etienne Pallier   GROSSE OPTIM : pl...
8
9
#from django.db import transaction
#from common.models import Command
30b22ee6   Etienne Pallier   Agent : agentX wo...
10

5fd52e37   Etienne Pallier   bugfix import Age...
11
12
sys.path.append("..")
from agent.Agent import Agent, extract_parameters
f5684b6d   Etienne Pallier   Agent complet ave...
13

eac3e2ab   Etienne Pallier   New AgentX
14
15


a7887020   Etienne Pallier   GROSSE OPTIM : pl...
16
##log = L.setupLogger("AgentXTaskLogger", "AgentX")
eac3e2ab   Etienne Pallier   New AgentX
17

eac3e2ab   Etienne Pallier   New AgentX
18
19
20


class AgentX(Agent):
a7899883   Etienne Pallier   AgentX v0.2
21

ff0550d2   Etienne Pallier   Multi-agents : Ag...
22
23
24

    # FOR TEST ONLY
    # Run this agent in simulator mode
c7318a4a   Etienne Pallier   Nouveau mode test...
25
    SIMULATOR_MODE = False
ff0550d2   Etienne Pallier   Multi-agents : Ag...
26
    # Run the assertion tests at the end
37d5efa2   Etienne Pallier   refactorisation, ...
27
    SIMULATOR_WITH_TEST = True
c7318a4a   Etienne Pallier   Nouveau mode test...
28
29
    #SIMULATOR_MAX_DURATION_SEC = None
    SIMULATOR_MAX_DURATION_SEC = 120
37d5efa2   Etienne Pallier   refactorisation, ...
30
    '''
ff0550d2   Etienne Pallier   Multi-agents : Ag...
31
32
33
34
    # Who should I send commands to ?
    #SIMULATOR_COMMANDS_DEST = "myself"
    SIMULATOR_COMMANDS_DEST = "AgentA"
    # Scenario to be executed
31e31f3b   Etienne Pallier   pyros.py peut lan...
35
    SIMULATOR_COMMANDS_LIST = [
ff0550d2   Etienne Pallier   Multi-agents : Ag...
36
37
38
39
40
41
42
43
        "go_active",
        "go_idle",
        "go_active",
        "go_idle",
        "go_active",
        "go_idle",
        "exit",
    ]
37d5efa2   Etienne Pallier   refactorisation, ...
44
    '''
ff0550d2   Etienne Pallier   Multi-agents : Ag...
45

93b402b7   Etienne Pallier   Added management ...
46
    """
6085d119   Etienne Pallier   Agent avec specif...
47
48
49
50
    =================================================================
        FUNCTIONS RUN INSIDE MAIN THREAD
    =================================================================
    """
3fec7115   Etienne Pallier   Agent encore très...
51
52

    # @override
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
53
    def __init__(self, name:str=None, config_filename=None, RUN_IN_THREAD=True):
3fec7115   Etienne Pallier   Agent encore très...
54
        if name is None: name = self.__class__.__name__
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
55
        super().__init__(name, config_filename, RUN_IN_THREAD)
a7899883   Etienne Pallier   AgentX v0.2
56
57
58
59

    # @override
    def init(self):
        super().init()
05775461   Alain Klotz   Lecture du fichie...
60
        # --- Set the mode according the startmode value
3fec7115   Etienne Pallier   Agent encore très...
61
62
        ##agent_alias = self.__class__.__name__
        ##self.set_mode_from_config(agent_alias)
a7899883   Etienne Pallier   AgentX v0.2
63

30b22ee6   Etienne Pallier   Agent : agentX wo...
64
    '''
6085d119   Etienne Pallier   Agent avec specif...
65
    # @override
a7899883   Etienne Pallier   AgentX v0.2
66
67
    def load_config(self):
        super().load_config()
30b22ee6   Etienne Pallier   Agent : agentX wo...
68
    '''
a7899883   Etienne Pallier   AgentX v0.2
69

30b22ee6   Etienne Pallier   Agent : agentX wo...
70
    '''
6085d119   Etienne Pallier   Agent avec specif...
71
    # @override
fcfc6200   Etienne Pallier   ajout nouvelle co...
72
    def update_survey(self):
30b22ee6   Etienne Pallier   Agent : agentX wo...
73
74
        super().update_survey()
    '''
a7899883   Etienne Pallier   AgentX v0.2
75

30b22ee6   Etienne Pallier   Agent : agentX wo...
76
    '''
6085d119   Etienne Pallier   Agent avec specif...
77
    # @override
30b22ee6   Etienne Pallier   Agent : agentX wo...
78
79
80
    def get_next_command(self):
        return super().get_next_command()
    '''
a7899883   Etienne Pallier   AgentX v0.2
81
82
83
84
85

    # @override
    def do_log(self):
        super().do_log()

6085d119   Etienne Pallier   Agent avec specif...
86
87
88
89


    """
    =================================================================
33151b38   Etienne Pallier   Agent fonctionne ...
90
        FUNCTIONS RUN INSIDE A SUB-THREAD (OR A PROCESS) (thread_*())
6085d119   Etienne Pallier   Agent avec specif...
91
92
93
    =================================================================
    """

33151b38   Etienne Pallier   Agent fonctionne ...
94
95
96
97
    # Define your own command step(s) here
    def cmd_step1(self, step:int):
        cmd = self._current_specific_cmd
        cmd.result = f"in step #{step}/{self._thread_total_steps_number}"
ba07274d   Etienne Pallier   Simplification du...
98
99
        cmd.save()
        """
6085d119   Etienne Pallier   Agent avec specif...
100
101
        if self.RUN_IN_THREAD:
            print("(save from thread)")
f5684b6d   Etienne Pallier   Agent complet ave...
102
            cmd.save()
6085d119   Etienne Pallier   Agent avec specif...
103
104
105
106
        else:
            #@transaction.atomic
            print("(save from process)")
            with transaction.atomic():
6085d119   Etienne Pallier   Agent avec specif...
107
                cmd.save()
ba07274d   Etienne Pallier   Simplification du...
108
109
110
                #Command.objects.select_for_update()
        """

33151b38   Etienne Pallier   Agent fonctionne ...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    def cmd_step2(self, step:int):
        self.cmd_step1(step)
    def cmd_step3(self, step:int):
        self.cmd_step1(step)
    def cmd_step4(self, step:int):
        self.cmd_step1(step)

    """
    # @override
    def thread_exec_specific_cmd_step(self, step:int, sleep_time:float=1.0):
        self.thread_stop_if_asked()
        cmd = self._current_specific_cmd
        print(f">>>>> Thread (cmd {cmd.name}): step #{step}/5")
        self.sleep(sleep_time)
    """
f5684b6d   Etienne Pallier   Agent complet ave...
126

6085d119   Etienne Pallier   Agent avec specif...
127
    '''
a7899883   Etienne Pallier   AgentX v0.2
128
    # @override
6085d119   Etienne Pallier   Agent avec specif...
129
130
131
    def exec_specific_cmd_start(self, cmd:Command, from_thread=True):
        super().exec_specific_cmd_start(cmd, from_thread)
    '''
f5684b6d   Etienne Pallier   Agent complet ave...
132

24aaf714   Etienne Pallier   ajout option SIMU...
133

6085d119   Etienne Pallier   Agent avec specif...
134
    # @override
33151b38   Etienne Pallier   Agent fonctionne ...
135
136
137
    def thread_exec_specific_cmd_main(self):
        # This is optional
        self.thread_set_total_steps_number(5)
24aaf714   Etienne Pallier   ajout option SIMU...
138

33151b38   Etienne Pallier   Agent fonctionne ...
139
        # HERE, write your own scenario
ba07274d   Etienne Pallier   Simplification du...
140
141
142

        # scenario OK
        self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
33151b38   Etienne Pallier   Agent fonctionne ...
143
144
145
146
147
        self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3)
        self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5)
        self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10)
        self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4)
        # ... as many as you need
f5684b6d   Etienne Pallier   Agent complet ave...
148

ba07274d   Etienne Pallier   Simplification du...
149
150
151
152
153
154
155
156
        """ autre scenario
        self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
        self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2)
        self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2)
        self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2)
        self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3)
        """

6085d119   Etienne Pallier   Agent avec specif...
157
158
159
160
    '''
    # @override
    def exec_specific_cmd_end(self, cmd:Command, from_thread=True):
        super().exec_specific_cmd_end(cmd, from_thread)
33151b38   Etienne Pallier   Agent fonctionne ...
161
    '''
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
162
163
164
165
166
167
168


if __name__ == "__main__":

    # with thread
    RUN_IN_THREAD=True
    # with process
c7318a4a   Etienne Pallier   Nouveau mode test...
169
    #RUN_IN_THREAD=False
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
170

c7318a4a   Etienne Pallier   Nouveau mode test...
171
    TEST_MODE, configfile = extract_parameters()
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
172
173
    #agent = AgentX()
    agent = AgentX("AgentX", configfile, RUN_IN_THREAD)
c7318a4a   Etienne Pallier   Nouveau mode test...
174
    agent.setSimulatorMode(TEST_MODE)
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
175
176
    print(agent)
    agent.run()