Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Space2.cs
Go to the documentation of this file.
1 using System;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Utilities.Structs {
10  [Serializable]
11  public struct Space2 : ISpace {
15  public int DecimalGranularity {
16  get { return this._decimal_granularity; }
17  set { this._decimal_granularity = value; }
18  }
19 
23  public Vector2 _Min_Values;
24 
28  public Vector2 _Max_Values;
29 
31  [SerializeField] bool normalised;
32 
36  [SerializeField]
38 
40  get { return this._distribution_sampler; }
41  set { this._distribution_sampler = value; }
42  }
43 
44  public Space2(int decimal_granularity = 2) : this() {
45  this._Min_Values = Vector2.one * -100f; //Vector2.negativeInfinity;
46  this._Max_Values = Vector2.one * 100f; //Vector2.positiveInfinity;
47  this._decimal_granularity = decimal_granularity;
48  this._distribution_sampler = new DistributionSampler();
49  }
50 
54  public Vector2 Span { get { return this._Max_Values - this._Min_Values; } }
55 
56  public Space1 Xspace {
57  get {
58  return new Space1(this.DecimalGranularity) {
59  _Min_Value = this._Min_Values.x,
60  _Max_Value = this._Max_Values.x
61  };
62  }
63  }
64 
65  public Space1 Yspace {
66  get {
67  return new Space1(this.DecimalGranularity) {
68  _Min_Value = this._Min_Values.y,
69  _Max_Value = this._Max_Values.y
70  };
71  }
72  }
73 
78  public dynamic Sample() {
79  var x = this.DistributionSampler.Range(this._Min_Values.x, this._Max_Values.x);
80  var y = this.DistributionSampler.Range(this._Min_Values.y, this._Max_Values.y);
81 
82  return new Vector3(x, y);
83  }
84 
90  public Vector2 ClipNormaliseRound(Vector2 v) {
91  if (v.x > this._Max_Values.x) {
92  v.x = this._Max_Values.x;
93  } else if (v.x < this._Min_Values.x) {
94  v.x = this._Min_Values.x;
95  }
96 
97  if (this.Span.x > 0) {
98  v.x = this.Round((v.x - this._Min_Values.x) / this.Span.x);
99  } else {
100  v.x = 0;
101  }
102 
103  if (v.y > this._Max_Values.y) {
104  v.y = this._Max_Values.y;
105  } else if (v.y < this._Min_Values.y) {
106  v.y = this._Min_Values.y;
107  }
108 
109  if (this.Span.y > 0) {
110  v.y = this.Round((v.y - this._Min_Values.y) / this.Span.y);
111  } else {
112  v.y = 0;
113  }
114 
115  return v;
116  }
117 
122  public float Round(float v) { return (float)Math.Round(v, this.DecimalGranularity); }
123 
127  public static Space2 ZeroOne {
128  get { return new Space2(1) {_Min_Values = Vector2.zero, _Max_Values = Vector2.one}; }
129  }
130 
131  public static Space2 TwentyEighty {
132  get { return new Space2(1) {_Min_Values = Vector2.one * 0.2f, _Max_Values = Vector2.one * 0.8f}; }
133  }
134 
135  public bool IsNormalised { get { return this.normalised; } set { this.normalised = value; } }
136  }
137 }
DistributionSampler _distribution_sampler
Definition: Space2.cs:37
Space2(int decimal_granularity=2)
Definition: Space2.cs:44
Vector2 ClipNormaliseRound(Vector2 v)
Definition: Space2.cs:90