File size: 1,468 Bytes
a283aa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np


class PriceAwareAgent:
    """
    Heuristic agent for EVChargeEnv.

    - Charges more when price is low and grid load is safe.
    - Charges less when price is high or grid load is high.
    """

    def __init__(self,
                 low_price_threshold: float = 0.4,
                 high_price_threshold: float = 0.7,
                 high_load_threshold: float = 0.8):
        self.low_price_threshold = low_price_threshold
        self.high_price_threshold = high_price_threshold
        self.high_load_threshold = high_load_threshold

    def select_action(self, observation):
        """
        observation = [charge, price, load, time_step_norm]
        returns: np.array([action]) in [0, 1]
        """
        charge, price, load, t = observation

        # If almost full, stop charging.
        if charge >= 0.98:
            return np.array([0.0], dtype=np.float32)

        # If grid is very stressed, back off.
        if load >= self.high_load_threshold:
            return np.array([0.1], dtype=np.float32)

        # If price is low, charge aggressively.
        if price <= self.low_price_threshold:
            return np.array([0.9], dtype=np.float32)

        # If price is very high, charge slowly, just enough to make progress.
        if price >= self.high_price_threshold:
            return np.array([0.2], dtype=np.float32)

        # Medium case: moderate charging.
        return np.array([0.5], dtype=np.float32)