浏览代码

initial commit of the curriculum with broadcast. Improved the Unity python handshake

/develop-generalizationTraining-TrainerController
vincentpierre 7 年前
当前提交
ac910514
共有 5 个文件被更改,包括 91 次插入6 次删除
  1. 1
      python/unityagents/__init__.py
  2. 15
      python/unityagents/environment.py
  3. 1
      unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs
  4. 12
      python/curriculum.json
  5. 68
      python/unityagents/curriculum.py

1
python/unityagents/__init__.py


from .environment import *
from .brain import *
from .exception import *
from .curriculum import *

15
python/unityagents/environment.py


from .brain import BrainInfo, BrainParameters
from .exception import UnityEnvironmentException, UnityActionException
from .curriculum import Curriculum
from PIL import Image
from sys import platform

class UnityEnvironment(object):
def __init__(self, file_name, worker_id=0,
base_port=5005):
base_port=5005, curriculum = None):
"""
Starts a new unity environment and establishes a connection with the environment.
Notice: Currently communication between Unity and Python takes place over an open socket without authentication.

proc1.kill()
self.close()
raise
self._data = {}
self._global_done = None

self._num_brains = len(self._brain_names)
self._num_external_brains = len(self._external_brain_names)
self._resetParameters = p["resetParameters"]
self._curriculum = Curriculum(curriculum, self._resetParameters)
self._conn.send(b".")
self._loaded = True
logger.info("\n'{}' started successfully!".format(self._academy_name))
if (self._num_external_brains == 0):

state_dict = json.loads(state)
return state_dict
def reset(self, train_mode=True, config=None):
def reset(self, train_mode=True, config=None, progress = None):
config = config or {}
old_lesson = self._curriculum.get_lesson_number()
config = self._curriculum.get_lesson(progress) if config is None else config
if old_lesson != self._curriculum.get_lesson_number():
logger.info("\nLesson changed. Now in Lesson {0} : \n\t{1}"
.format(self._curriculum.get_lesson_number(),
', '.join([str(x)+' -> '+str(config[x]) for x in config])))
if self._loaded:
self._conn.send(b"RESET")
self._conn.recv(self._buffer_size)

1
unity-environment/Assets/ML-Agents/Scripts/ExternalCommunicator.cs


{
string envMessage = JsonConvert.SerializeObject(envParams, Formatting.Indented);
sender.Send(Encoding.ASCII.GetBytes(envMessage));
Receive();
}
/// Receives messages from external agent

12
python/curriculum.json


{
"measure" : "progress",
"thresholds" : [0.1, 0.2, 0.5],
"min_lesson_length" : 3,
"signal_smoothing" : true,
"parameters" :
{
"param1" : [100, 50, 10, 1],
"param2" : [0.1, 0.1, 0.5, 0.01,0],
"param3" : [0.2, 0.3, 0.7, 0.9]
}
}

68
python/unityagents/curriculum.py


import json
import numpy as np
from .exception import UnityEnvironmentException
class Curriculum(object):
def __init__(self, location, default_reset_parameters):
self.lesson_number = 0
self.lesson_length = 0
self.measure_type = None
if location == None:
self.data = None
else:
try:
with open(location) as data_file:
self.data = json.load(data_file)
except:
raise UnityEnvironmentException(
"The file {0} could not be found.".format(location))
parameters = self.data["parameters"]
self.measure_type = self.data["measure"]
self.max_lesson_number = len(self.data['thresholds'])
self.smoothing_value = 0
for key in parameters:
if key not in default_reset_parameters:
raise UnityEnvironmentException(
"The parameter {0} in Curriculum {1} is not present in "
"the Environment".format(key, location))
for key in parameters:
if len(parameters[key]) != self.max_lesson_number + 1:
raise UnityEnvironmentException(
"The parameter {0} in Curriculum {1} must have {2} values "
"but {3} were found".format(key, location,
self.max_lesson_number + 1, len(parameters[key])))
@property
def measure(self):
return self.measure_type
def get_lesson_number(self):
return self.lesson_number
def set_lesson_number(self, value):
self.lesson_length = 0
self.lesson_number = max(0,min(value,self.max_lesson_number))
def get_lesson(self, progress):
if (self.data == None ) or (progress == None):
return {}
if self.data["signal_smoothing"]:
progress = self.smoothing_value*0.9 + 0.1*progress
self.smoothing_value = progress
self.lesson_length += 1
if self.lesson_number < self.max_lesson_number:
if ((progress > self.data['thresholds'][self.lesson_number])
and (self.lesson_length > self.data['min_lesson_length'])):
self.lesson_length = 0
self.lesson_number += 1
config = {}
parameters = self.data["parameters"]
for key in parameters:
config[key] = parameters[key][self.lesson_number]
return config
正在加载...
取消
保存