Commit 7e98f4bc authored by Mikko Lainio's avatar Mikko Lainio 🇺🇦
Browse files

Implemented plot drawing with lineRenderer. Documented some. Fixed a problem...

Implemented plot drawing with lineRenderer. Documented some. Fixed a problem with inputs, whoops, now the game is actually making more sense!
parent f0ca220e
......@@ -885,6 +885,166 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_renderer: {fileID: 1712676082}
--- !u!1 &427361713
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 427361714}
- component: {fileID: 427361716}
- component: {fileID: 427361715}
m_Layer: 5
m_Name: VisualizeHintText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &427361714
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 427361713}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 488868485}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: -456}
m_SizeDelta: {x: 1920.8, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &427361715
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 427361713}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_text: Press <color=#0ff>Space</color> to switch Visualization mode
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_outlineColor:
serializedVersion: 2
rgba: 4278190080
m_fontSize: 36
m_fontSizeBase: 36
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_textAlignment: 258
m_isAlignmentEnumConverted: 1
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_firstOverflowCharacterIndex: -1
m_linkedTextComponent: {fileID: 0}
m_isLinkedTextComponent: 0
m_isTextTruncated: 0
m_enableKerning: 1
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
m_ignoreRectMaskCulling: 0
m_ignoreCulling: 1
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_firstVisibleCharacter: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_textInfo:
textComponent: {fileID: 427361715}
characterCount: 40
spriteCount: 0
spaceCount: 5
wordCount: 6
linkCount: 0
lineCount: 1
pageCount: 1
materialCount: 1
m_havePropertiesChanged: 0
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
m_spriteAnimator: {fileID: 0}
m_isInputParsingRequired: 0
m_inputSource: 0
m_hasFontAssetChanged: 0
m_subTextObjects:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!222 &427361716
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 427361713}
m_CullTransparentMesh: 0
--- !u!1 &476367178
GameObject:
m_ObjectHideFlags: 0
......@@ -1142,6 +1302,7 @@ RectTransform:
- {fileID: 1135741839}
- {fileID: 6793702}
- {fileID: 29928317}
- {fileID: 427361714}
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
......@@ -1507,11 +1668,12 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_mid: {fileID: 363266372}
UpdateFrequency: 0.1
UpdateFrequency: 0.02
m_timeStepText: {fileID: 476367180}
m_scoreText: {fileID: 798756960}
m_growthText: {fileID: 267811705}
m_endScoreText: {fileID: 843634337}
m_visualizeHintText: {fileID: 427361715}
m_rewardImage: {fileID: 1135741840}
FinalScoreObject: {fileID: 6793701}
StartTextObject: {fileID: 29928316}
......@@ -1546,7 +1708,134 @@ MonoBehaviour:
m_NumAlphaKeys: 2
MaxGradientThreshold: 50
m_growthBuffer: 10
TimeSteps: 500
m_plotRenderer: {fileID: 797969627}
m_plotAccuracy: 1
m_plotVisualizeMode: 0
TimeSteps: 1000
--- !u!1 &797969625
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 797969626}
- component: {fileID: 797969627}
m_Layer: 0
m_Name: PlotRenderer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &797969626
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 797969625}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1813764432}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!120 &797969627
LineRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 797969625}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_MotionVectors: 0
m_LightProbeUsage: 0
m_ReflectionProbeUsage: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: b8ba30d1c9d30fd44bf2151642c97062, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 0
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_Positions: []
m_Parameters:
serializedVersion: 3
widthMultiplier: 0.04
widthCurve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
colorGradient:
serializedVersion: 2
key0: {r: 1, g: 1, b: 1, a: 1}
key1: {r: 1, g: 1, b: 1, a: 1}
key2: {r: 0, g: 0, b: 0, a: 0}
key3: {r: 0, g: 0, b: 0, a: 0}
key4: {r: 0, g: 0, b: 0, a: 0}
key5: {r: 0, g: 0, b: 0, a: 0}
key6: {r: 0, g: 0, b: 0, a: 0}
key7: {r: 0, g: 0, b: 0, a: 0}
ctime0: 0
ctime1: 65535
ctime2: 0
ctime3: 0
ctime4: 0
ctime5: 0
ctime6: 0
ctime7: 0
atime0: 0
atime1: 65535
atime2: 0
atime3: 0
atime4: 0
atime5: 0
atime6: 0
atime7: 0
m_Mode: 0
m_NumColorKeys: 2
m_NumAlphaKeys: 2
numCornerVertices: 3
numCapVertices: 3
alignment: 0
textureMode: 0
shadowBias: 0.5
generateLightingData: 0
m_UseWorldSpace: 0
m_Loop: 0
--- !u!1 &798756958
GameObject:
m_ObjectHideFlags: 0
......@@ -2124,6 +2413,37 @@ MeshFilter:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1712676079}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &1813764431
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1813764432}
m_Layer: 0
m_Name: Plot
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1813764432
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1813764431}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -2.98}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 797969626}
m_Father: {fileID: 0}
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1867985269
GameObject:
m_ObjectHideFlags: 0
......
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using System.Numerics;
using UnityEngine;
using Python.Runtime;
using UnityEditor.U2D;
using Vector3 = UnityEngine.Vector3;
public class QuantumDemoManager : MonoBehaviour
{
......@@ -34,15 +34,32 @@ public class QuantumDemoManager : MonoBehaviour
[SerializeField] private TMPro.TextMeshProUGUI m_scoreText;
[SerializeField] private TMPro.TextMeshProUGUI m_growthText;
[SerializeField] private TMPro.TextMeshProUGUI m_endScoreText;
[SerializeField] private TMPro.TextMeshProUGUI m_visualizeHintText;
[SerializeField] private UnityEngine.UI.Image m_rewardImage;
[SerializeField] private GameObject FinalScoreObject;
[SerializeField] private GameObject StartTextObject;
[SerializeField] private Gradient m_rewardGradient;
[SerializeField] private float MaxGradientThreshold = 10f;
[SerializeField] private int m_growthBuffer = 10;
[SerializeField] private LineRenderer m_plotRenderer;
[SerializeField] private PlotAccuracy m_plotAccuracy = PlotAccuracy.Max;
[SerializeField] private VisualizeMode m_plotVisualizeMode;
public enum VisualizeMode
{
ComplexAsVector,
ComplexMagnitude
}
public enum PlotAccuracy
{
Max = 1,
High = 2,
Medium = 4,
Low = 8
}
private MaterialColorChanger m_leftColor;
private MaterialColorChanger m_midColor;
private MaterialColorChanger m_rightColor;
......@@ -72,6 +89,7 @@ public class QuantumDemoManager : MonoBehaviour
StartTextObject.SetActive(true);
m_left.enabled = false;
m_right.enabled = false;
m_visualizeHintText.gameObject.SetActive(false);
ResetUI();
m_leftColor = m_left.GetComponent<MaterialColorChanger>();
......@@ -98,16 +116,20 @@ public class QuantumDemoManager : MonoBehaviour
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && m_state != State.Started)
if (Input.GetKeyUp(KeyCode.Space))
{
StartGame();
if (m_state != State.Started)
StartGame();
else
m_plotVisualizeMode = (VisualizeMode) ((1 + (int) m_plotVisualizeMode) % Enum.GetValues(typeof(VisualizeMode)).Length);
}
if (Input.GetKeyDown(KeyCode.Escape))
{
StopAllCoroutines();
Application.Quit();
}
}
public void StartGame()
......@@ -118,6 +140,7 @@ public class QuantumDemoManager : MonoBehaviour
StartCoroutine(GameCoroutine());
m_left.enabled = true;
m_right.enabled = true;
m_visualizeHintText.gameObject.SetActive(true);
}
private void InitEnv()
......@@ -125,7 +148,7 @@ public class QuantumDemoManager : MonoBehaviour
// To be safe, add a using (Py.Gil()) block every time you interact with any python wrapper class like StirapEnv
using (Python.Runtime.Py.GIL())
{
m_env = new StirapEnv(false, TimeSteps+1);
m_env = new StirapEnv(TimeSteps+1);
}
}
......@@ -150,7 +173,7 @@ public class QuantumDemoManager : MonoBehaviour
m_timeStepText.text = (TimeSteps - step).ToString();
step++;
} while (!RunStep(delta));
} while (!RunStep(step, delta));
EndGame();
}
......@@ -168,6 +191,7 @@ public class QuantumDemoManager : MonoBehaviour
m_left.enabled = false;
m_right.enabled = false;
m_growthRight.Clear();
m_visualizeHintText.gameObject.SetActive(false);
}
/// <summary>
......@@ -175,7 +199,7 @@ public class QuantumDemoManager : MonoBehaviour
/// </summary>
/// <param name="deltaTime"></param>
/// <returns></returns>
private bool RunStep(float deltaTime)
private bool RunStep(int step, float deltaTime)
{
StirapEnv.StepResult result;
......@@ -225,12 +249,43 @@ public class QuantumDemoManager : MonoBehaviour
//m_left.transform.position = new Vector3(lPos * 2, 0f, 0f) + Vector3.left * 2f;
//m_right.transform.position = new Vector3(rPos * 2, 0f, 0f) + Vector3.right * 2f;
SetScore(result.Reward);
RenderPlot(result);
SetScore(result.RightPopulation, step);
return result.Done;
}
private void SetScore(float score)
private void RenderPlot(StirapEnv.StepResult result)
{
if (m_plotRenderer == null)
return;
int len = result.WavePoints.Length;
List<Vector3> v = new List<Vector3>();
float xx = 10f;
float x_step = xx / len;
int step = (int) m_plotAccuracy;
m_plotRenderer.positionCount = len / step;
for (int i=0; i<len-step+1; i+=step)
{
Complex c = result.WavePoints[i];
if (m_plotVisualizeMode == VisualizeMode.ComplexAsVector)
v.Add(new Vector3((float) c.Real, (float) c.Imaginary, 0f));
if (m_plotVisualizeMode == VisualizeMode.ComplexMagnitude)
v.Add(new Vector3(i * x_step - xx/2f, (float)c.Magnitude, 0f));
}
m_plotRenderer.SetPositions(v.ToArray());
}
/// <summary>
/// Set the score of the demo game based on right well population
/// </summary>
private void SetScore(float pop, int step)
{
float score = pop * ((float)step / (float)TimeSteps);
m_score += score;
if (m_scoreText != null)
{
......
......@@ -10,15 +10,13 @@ public class ExampleStirap : MonoBehaviour
UnityEngine.Random.InitState(9989);
using (Python.Runtime.Py.GIL())
{
StirapEnv stirapEnv = new StirapEnv(true);
float[] Rewards = new float[stirapEnv.TimeSteps];
StirapEnv stirapEnv = new StirapEnv();
for (int i = 0; i < stirapEnv.TimeSteps; i++)
{
float left = Random.Range(-.5f, .5f);
float right = Random.Range(-.5f, .5f);
StirapEnv.StepResult result = stirapEnv.Step(left, right);
Rewards[i] = result.Reward;
if (result.Done)
{
......@@ -27,7 +25,6 @@ public class ExampleStirap : MonoBehaviour
}
}
Debug.Log("Score: " + Numpy.Sum(Rewards));
}
}
......
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using Python.Runtime;
......@@ -28,6 +29,19 @@ public class PythonManager/* : MonoBehaviour*/
}
return values.ToArray();
}
public Complex[] ComplexArray(dynamic array)
{
List<Complex> values = new List<Complex>();
foreach (dynamic complexNum in array)
{
Complex c = new Complex((double) complexNum.real, (double) complexNum.imag);
values.Add(c);
}
return values.ToArray();
}
}