Blame view

src/core/pyros_django/agent/AgentX.py 4.43 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
sys.path.append("..")
8902a770   Etienne Pallier   ctrl-c available ...
12
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

30eeadc1   Etienne Pallier   cleanup
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

30eeadc1   Etienne Pallier   cleanup
57
    # @Override
8902a770   Etienne Pallier   ctrl-c available ...
58
    def _init(self):
30eeadc1   Etienne Pallier   cleanup
59
        pass
a7899883   Etienne Pallier   AgentX v0.2
60

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

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

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

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

6085d119   Etienne Pallier   Agent avec specif...
83
84
85
86


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

33151b38   Etienne Pallier   Agent fonctionne ...
91
92
93
94
    # 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...
95
96
        cmd.save()
        """
6085d119   Etienne Pallier   Agent avec specif...
97
98
        if self.RUN_IN_THREAD:
            print("(save from thread)")
f5684b6d   Etienne Pallier   Agent complet ave...
99
            cmd.save()
6085d119   Etienne Pallier   Agent avec specif...
100
101
102
103
        else:
            #@transaction.atomic
            print("(save from process)")
            with transaction.atomic():
6085d119   Etienne Pallier   Agent avec specif...
104
                cmd.save()
ba07274d   Etienne Pallier   Simplification du...
105
106
107
                #Command.objects.select_for_update()
        """

33151b38   Etienne Pallier   Agent fonctionne ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    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...
123

6085d119   Etienne Pallier   Agent avec specif...
124
    '''
a7899883   Etienne Pallier   AgentX v0.2
125
    # @override
6085d119   Etienne Pallier   Agent avec specif...
126
127
128
    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...
129

24aaf714   Etienne Pallier   ajout option SIMU...
130

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

33151b38   Etienne Pallier   Agent fonctionne ...
136
        # HERE, write your own scenario
ba07274d   Etienne Pallier   Simplification du...
137
138
139

        # scenario OK
        self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1)
33151b38   Etienne Pallier   Agent fonctionne ...
140
141
142
143
144
        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...
145

ba07274d   Etienne Pallier   Simplification du...
146
147
148
149
150
151
152
153
        """ 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...
154
155
156
157
    '''
    # @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 ...
158
    '''
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
159
160
161
162
163
164
165


if __name__ == "__main__":

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

8902a770   Etienne Pallier   ctrl-c available ...
168
    TEST_MODE, configfile = __extract_parameters()
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
169
170
    #agent = AgentX()
    agent = AgentX("AgentX", configfile, RUN_IN_THREAD)
c7318a4a   Etienne Pallier   Nouveau mode test...
171
    agent.setSimulatorMode(TEST_MODE)
a7887020   Etienne Pallier   GROSSE OPTIM : pl...
172
173
    print(agent)
    agent.run()