Skip to content
Snippets Groups Projects
Commit 62fb2f68 authored by Julian Grothoff's avatar Julian Grothoff
Browse files

Added possibility to use multiple paletts in PTS.

Paletts are dynamically destroyed and added.
Count of paletts is parameterised in PTS.
Random positions for start and shiftPosition also available as PTS parameter.
Parameters may be changed via ML-Agents environment parameters (not tested yet).
parent 69939041
Branches
Tags
No related merge requests found
...@@ -261,8 +261,6 @@ Transform: ...@@ -261,8 +261,6 @@ Transform:
- {fileID: 169174052277945025} - {fileID: 169174052277945025}
- {fileID: 169174050895113485} - {fileID: 169174050895113485}
- {fileID: 169174050707208121} - {fileID: 169174050707208121}
- {fileID: 6332432346143671051}
- {fileID: 3112591704662025796}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
...@@ -278,9 +276,12 @@ MonoBehaviour: ...@@ -278,9 +276,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1be806d27cbd6477db64302a2f0ff1c0, type: 3} m_Script: {fileID: 11500000, guid: 1be806d27cbd6477db64302a2f0ff1c0, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
PalettePrefab: {fileID: 169174050707208125}
conveyors: [] conveyors: []
palettes: []
shiftPositions: [] shiftPositions: []
RandomShiftPosition: 0
RandomStartPosition: 0
PaletteCount: 1
--- !u!1001 &169174050709379159 --- !u!1001 &169174050709379159
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -1058,181 +1059,6 @@ Transform: ...@@ -1058,181 +1059,6 @@ Transform:
type: 3} type: 3}
m_PrefabInstance: {fileID: 2917908171107060378} m_PrefabInstance: {fileID: 2917908171107060378}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1001 &3339063132006125152
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 6844626074183280644}
m_Modifications:
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: maxStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RPositionChanged
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderCompleted
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RNullOutputRequested
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOccupied
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOpModeReset
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOpModeChange
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: AllowActionStepCount
value: 100
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: AllowActionOnCurrentIdle
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1063512414600440199, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ActionTimeout
value: 1000
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ActionStartTimeout
value: 10
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderCompleted
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RActionTimeout
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderRejected
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROutputOccupied
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RCloserToTarget
value: 0.1
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RPositionChanged
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.x
value: 6
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.y
value: 0.25
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.x
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.y
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.z
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_RootOrder
value: 3
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_Name
value: PA02
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1d9169e55f0cc40c88783fd142df404e, type: 3}
--- !u!4 &6332432346143671051 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
m_PrefabInstance: {fileID: 3339063132006125152}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &3602103055782209745 --- !u!1001 &3602103055782209745
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -1368,191 +1194,6 @@ Transform: ...@@ -1368,191 +1194,6 @@ Transform:
type: 3} type: 3}
m_PrefabInstance: {fileID: 3676577131140617877} m_PrefabInstance: {fileID: 3676577131140617877}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1001 &5946379039596309295
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 6844626074183280644}
m_Modifications:
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: maxStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RPositionChanged
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderCompleted
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RNullOutputRequested
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOccupied
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOpModeReset
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderOutputOpModeChange
value: 0
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: AllowActionStepCount
value: 100
objectReference: {fileID: 0}
- target: {fileID: 38864609666886958, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: AllowActionOnCurrentIdle
value: 1
objectReference: {fileID: 0}
- target: {fileID: 767232303689607797, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_BrainParameters.vectorObservationSize
value: 41
objectReference: {fileID: 0}
- target: {fileID: 767232303689607797, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_BrainParameters.VectorObservationSize
value: 41
objectReference: {fileID: 0}
- target: {fileID: 1063512414600440199, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RStep
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ActionTimeout
value: 1000
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ActionStartTimeout
value: 10
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderCompleted
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RActionTimeout
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROrderRejected
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: ROutputOccupied
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RCloserToTarget
value: 0.1
objectReference: {fileID: 0}
- target: {fileID: 4800416601660351983, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: RPositionChanged
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.x
value: 6
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.y
value: 0.25
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.x
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.y
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.z
value: -0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_RootOrder
value: 4
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_Name
value: PA03
objectReference: {fileID: 0}
- target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1d9169e55f0cc40c88783fd142df404e, type: 3}
--- !u!4 &3112591704662025796 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 8770740007255297387, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
m_PrefabInstance: {fileID: 5946379039596309295}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &7567009144639747036 --- !u!1001 &7567009144639747036
PrefabInstance: PrefabInstance:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
...@@ -1863,7 +1504,7 @@ PrefabInstance: ...@@ -1863,7 +1504,7 @@ PrefabInstance:
- target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e, - target: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3} type: 3}
propertyPath: m_Name propertyPath: m_Name
value: PA01 value: Palette
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1d9169e55f0cc40c88783fd142df404e, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 1d9169e55f0cc40c88783fd142df404e, type: 3}
...@@ -1873,3 +1514,9 @@ Transform: ...@@ -1873,3 +1514,9 @@ Transform:
type: 3} type: 3}
m_PrefabInstance: {fileID: 8930334003838302930} m_PrefabInstance: {fileID: 8930334003838302930}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
--- !u!1 &169174050707208125 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 8770740007255297391, guid: 1d9169e55f0cc40c88783fd142df404e,
type: 3}
m_PrefabInstance: {fileID: 8930334003838302930}
m_PrefabAsset: {fileID: 0}
...@@ -115,4 +115,16 @@ public partial class ControlComponent ...@@ -115,4 +115,16 @@ public partial class ControlComponent
OrderOutputs[output].Error = OrderOutputError.Occupied; OrderOutputs[output].Error = OrderOutputError.Occupied;
} }
} }
private void OnDestroy()
{
foreach (var oo in OrderOutputs)
{
if (oo.Value != null && oo.Value.Cc != null && oo.Value.Cc.IsOccupied(name))
{
oo.Value.Cc.Stop(name);
oo.Value.Cc.Free(name);
}
}
}
} }
...@@ -39,7 +39,7 @@ public class MLModel : Agent ...@@ -39,7 +39,7 @@ public class MLModel : Agent
/// <summary> /// <summary>
/// Connects the model to the <see cref="ControlComponent"/> and its <see cref="MLOperationMode"/> on start. /// Connects the model to the <see cref="ControlComponent"/> and its <see cref="MLOperationMode"/> on start.
/// </summary> /// </summary>
public void Start() public void Awake()
{ {
cc = GetComponent<ControlComponent>(); cc = GetComponent<ControlComponent>();
opMode = GetComponent<IMLOperationMode>(); opMode = GetComponent<IMLOperationMode>();
... ...
......
...@@ -117,11 +117,10 @@ public abstract class MLOperationMode : OperationMode, IMLOperationMode ...@@ -117,11 +117,10 @@ public abstract class MLOperationMode : OperationMode, IMLOperationMode
// all occupied order outputs are completed or stopped // all occupied order outputs are completed or stopped
if (control.OrderOutputs.All(entry => if (control.OrderOutputs.All(entry =>
entry.Value.Cc == null entry.Value.Cc == null
|| entry.Value.Cc.IsFree() || !entry.Value.Cc.IsOccupied(control.cc)
|| entry.Value.Cc.IsOccupied(control.cc) || entry.Value.Cc.EXST == ExecutionState.COMPLETED
&& (entry.Value.Cc.EXST == ExecutionState.COMPLETED
|| entry.Value.Cc.EXST == ExecutionState.STOPPED || entry.Value.Cc.EXST == ExecutionState.STOPPED
|| entry.Value.Cc.EXST == ExecutionState.IDLE))) || entry.Value.Cc.EXST == ExecutionState.IDLE))
{ {
control.EXST = ExecutionState.SUSPENDING; control.EXST = ExecutionState.SUSPENDING;
} }
... ...
......
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Unity.MLAgents; using Unity.MLAgents;
using System.Linq; using System.Linq;
using System; using System;
using System.Collections.Generic;
/// <summary> /// <summary>
/// This class defines the "Pallete Transport System" as a training environment for the palette agents. /// This class defines the "Pallete Transport System" as a training environment for the palette agents.
...@@ -18,13 +18,27 @@ using System; ...@@ -18,13 +18,27 @@ using System;
/// </remarks> /// </remarks>
public class PTS : MonoBehaviour public class PTS : MonoBehaviour
{ {
[Tooltip("Prefab for the palette, which is used to populate the environment based on PaletteCount value.")]
public GameObject PalettePrefab;
[Tooltip("Array of all conveyor control components belonging to this PTS. (Is automatically filled during startup.)")] [Tooltip("Array of all conveyor control components belonging to this PTS. (Is automatically filled during startup.)")]
public ControlComponent[] conveyors; public ControlComponent[] conveyors;
[Tooltip("Array of all palette control components belonging to this PTS. (Is automatically filled during startup.)")]
public ControlComponent[] palettes;
[Tooltip("Array of all shift position sensors belonging to this PTS. (Is automatically filled during startup.)")] [Tooltip("Array of all shift position sensors belonging to this PTS. (Is automatically filled during startup.)")]
public ShiftPosition[] shiftPositions; public ShiftPosition[] shiftPositions;
[Header("Curriculum Learning")]
public bool RandomShiftPosition = false;
public bool RandomStartPosition = false;
public uint PaletteCount = 1;
// Flag to indicate that a reset was started, so that a reset isn't started over and over
private bool resetting = false;
private int ready = 0;
/// <summary>
/// List of all palette control components watched by the PTS.
/// More palette control components may exist in the PTS, but are not used by the Academy, e.g. a palette that is controlled by heuristic
/// </summary>
private List<ControlComponent> palettes = new List<ControlComponent>();
public void Awake() public void Awake()
{ {
// Set environment reset callback to initalise the PTS correctly // Set environment reset callback to initalise the PTS correctly
...@@ -33,48 +47,54 @@ public class PTS : MonoBehaviour ...@@ -33,48 +47,54 @@ public class PTS : MonoBehaviour
public void Start() public void Start()
{ {
// Find all conveyors and palettes // Find all conveyors
List<ControlComponent> conveyorList = new List<ControlComponent>(); conveyors = Array.FindAll(GetComponentsInChildren<ControlComponent>(),
List<ControlComponent> paletteList = new List<ControlComponent>(); cc => cc.CompareTag("Conveyor"));
foreach (var cc in GetComponentsInChildren<ControlComponent>())
{
// Distinguish conveyors and palettes by tags
// TODO add "Type" field to control component (maybe as string or as reference to objects of a new class "ControlComponentType")
if (cc.gameObject.CompareTag("Conveyor"))
conveyorList.Add(cc);
else if (cc.gameObject.CompareTag("Palette"))
paletteList.Add(cc);
}
conveyors = conveyorList.ToArray();
palettes = paletteList.ToArray();
// Find all shift position sensors // Find all shift position sensors
shiftPositions = GetComponentsInChildren<ShiftPosition>(); shiftPositions = GetComponentsInChildren<ShiftPosition>();
} }
// Flag to indicate that a reset was started, so that a reset isn't started over and over
private bool resetting = false; /// <summary>
/* /// Cyclic check of palette control components state to initiate environment resets.
* Cyclic check of palette control components state to initiate environment resets. /// Disables palettes that are in COMPLETE state.
*/ /// </summary>
public void LateUpdate() public void LateUpdate()
{ {
// Check if all agents are done or have failures // Check if all agents are done or have failures
// TODO add a reset timeout as watchdog // TODO add a reset timeout as watchdog
if (!resetting) if (!resetting)
{ {
if (Array.TrueForAll(palettes, p => // Destroy and delete palette if it is done
p.EXST == ExecutionState.COMPLETED for (int i = palettes.Count - 1; i >= 0; i--)
|| p.EXST == ExecutionState.IDLE)) {
var p = palettes[i];
if (p.EXST == ExecutionState.COMPLETED
|| p.EXST == ExecutionState.IDLE)
{
Destroy(p.gameObject);
palettes.Remove(p);
}
}
if (!palettes.Any())
EnvironmentReset(); EnvironmentReset();
else if (Array.Find(palettes, p => else if (palettes.Find(p =>
p.EXST == ExecutionState.ABORTED p.EXST == ExecutionState.ABORTED
|| p.EXST == ExecutionState.STOPPED // TODO check if STOPPED counts as an error || p.EXST == ExecutionState.STOPPED // TODO check if STOPPED counts as an error
) != null) ) != null)
{
EnvironmentReset(); EnvironmentReset();
}
else if (Input.GetKey(KeyCode.R)) else if (Input.GetKey(KeyCode.R))
EnvironmentReset(); EnvironmentReset();
} }
else
{
if (palettes.Count > 0 && ready == palettes.Count)
resetting = false;
}
} }
/// <summary> /// <summary>
...@@ -88,6 +108,10 @@ public class PTS : MonoBehaviour ...@@ -88,6 +108,10 @@ public class PTS : MonoBehaviour
// indicate resetting process is active // indicate resetting process is active
resetting = true; resetting = true;
// Delete remaining palettes
palettes.ForEach(p => Destroy(p.gameObject));
palettes.Clear();
// Reset all conveyors // Reset all conveyors
foreach (ControlComponent cc in conveyors) foreach (ControlComponent cc in conveyors)
{ {
...@@ -97,31 +121,48 @@ public class PTS : MonoBehaviour ...@@ -97,31 +121,48 @@ public class PTS : MonoBehaviour
// Set shift positions according to curriculum // Set shift positions according to curriculum
foreach (ShiftPosition shift in shiftPositions) foreach (ShiftPosition shift in shiftPositions)
{ {
// TODO change in curriculum: first position 0, later random if (Academy.Instance.EnvironmentParameters.GetWithDefault("RandomShiftPosition",
Convert.ToSingle(RandomShiftPosition)) != 0f)
shift.SetRandomPosition(); shift.SetRandomPosition();
//shift.SetPosition(0); else
shift.SetPosition(0);
} }
// TODO spawn / enable/disable palettes in environment reset according to curriculum // Spawn palettes according to curriculum or PaletteCount value
int paletteCount = Convert.ToInt32(Academy.Instance.EnvironmentParameters.GetWithDefault("PaletteCount",
Convert.ToSingle(PaletteCount)));
ready = 0;
// Get random palette positions // Get random palette positions
int[] randomIndex = Enumerable.Range(0, conveyors.Length - 1).OrderBy(x => System.Guid.NewGuid()).Take(palettes.Length).ToArray(); int[] randomIndex = Enumerable.Range(0, conveyors.Length - 1).OrderBy(x => System.Guid.NewGuid()).Take(paletteCount).ToArray();
for (int i = 0; i < palettes.Length; i++) for (int i = 0; i < paletteCount; i++)
{ {
MLOpModePalette opMode = palettes[i].GetComponent<MLOpModePalette>(); ControlComponent paletteCC = Instantiate(PalettePrefab, gameObject.transform).GetComponent<ControlComponent>();
paletteCC.gameObject.name = "PA" + i;
paletteCC.gameObject.SetActive(true);
// Place palette at default position MLOpModePalette opMode = paletteCC.GetComponent<MLOpModePalette>();
// opMode.SetPhysicalPosition(conveyors[0]);
// Place palette random or at default position
if (Academy.Instance.EnvironmentParameters.GetWithDefault("RandomStartPosition",
Convert.ToSingle(RandomStartPosition)) != 0f)
{
// Place palette at random position // Place palette at random position
opMode.SetPhysicalPosition(conveyors[randomIndex[i]]); // TODO change in curriculum opMode.SetPhysicalPosition(conveyors[randomIndex[i]]);
} else
{
// Place palette at default position
opMode.SetPhysicalPosition(conveyors[i]);
}
// Set exit as target for all palettes // Set exit as target for all palettes
opMode.target = conveyors[conveyors.Length - 1].gameObject; // assume that the last conveyor is the exit //TODO change in curriculum opMode.target = conveyors[conveyors.Length - 1].gameObject; // assume that the last conveyor is the exit //TODO change in curriculum
// Set random goal position for all palettes // Set random goal position for all palettes
//palettes[i].target = conveyors[Random.Range(0, conveyors.Length - 2)]; //opMode.target = conveyors[Random.Range(0, conveyors.Length - 2)];
palettes.Add(paletteCC);
// Start ML operation mode of the palette // Start ML operation mode of the palette
StartCoroutine(StartMLOpMode(palettes[i])); StartCoroutine(StartMLOpMode(paletteCC));
} }
} }
...@@ -170,6 +211,6 @@ public class PTS : MonoBehaviour ...@@ -170,6 +211,6 @@ public class PTS : MonoBehaviour
palette.Start(name); palette.Start(name);
yield return new WaitWhile(() => palette.EXST == ExecutionState.IDLE); yield return new WaitWhile(() => palette.EXST == ExecutionState.IDLE);
resetting = false; ready++;
} }
} }
...@@ -71,7 +71,7 @@ public class MLOpModePalette : MLOperationMode ...@@ -71,7 +71,7 @@ public class MLOpModePalette : MLOperationMode
// TODO add tag for palettes (and ground) // TODO add tag for palettes (and ground)
if (collision.gameObject.CompareTag("Palette") || collision.gameObject.CompareTag("Ground")) if (collision.gameObject.CompareTag("Palette") || collision.gameObject.CompareTag("Ground"))
{ {
control.EXST = ExecutionState.ABORTING; if(control!=null) control.EXST = ExecutionState.ABORTING;
} }
} }
#endregion Unity specific overrides #endregion Unity specific overrides
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment