Commit 0786a06c authored by Ibrahim Ahmed's avatar Ibrahim Ahmed
Browse files

added notebook, reset returns state from random action

parent 5fbd8159
Loading
Loading
Loading
Loading

Tutorial.ipynb

0 → 100644
+146 −0
Changes for Tutorial.ipynb: 146 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
%reload_ext autoreload
%autoreload 2

import requests
import json
from pprint import pprint

from env import BOPTestEnv
```

%% Cell type:code id: tags:

``` python
# Clone repository
!git clone https://github.com/ibpsa/project1-boptest.git ../boptest
```

%% Cell type:code id: tags:

``` python
# Make docker image
!cd ../boptest && make build TESTCASE=testcase2
```

%% Cell type:markdown id: tags:

If not already on docker group, add youself:

    sudo adduser $USER docker

Run this command in a separate terminal:

    make run TESTCASE=testcase2

%% Cell type:markdown id: tags:

## BOPTest structure

### API

| Interaction| Request|
|:-----------|:-------|
| Advance simulation with control input and receive measurements        |  POST ``advance`` with json data "{<input_name>:<value>}" |
| Reset simulation to beginning                                         |  PUT ``reset`` with no argument                           |
| Receive communication step in seconds                                 |  GET ``step``                                             |
| Set communication step in seconds                                     |  PUT ``step`` with argument ``step=<value>``              |
| Receive sensor signal names (y) and metadata                          |  GET ``measurements``                                     |
| Receive control signals names (u) and metadata                        |  GET ``inputs``                                           |
| Receive test result data (history of actions and states)                                              |  GET ``results``                                          |
| Receive test KPIs                                                     |  GET ``kpi``                                              |
| Receive test case name                                                |  GET ``name``                                             |
| Receive boundary condition forecast from current communication step   |  GET ``forecast``                                         |
| Receive boundary condition forecast parameters in seconds             |  GET ``forecast_parameters``                              |
| Set boundary condition forecast parameters in seconds  		        |  PUT ``forecast_parameters`` with arguments ``horizon=<value>``, ``interval=<value>``|


### Model structure

    HTTP server API {
        BOPTest Testcase python class [1] {
            pyFMI FMUModel python class [2] {
                binary FMU generated from [3] {
                    Modellica code
                }
            }
        }
    }

1]: https://github.com/ibpsa/project1-boptest/blob/master/testcase.py
2]: https://github.com/modelon-community/PyFMI
3]: https://en.wikipedia.org/wiki/Functional_Mock-up_Interface

%% Cell type:code id: tags:

``` python
# This is the address at which the test case listens:
url = 'http://0.0.0.0:5000/'
```

%% Cell type:markdown id: tags:

## API Endpoints

%% Cell type:code id: tags:

``` python
# Get input and outputs information
i = json.loads(requests.get(url + 'inputs').content)
o = json.loads((requests.get(url + 'measurements').content))

pprint(i)
pprint(o)
```

%% Cell type:code id: tags:

``` python
# Reset environment
requests.put(url + 'reset').text
```

%% Cell type:code id: tags:

``` python
# Get history of states and actions
s = json.loads(requests.get(url + 'results').content)
pprint(s)
```

%% Cell type:markdown id: tags:

## gym Env wrapper

%% Cell type:code id: tags:

``` python
env = BOPTestEnv(url)
```

%% Cell type:code id: tags:

``` python
env.reset()
```

%% Cell type:code id: tags:

``` python
env.describe()
```

%% Cell type:code id: tags:

``` python
%%time
state, reward, done, info = env.step([300, 300, 300, 300])
```

%% Cell type:code id: tags:

``` python
requests.get(url + 'kpi').content
```
+3 −2
Changes for env.py: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ class BOPTestEnv(gym.Env):

    def setup_spaces(self):
        """
        Initialize observation_ and action_ spaces by parsing metadata from the
        Initialize observation and action spaces by parsing metadata from the
        BOPTest testcase server.
        """
        minimum = [dim['Minimum'] for _, dim in self.states.items()]
@@ -126,7 +126,8 @@ class BOPTestEnv(gym.Env):
    def reset(self):
        requests.put(self.url + 'reset')
        self.time_ = 0
        # TODO: return initial state.
        self.state_, _, _, _ = self.step(action=self.action_space.sample())
        return self.state_

        
    def get_inputs(self):