File size: 2,957 Bytes
f15113b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Methanol APC Environment Client."""

from typing import Dict

from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State

from .models import MethanolAPCAction, MethanolAPCObservation


class MethanolAPCEnv(EnvClient[MethanolAPCAction, MethanolAPCObservation, State]):
    """Async WebSocket client for the Methanol APC Environment.



    Example::



        async with MethanolAPCEnv(base_url="http://localhost:8000") as env:

            result = await env.reset(task_name="startup")

            action = MethanolAPCAction(

                feed_rate_h2=3.0, feed_rate_co=1.5,

                cooling_water_flow=50.0, compressor_power=40.0,

            )

            result = await env.step(action)

            print(result.observation.temperature)

    """

    def _step_payload(self, action: MethanolAPCAction) -> Dict:
        return {
            "feed_rate_h2": action.feed_rate_h2,
            "feed_rate_co": action.feed_rate_co,
            "cooling_water_flow": action.cooling_water_flow,
            "compressor_power": action.compressor_power,
        }

    def _parse_result(self, payload: Dict) -> StepResult[MethanolAPCObservation]:
        obs_data = payload.get("observation", {})
        observation = MethanolAPCObservation(
            temperature=obs_data.get("temperature", 0.0),
            pressure=obs_data.get("pressure", 0.0),
            feed_rate_h2=obs_data.get("feed_rate_h2", 0.0),
            feed_rate_co=obs_data.get("feed_rate_co", 0.0),
            h2_co_ratio=obs_data.get("h2_co_ratio", 2.0),
            cooling_water_flow=obs_data.get("cooling_water_flow", 0.0),
            cooling_water_temp=obs_data.get("cooling_water_temp", 25.0),
            catalyst_health=obs_data.get("catalyst_health", 1.0),
            methanol_produced=obs_data.get("methanol_produced", 0.0),
            reaction_rate=obs_data.get("reaction_rate", 0.0),
            profit_this_step=obs_data.get("profit_this_step", 0.0),
            cumulative_profit=obs_data.get("cumulative_profit", 0.0),
            step_number=obs_data.get("step_number", 0),
            max_steps=obs_data.get("max_steps", 100),
            task_name=obs_data.get("task_name", "startup"),
            safety_warning=obs_data.get("safety_warning"),
            temperature_trend=obs_data.get("temperature_trend", 0.0),
            done=payload.get("done", False),
            reward=payload.get("reward", 0.0),
            metadata=obs_data.get("metadata", {}),
        )
        return StepResult(
            observation=observation,
            reward=payload.get("reward"),
            done=payload.get("done", False),
        )

    def _parse_state(self, payload: Dict) -> State:
        return State(
            episode_id=payload.get("episode_id"),
            step_count=payload.get("step_count", 0),
        )