From 88c75759b368369fd39597fc7c049c1a57f9577b Mon Sep 17 00:00:00 2001 From: perceptualrobots Date: Tue, 28 Nov 2023 20:11:44 +0000 Subject: [PATCH] update --- nbs/04_hierarchy.ipynb | 273 +++++++++------------------------------- nbs/12_yaw_module.ipynb | 4 +- pct/hierarchy.py | 17 ++- pct/yaw_module.py | 2 +- 4 files changed, 74 insertions(+), 222 deletions(-) diff --git a/nbs/04_hierarchy.ipynb b/nbs/04_hierarchy.ipynb index 4ba52a72..2ba291e7 100644 --- a/nbs/04_hierarchy.ipynb +++ b/nbs/04_hierarchy.ipynb @@ -20,11 +20,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -43,11 +39,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| default_exp hierarchy" @@ -56,11 +48,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| export\n", @@ -79,11 +67,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| include: false\n", @@ -93,11 +77,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| export\n", @@ -131,11 +111,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| export\n", @@ -152,7 +128,7 @@ " def load_db(self, file):\n", " \"Load properties from file.\"\n", " from jproperties import Properties\n", - "\n", + " skip = ['raw', 'env', 'config', 'col', '', '', '', '', '', '']\n", " # read properties from file\n", " configs = Properties()\n", " #print(file)\n", @@ -162,6 +138,10 @@ " items_view = configs.items()\n", " self.db = {}\n", " for item in items_view:\n", + " if item in skip:\n", + " continue\n", + " if item.startswith('level'):\n", + " continue\n", " self.db[item[0]] = item[1].data\n", "\n", "\n", @@ -185,11 +165,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| export\n", @@ -1123,12 +1099,19 @@ " \n", " return environment_properties\n", "\n", + " # @classmethod\n", + " # def parameters_for_experiment(cls, db):\n", + " # d = db.copy()\n", + " # del d['raw']\n", + " \n", + "\n", " @classmethod\n", " def run_from_file(cls, filename, env_props=None, seed=None, render=False, history=False, move=None, plots=None, hpct_verbose= False, runs=None, outdir=None, early_termination = None, draw_file=None, experiment=None):\n", " \n", " prp = PCTRunProperties()\n", " prp.load_db(filename)\n", - " experiment.log_parameters(prp.db)\n", + " if experiment:\n", + " experiment.log_parameters(prp.db)\n", " \n", "\n", " error_collector_type = prp.db['error_collector_type'].strip()\n", @@ -1175,11 +1158,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "from pct.functions import Constant" @@ -1188,11 +1167,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "data": { @@ -1224,11 +1199,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1257,11 +1228,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1278,11 +1245,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1299,11 +1262,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "hpct.change_namespace()" @@ -1312,11 +1271,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1404,11 +1359,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#FunctionsList.getInstance().report() " @@ -1424,11 +1375,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1446,11 +1393,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "h = PCTHierarchy.from_config(config, namespace=namespace)" @@ -1459,11 +1402,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "assert h.get_config() == hpct.get_config()" @@ -1481,11 +1420,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1586,11 +1521,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "data": { @@ -1821,11 +1752,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1850,11 +1777,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "import os" @@ -1863,11 +1786,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "ahpct = PCTHierarchy(2,2, links=\"dense\")\n", @@ -1894,11 +1813,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -1914,11 +1829,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -1962,11 +1873,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "data": { @@ -2004,11 +1911,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2040,11 +1943,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "import json" @@ -2053,11 +1952,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "hpct1.save(\"hpct.json\")" @@ -2073,11 +1968,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2166,11 +2057,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "import networkx as nx\n", @@ -2180,11 +2067,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "# https://matplotlib.org/3.1.0/gallery/color/named_colors.html\n", @@ -2210,11 +2093,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2245,11 +2124,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2304,11 +2179,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "myhpct.insert_function(level=0, col=0, collection=\"perception\", function=Proportional(3, name=\"prop2\", namespace=namespace))\n", @@ -2326,11 +2197,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "myhpct.add_preprocessor(Constant(1, name=\"cons1\", namespace=namespace))\n", @@ -2349,11 +2216,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "myhpct.set_links(\"prop1\", \"cons1\")\n", @@ -2367,11 +2230,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2423,11 +2282,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "myhpctconfig = myhpct.get_config()\n", @@ -2445,11 +2300,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "myhpct.set_order([\"pctnode2\", \"pctnode1\", \"pctnode\"])" @@ -2465,11 +2316,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -2493,11 +2340,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "# config = {'type': 'Individual', 'name': 'pcthierarchy', 'pre': {'pre0': {'type': 'CartPoleV1', 'name': 'CartPoleV1', 'value': [0.03498833197860944, 0.20994561633454428, 0.012668159509212712, -0.2705237130920193, 0.047656152654718356], 'links': {0: 'Action1'}, 'env_name': 'CartPole-v1', 'reward': 1.0, 'done': False, 'info': {}}, 'pre1': {'type': 'IndexedParameter', 'name': 'ICV', 'value': 0.20994561633454428, 'links': {0: 'CartPoleV1'}, 'index': 1}, 'pre2': {'type': 'IndexedParameter', 'name': 'ICP', 'value': 0.03498833197860944, 'links': {0: 'CartPoleV1'}, 'index': 0}, 'pre3': {'type': 'IndexedParameter', 'name': 'IPV', 'value': -0.2705237130920193, 'links': {0: 'CartPoleV1'}, 'index': 3}, 'pre4': {'type': 'IndexedParameter', 'name': 'IPA', 'value': 0.012668159509212712, 'links': {0: 'CartPoleV1'}, 'index': 2}}, 'levels': {'level0': {'level': 0, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L0C0', 'refcoll': {'0': {'type': 'EAConstant', 'name': 'RL0C0', 'value': 0, 'links': {}}}, 'percoll': {'0': {'type': 'EAWeightedSum', 'name': 'PL0C0', 'value': -0.2705237130920193, 'links': {0: 'ICV', 1: 'ICP', 2: 'IPV', 3: 'IPA'}, 'weights': [0, 0, 1, 0]}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL0C0', 'value': 0.2705237130920193, 'links': {0: 'RL0C0', 1: 'PL0C0'}}}, 'outcoll': {'0': {'type': 'EAProportional', 'name': 'OL0C0', 'value': -0.05046166000036782, 'links': {0: 'CL0C0'}, 'gain': -0.1865332226280776}}}}}}}, 'post': {'post0': {'type': 'EAWeightedSum', 'name': 'Action1', 'value': -0.005282911840894066, 'links': {0: 'OL0C0'}, 'weights': [0.10469159835121472]}}}\n", @@ -2507,11 +2350,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "vscode": { - "languageId": "python" - } - }, + "metadata": {}, "outputs": [], "source": [ "#| hide\n", diff --git a/nbs/12_yaw_module.ipynb b/nbs/12_yaw_module.ipynb index 247808fa..0fbd5c1d 100644 --- a/nbs/12_yaw_module.ipynb +++ b/nbs/12_yaw_module.ipynb @@ -712,13 +712,13 @@ "source": [ "#| export\n", " \n", - "def test_hpct_wind(file=None,plots=None,history=None,verbose=None,outdir=None,early=None,environment_properties=None,start_index=None,stop_index=None,experiment=None,datatype='test', draw_file=None, model_file=None, experiment=None):\n", + "def test_hpct_wind(file=None,plots=None,history=None,verbose=None,outdir=None,early=None,environment_properties=None,start_index=None,stop_index=None,experiment=None,datatype='test', draw_file=None, model_file=None):\n", " '''\n", " test RLYCA\n", " '''\n", " \n", " from pct.hierarchy import PCTHierarchy\n", - " hierarchy, score = PCTHierarchy.run_from_file(file, env_props=environment_properties, plots=plots, history=history, hpct_verbose= verbose, runs=None, outdir=outdir, early_termination=early, draw_file=draw_file, experiment=None)\n", + " hierarchy, score = PCTHierarchy.run_from_file(file, env_props=environment_properties, plots=plots, history=history, hpct_verbose= verbose, runs=None, outdir=outdir, early_termination=early, draw_file=draw_file, experiment=experiment)\n", "\n", " env = hierarchy.get_preprocessor()[0].env\n", "\n", diff --git a/pct/hierarchy.py b/pct/hierarchy.py index 4e80aaf5..074ad97d 100644 --- a/pct/hierarchy.py +++ b/pct/hierarchy.py @@ -56,7 +56,7 @@ class PCTRunProperties(): def load_db(self, file): "Load properties from file." from jproperties import Properties - + skip = ['raw', 'env', 'config', 'col', '', '', '', '', '', ''] # read properties from file configs = Properties() #print(file) @@ -66,6 +66,10 @@ def load_db(self, file): items_view = configs.items() self.db = {} for item in items_view: + if item in skip: + continue + if item.startswith('level'): + continue self.db[item[0]] = item[1].data @@ -1017,11 +1021,20 @@ def get_environment_properties(cls, filename): return environment_properties + # @classmethod + # def parameters_for_experiment(cls, db): + # d = db.copy() + # del d['raw'] + + @classmethod - def run_from_file(cls, filename, env_props=None, seed=None, render=False, history=False, move=None, plots=None, hpct_verbose= False, runs=None, outdir=None, early_termination = None, draw_file=None): + def run_from_file(cls, filename, env_props=None, seed=None, render=False, history=False, move=None, plots=None, hpct_verbose= False, runs=None, outdir=None, early_termination = None, draw_file=None, experiment=None): prp = PCTRunProperties() prp.load_db(filename) + if experiment: + experiment.log_parameters(prp.db) + error_collector_type = prp.db['error_collector_type'].strip() error_response_type = prp.db['error_response_type'] diff --git a/pct/yaw_module.py b/pct/yaw_module.py index ac2b9af3..0ff9f628 100644 --- a/pct/yaw_module.py +++ b/pct/yaw_module.py @@ -594,7 +594,7 @@ def test_hpct_wind(file=None,plots=None,history=None,verbose=None,outdir=None,ea ''' from pct.hierarchy import PCTHierarchy - hierarchy, score = PCTHierarchy.run_from_file(file, env_props=environment_properties, plots=plots, history=history, hpct_verbose= verbose, runs=None, outdir=outdir, early_termination=early, draw_file=draw_file) + hierarchy, score = PCTHierarchy.run_from_file(file, env_props=environment_properties, plots=plots, history=history, hpct_verbose= verbose, runs=None, outdir=outdir, early_termination=early, draw_file=draw_file, experiment=experiment) env = hierarchy.get_preprocessor()[0].env