Converters
This page is organized as follow:
Objectives
In this module of grid2op, the “converters” are defined.
A converter is a specific class of grid2op.Action.ActionSpace
(ie of BaseAction Space) that allows the
agent to manipulate this action to have a different representation of it.
For example, suppose we are dealing with grid2op.Action.TopologyAndDispatchAction
(only manipulating the
graph of the powergrid). This is a discrete “action space”. Often, it’s custom to deal with such action space by
enumerating all actions, and then assign to all valid actions a unique ID.
This can be done easily with the IdToAct
class.
More concretely, the diagram of an agent is:
receive an observation (in a form of an object of class
grid2op.Observation.BaseObservation
)implement the
grid2op.Agent.BaseAgent.act()
taking as input angrid2op.Observation.BaseObservation
and returning angrid2op.Action.BaseAction
this
grid2op.Action.BaseAction
is then digested by the environment
Introducing some converters lead to the following:
receive an observation (
grid2op.Observation.BaseObservation
)the transformer automatically (using
Converter.convert_obs()
) to a transformed observationimplement the function
grid2op.Agent.AgentWithConverter.my_act()
that takes as input a transformed observation and returns an encoded actionthe transformer automatically transforms back the encoded action into a proper
grid2op.Action.BaseAction
this
grid2op.Action.BaseAction
is then digested by the environment
This simple mechanism allows people to focus on iii) above (typically implemented with artificial neural networks) without having to worry each time about the complex representations of actions and observations.
More details and a concrete example is given in the documentation of the class
grid2op.Agent.AgentWithConverter
.
Some examples of converters are given in IdToAct
and ToVect
.
Detailed Documentation by class
Classes:
|
Converter that can be used with analog representation of the grid state. |
|
In this converter, you have as many output as pairs of object that can be connected, and your model is asked to output 0 if he wants these elements disconnected and 1 if he wants them connected. |
|
This Base class should be use to implement any converter. |
|
This type of converter allows to represent action with unique id. |
|
This converters allows to manipulate the vector representation of the actions and observations. |
- class grid2op.Converter.AnalogStateConverter(action_space, bias=0.0)[source]
Converter that can be used with analog representation of the grid state. Details are provided in convert_obs and convert_act
The grid2op observation is converted into a 1d normalied array The grid2op action is created from a set of real valued arrays
It can not yet be converted to / from gym space. If this feature is interesting for you, you can reply to the issue posted at https://github.com/rte-france/Grid2Op/issues/16
Methods:
convert_act
(netstate)Create a grid2op action based on the last observation and the real valued state vectors in parameters
convert_obs
(obs)This converter will convert the observation into a 1D vector, with all values normalized, plus bias (if provided)
- convert_act(netstate)[source]
Create a grid2op action based on the last observation and the real valued state vectors in parameters
- Parameters:
netstate (
tuple
) –A tuple containing the following (3) elements:
netbus:
np.array
A numpy array of dimension n_bus(2) x dim_topo and range [0.0; 1.0].Where the first axis represent the bus, the second the elements. Then, for element i, netbus[bus_index][i] represent the probability element i should be on bus_index + 1. The buses are then picked using argmax across dimension 0
netline:
np.array
A numpy array of dimension n_line and range [0.0; 1.0] Each element representing a line status: 0 meaning disconnected and > 0.0 connectednetdisp:
np.array
A numpy array of dimension n_gen and range[-1.0;1.0] Each generator redispatch setpoint is then rescaled to the range [-rdown;+rup]. This is cumulative over time, as per grid2op convention.
- Returns:
res – An action that will change the last observation (current state) To the state described in parameters
- Return type:
grid2op.Action.Action
- class grid2op.Converter.ConnectivityConverter(action_space)[source]
In this converter, you have as many output as pairs of object that can be connected, and your model is asked to output 0 if he wants these elements disconnected and 1 if he wants them connected.
This type of modeling is rather hard to “get working” the first time, especially because some “conflict” might appear. For example, consider three objects (line for example) on a given substation. You can chose to “connect A and B”, connect “B and C” but “not connect A and C” in this case you need an algorithm to disambuate your action.
The section “examples” below provides a concrete example on what we mean by that and how to make it working.
It can not yet be converted to / from gym space. If this feature is interesting for you, you can reply to the issue posted at https://github.com/rte-france/Grid2Op/issues/16
NB compare to
IdToAct
this converter allows for a smaller size. If you have N elements connected at a substation, you end up with N*(N-1)/2 different action. Compare to IdToAct though, it is expected that your algorithm produces more than 1 outputs.VERY IMPORTANT : for this converter to work, it needs to remember the previous state of the grid, so you absolutely need to call its method
ConnectivityConverter.convert_obs()
a each observation.Note
This converter does not allow to affect the status (connected / disconnected) of the objects, neither to perform redispatching actions, neither to perform actions on storage units.
Examples
TODO: documentation in progress
The idea of this converter is to allow to provide an interface if you want to provide action with what elements should be connected together.
This is useful if an agent should reason on the target graph of the grid rather than reasoning on which elements are connected on which busbar.
This converters then expects a vector of floats, all in [0., 1.]. The number of components of this vector is determined once and for all at the initialization and is accessible with converter.n. This is determined with the following rule. A pair of element of the grid el_i, el_j (elements here is: load, generator, storage unit, origin side of a powerline, extremity side of a powerline):
el_i and el_j belongs at the same substation
the substation to which el_i and el_j belongs counts 4 or more elements
You can access which pair of elements is encoded for each component of this vector with
ConnectivityConverter.which_pairs()
.To create use the connectivity converter, you can:
import grid2op import numpy as np from grid2op.Converter import ConnectivityConverter env = grid2op.make("l2rpn_case14_sandbox", test=True) converter = ConnectivityConverter(env.action_space) # it's a good practice to seed the element that can be, for reproducibility converter.seed(0) # to avoid creating illegal actions affecting more than the allowed number of parameters converter.init_converter(max_sub_changed=env.parameters.MAX_SUB_CHANGED)
This converter is expected to receive a vector of the proper size with components being floats, representing:
-1.000…: the pairs should not be connected
1.000…: the pairs should be connected
0.000…: i have no opinion on this pairs of objects
It uses an heuristic (greedy) to compute a resulting target topology (the vector act.set_bus) that tries to minimize the “disagreement” between the connectivity provided and the topology computed.
More concretely, say you have 4 objects el1, el2, el3 and el4 connected on a substation. You want:
el1 connected to el2 with score of 0.7
In the above example, we can change the connectivity of 77 pairs of elements, being:
print(f"The connectivity of {converter.n} pairs of elements can be affected") for i in range(converter.n): sub_id, (type0, id0), (type1, id1) = converter.which_pairs(i) print(f"You can decide to connect / disconnect the "{type0} id {id0}" and the "{type1} id {id1}" at " f"substation {sub_id} by action on component {i}")
For example, if you want, at substation 1 to have:
“line_ex id 0”, “line_or id 2” and “load id 0” on the same busbar
“line_or id 3”, “line_or id 4” and “gen id 0” on the other one
You can (this is one of the possible way to do it):
encoded_act = np.zeros(converter.n) encoded_act[0] = 1 # i want to connect "line_ex id 0" and the "line_or id 2" encoded_act[1] = -1 # i don't want to connect "line_ex id 0" and the "line_or id 3" encoded_act[2] = -1 # i don't want to connect "line_ex id 0" and the "line_or id 4" encoded_act[3] = -1 # i don't want to connect "line_ex id 0" and the "gen id 0" encoded_act[4] = 1 # i want to connect "line_ex id 0" and the "load id 0" # and now retrieve the corresponding grid2op action: grid2op_act = converter.convert_act(encoded_act) print(grid2op_act)
Another one, to express exactly the same action:
encoded_act2 = np.zeros(converter.n) encoded_act2[0] = 1 # i want to connect "line_ex id 0" and the "line_or id 2" encoded_act2[4] = 1 # i want to connect "line_ex id 0" and the "load id 0" encoded_act2[9] = 1 # i want to connect "line_or id 3" and the "line_or id 4" encoded_act2[10] = 1 # i want to connect "line_or id 3" and the "gen id 0" encoded_act2[14] = -1 # i don't want to connect "gen id 0" and the "load id 0" # and now retrieve the corresponding grid2op action: grid2op_act2 = converter.convert_act(encoded_act2) print(grid2op_act2)
In most cases, “something” (eg a neural network) is responsible to predict the “encoded action” and this converter can then be used to convert it to a valid grid2op action.
Notes
This converter does not allow to connect / disconnect any object. This feature might be added in the future.
This converter takes as input a vector of (-1, 1) each component representing the “score” of the corresponding pairs of element on the grid to be connected or disconnected.
A perfect converter would minimize (the variables are the component of act.set_bus vector that can be either 0 (i dont change) 1 or 2) the sum, for all index i fo pairs of elements in the grid el_k, el_j (that are encoded at position i) 1 - encoded_act[i] if the pairs of elements el_k, el_j are on the same busbar {i.e iif (act.set_bus[el_k] == 1 and act.set_bus[el_j] == 1) or (act.set_bus[el_k] == 2 and act.set_bus[el_j] == 2)} and 1 + encoded_act[i] otherwise {i.e iif (act.set_bus[el_k] == 1 and act.set_bus[el_j] == 2) or (act.set_bus[el_k] == 2 and act.set_bus[el_j] == 1)}.
For now a heuristic based on a greedy approach is used. This is far from giving an “optimal” solution.
This heuristic tries to act on as little elements as possible.
Methods:
_compute_disagreement
(encoded_act, topo_vect)INTERNAL
convert_act
(encoded_act[, explore])For this converter, encoded_act is a vector, with the same size as there are possible ways to reconfigure the grid.
convert_obs
(obs)This function is used to convert an observation into something that is easier to manipulate.
do_nothing_encoded_act
()returns the do nothing encoding act
sample
()A utility used to sample a new random
BaseAction
.which_pairs
(pair_id)Returns a description of the pair of element that is encoded at position pair_id of the encoded_act
- _compute_disagreement(encoded_act, topo_vect)[source]
INTERNAL
Warning
/!\ Internal, do not use unless you know what you are doing /!\
Computes the disagreement between the encoded act and the proposed topo_vect
NB if encoded act is random uniform, and topo_vect is full of 1, then disagreement is, on average 0.5.
Lower disagreement is always better.
- convert_act(encoded_act, explore=None)[source]
For this converter, encoded_act is a vector, with the same size as there are possible ways to reconfigure the grid.
And it find a consistent state that does not break too much the connectivity asked.
NOTE: there might be better ways to do it… This is computed with a greedy approach for now.
- Parameters:
encoded_act (
numpy.ndarray
) – This action should have the same size as the number of pairs of element that can be connected. A number close to -1 means you don’t want to connect the pair together, a number close to +1 means you want the pairs to be connected together.explore (
int
) – Defaults toNone
to be purely greedy. The higher explore the closer the returned solution will be to the “global optimum”, but the longer it will takes.None
will return the greedy approaches. Note that this is definitely not optimized for performance, and casting this problem into an optimization problem and solving this combinatorial optimization would definitely make this convereter more usefull.
- Returns:
act – The action that is usable by grid2op (after conversion) [the action space must be compatible with the “set_bus” key word]
- Return type:
- convert_obs(obs)[source]
This function is used to convert an observation into something that is easier to manipulate.
VERY IMPORTANT: for this converter to work, it needs to remember the previous state of the grid, so you absolutely need to call its method
ConnectivityConverter.convert_obs()
at each observation.- Parameters:
obs (
grid2op.Observation.Observation
) – The input observation.- Returns:
transformed_obs – An different representation of the input observation, typically represented as a 1d vector that can be processed by a neural networks.
- Return type:
object
- sample()[source]
A utility used to sample a new random
BaseAction
.The sampled action is unitary: It has an impact on a single line/substation/generator.
There is no guarantee concerning the “legality” of the action (see the description of the Action module for more information about illegal action).
It will only act by doing action supported by the action space. For example, if the action space does not support “redispatching” then this method will NOT sample any redispatching action.
- Returns:
res – A random action sampled from the
ActionSpace.actionClass
- Return type:
BaseAction
Examples
The first usage is to sample uniform unary actions, you can do this with the following:
import grid2op env = grid2op.make("l2rpn_case14_sandbox") # and now you can sample from the action space random_action = env.action_space.sample()
Note that the random action can be illegal depending on the game rules defined in the rules
grid2op.Rules
If for some reason you want to sample more complex actions, you can do this the following way:
import grid2op env = grid2op.make("l2rpn_case14_sandbox") # and now you can sample from the action space random_action = env.action_space() # this action is not random at all, it starts by "do nothing" for i in range(5): # my resulting action will be a complex action # that will be the results of applying 5 random actions random_action += env.action_space.sample() print(random_action)
- which_pairs(pair_id)[source]
Returns a description of the pair of element that is encoded at position pair_id of the encoded_act
- Parameters:
pair_id (
int
) –- Returns:
res – Tuple of 3 elements containing:
sub_id the id of the substation affected by the component pair_id
(obj_type, obj_id) the i
- Return type:
tuple
- class grid2op.Converter.Converter(action_space)[source]
This Base class should be use to implement any converter. If for some reasons
Methods:
convert_act
(encoded_act)This function will transform the action, encoded somehow (for example identified by an id, represented by an integer) to a valid actions that can be processed by the environment.
convert_action_from_gym
(gymlike_action)Convert the action (represented as a gym object, in fact an ordered dict) as an action compatible with this converter.
convert_action_to_gym
(action)Convert the action (compatible with this converter) into a "gym action" (ie an OrderedDict)
convert_obs
(obs)This function is used to convert an observation into something that is easier to manipulate.
get_gym_dict
(cls_gym)To convert this space into a open ai gym space.
- convert_act(encoded_act)[source]
This function will transform the action, encoded somehow (for example identified by an id, represented by an integer) to a valid actions that can be processed by the environment.
- Parameters:
encoded_act (
object
) – Representation of an action, as a vector or an integer etc.- Returns:
regular_act – The action corresponding to the encoded_action above converted into a format that can be processed by the environment.
- Return type:
grid2op.Action.Action
- convert_action_from_gym(gymlike_action)[source]
Convert the action (represented as a gym object, in fact an ordered dict) as an action compatible with this converter.
This is not compatible with all converters and you need to install gym for it to work.
- Parameters:
gymlike_action – the action to be converted to an action compatible with the action space representation
- Returns:
The action converted to be understandable by this converter.
- Return type:
res
Examples
Here is an example on how to use this feature with the
grid2op.Converter.IdToAct
converter (imports are not shown here).# create the environment env = grid2op.make("l2rpn_case14_sandbox") # create the converter converter = IdToAct(env.action_space) # create the gym action space gym_action_space = GymObservationSpace(action_space=converter) gym_action = gym_action_space.sample() converter_action = converter.convert_action_from_gym(gym_action) # this represents the same action grid2op_action = converter.convert_act(converter_action)
- convert_action_to_gym(action)[source]
Convert the action (compatible with this converter) into a “gym action” (ie an OrderedDict)
This is not compatible with all converters and you need to install gym for it to work.
- Parameters:
action – the action to be converted to an action compatible with the action space representation
- Returns:
The action converted to a “gym” model (can be used by a machine learning model)
- Return type:
res
Examples
Here is an example on how to use this feature with the
grid2op.Converter.IdToAct
converter (imports are not shown here).# create the environment env = grid2op.make("l2rpn_case14_sandbox") # create the converter converter = IdToAct(env.action_space) # create the gym action space gym_action_space = GymObservationSpace(action_space=converter) converter_action = converter.sample() gym_action = converter.to_gym(converter_action) # this represents the same action
- convert_obs(obs)[source]
This function is used to convert an observation into something that is easier to manipulate.
- Parameters:
obs (
grid2op.Observation.Observation
) – The input observation.- Returns:
transformed_obs – An different representation of the input observation, typically represented as a 1d vector that can be processed by a neural networks.
- Return type:
object
- get_gym_dict(cls_gym)[source]
To convert this space into a open ai gym space. This function returns a dictionnary used to initialize such a converter.
It should not be used directly. Prefer to use the
grid2op.Converter.GymConverter
cls_gym represents either
grid2op.gym_compat.LegacyGymActionSpace
orgrid2op.gym_compat.GymnasiumActionSpace
- class grid2op.Converter.IdToAct(action_space)[source]
This type of converter allows to represent action with unique id. Instead of manipulating complex objects, it allows to manipulate only positive integer.
The list of all actions can either be the list of all possible unary actions (see below for a complete description) or by a given pre computed list.
A “unary action” is an action that consists only in acting on one “concept” it includes:
disconnecting a single powerline
reconnecting a single powerline and connect it to bus xxx on its origin side and yyy on its extremity side
changing the topology of a single substation
performing redispatching on a single generator
performing curtailment on a single generator
performing action on a single storage unit
Examples of non unary actions include: - disconnection / reconnection of 2 or more powerlines - change of the configuration of 2 or more substations - disconnection / reconnection of a single powerline and change of the configration of a single substation
NB All the actions created automatically are unary. For the L2RPN 2019, agent could be allowed to act with non unary actions, for example by disconnecting a powerline and reconfiguring a substation. This class would not allow to do such action at one time step.
NB The actions that are initialized by default uses the “set” way and not the “change” way (see the description of
grid2op.BaseAction.BaseAction
for more information).For each powerline, 5 different actions will be computed:
disconnect it
reconnect it and connect it to bus 1 on “origin” end ann bus 1 on “extremity” end
reconnect it and connect it to bus 1 on “origin” end ann bus 2 on “extremity” end
reconnect it and connect it to bus 2 on “origin” end ann bus 1 on “extremity” end
reconnect it and connect it to bus 2 on “origin” end ann bus 2 on “extremity” end
Actions corresponding to all topologies are also used by default. See
grid2op.BaseAction.ActionSpace.get_all_unitary_topologies_set()
for more information.In this converter:
encoded_act are positive integer, representing the index of the actions.
transformed_obs are regular observations.
NB The number of actions in this converter can be especially big. For example, if a substation counts N elements there are roughly 2^(N-1) possible actions in this substation. This means if there are a single substation with more than N = 15 or 16 elements, the amount of actions (for this substation alone) will be higher than 16.000 which makes it rather difficult to handle for most machine learning algorithm. Be carefull with that !
Methods:
convert_act
(encoded_act)In this converter, we suppose that "encoded_act" is an id of an action stored in the
IdToAct.all_actions
list.convert_action_from_gym
(gymlike_action)Convert the action (represented as a gym object, in fact an ordered dict) as an action compatible with this converter.
convert_action_to_gym
(action)Convert the action (compatible with this converter) into a "gym action" (ie an OrderedDict)
filter_action
(filtering_fun)This function allows you to "easily" filter generated actions.
get_gym_dict
(cls_gym)Transform this converter into a dictionary that can be used to initialized a
gym.spaces.Dict
.init_converter
([all_actions])This function is used to initialized the converter.
sample
()Having define a complete set of observation an agent can do, sampling from it is now made easy.
save
(path[, name])Save the action space as a numpy array that can be reloaded afterwards with the
IdToAct.init_converter()
function by setting argument all_actions to os.path.join(path, name)- convert_act(encoded_act)[source]
In this converter, we suppose that “encoded_act” is an id of an action stored in the
IdToAct.all_actions
list.Converting an id of an action (here called “act”) into a valid action is then easy: we just need to take the “act”-th element of
IdToAct.all_actions
.- Parameters:
encoded_act (
int
) – The id of the action- Returns:
action – The action corresponding to id “act”
- Return type:
grid2op.Action.Action
- convert_action_from_gym(gymlike_action)[source]
Convert the action (represented as a gym object, in fact an ordered dict) as an action compatible with this converter.
This is not compatible with all converters and you need to install gym for it to work.
- Parameters:
gymlike_action – the action to be converted to an action compatible with the action space representation
- Returns:
The action converted to be understandable by this converter.
- Return type:
res
Examples
Here is an example on how to use this feature with the
grid2op.Converter.IdToAct
converter (imports are not shown here).# create the environment env = grid2op.make("l2rpn_case14_sandbox") # create the converter converter = IdToAct(env.action_space) # create the gym action space gym_action_space = GymObservationSpace(action_space=converter) gym_action = gym_action_space.sample() converter_action = converter.from_gym(gym_action) # this represents the same action grid2op_action = converter.convert_act(converter_action) # this is a grid2op action
- convert_action_to_gym(action)[source]
Convert the action (compatible with this converter) into a “gym action” (ie an OrderedDict)
This is not compatible with all converters and you need to install gym for it to work.
- Parameters:
action – the action to be converted to an action compatible with the action space representation
- Returns:
The action converted to a “gym” model (can be used by a machine learning model)
- Return type:
res
Examples
Here is an example on how to use this feature with the
grid2op.Converter.IdToAct
converter (imports are not shown here).# create the environment env = grid2op.make("l2rpn_case14_sandbox") # create the converter converter = IdToAct(env.action_space) # create the gym action space gym_action_space = GymObservationSpace(action_space=converter) converter_action = converter.sample() gym_action = converter.to_gym(converter_action) # this represents the same action
- filter_action(filtering_fun)[source]
This function allows you to “easily” filter generated actions.
NB the action space will change after a call to this function, especially its size. It is NOT recommended to apply it once training has started.
- Parameters:
filtering_fun (
function
) – This takes an action as input and should retrieveTrue
meaning “this action will be kept” orFalse
meaning “this action will be dropped.
- get_gym_dict(cls_gym)[source]
Transform this converter into a dictionary that can be used to initialized a
gym.spaces.Dict
. The converter is modeled as a “Discrete” gym space with as many elements as the number of different actions handled by this converter.This is available as the “action” keys of the spaces.Dict gym action space build from it.
This function should not be used “as is”, but rather through
grid2op.Converter.GymConverter
cls_gym represents either
grid2op.gym_compat.LegacyGymActionSpace
orgrid2op.gym_compat.GymnasiumActionSpace
- init_converter(all_actions=None, **kwargs)[source]
This function is used to initialized the converter. When the converter is created, this method should be called otherwise the converter might be in an unstable state.
- Parameters:
all_actions (
None
,list
,str
,np.ndarray
) –See the example section for more informations.
if all_actions is:
None
: the action space will be built from scratch using the provided key word arguments.a
list
: The (ordered) list of all actions that the agent will be able to perform. If given a numberi
the converter will return actionall_actions[i]
. In the “pacman” game, this vector could be [“up”, “down”, “left”, “right”], in this case “up” would be encode by 0, “down” by 1, “left” by 2 and “right” by 3. If nothing is provided, the converter will output all the unary actions possible for the environment. Be careful, computing all these actions might take some time.a
str
this will be considered as a path where a previous converter has been saved. You need to provide the full path, including the filename and its extension. It gives something like: “/path/where/it/is/saved/action_space_vect.npy”
kwargs –
other keyword arguments (all considered to be
True
by default) that can be:- set_line_status:
bool
Whether you want to include the set line status in your action (in case the original action space allows it)
- change_line_status:
bool
Whether you want to include the “change line status” in your action space (in case the original action space allows it)
- change_line_status:
bool
Whether you want to include the “change line status” in your action space (in case the original action space allows it)
- set_topo_vect:
bool
Whether you want to include the “set_bus” in your action space (in case the original action space allows it)
- change_bus_vect:
bool
Whether you want to include the “change_bus” in your action space (in case the original action space allows it)
- redispatch:
bool
Whether you want to include the “redispatch” in your action space (in case the original action space allows it)
- curtail:
bool
Whether you want to include the “curtailment” in your action space (in case the original action space allows it)
- storage:
bool
Whether you want to include the “storage unit” in your action space (in case the original action space allows it)
- set_line_status:
Examples
Here is an example of a code that will: make a converter by selecting some action. Save it, and then restore its original state to be used elsewhere.
import grid2op from grid2op.Converter import IdToAct env = grid2op.make("l2rpn_case14_sandbox") converter = IdToAct(env.action_space) # the path were will save it path_ = "/path/where/it/is/saved/" name_file = "tmp_convert.npy" # init the converter, the first time, here by passing some key word arguments, to not consider # redispatching for example converter.init_converter(redispatch=False) converter.save(path_, name_file) # i just do an action, for example the number 27... whatever it does does not matter here act = converter.convert_act(27) converter2 = IdToAct(self.env.action_space) converter2.init_converter(all_actions=os.path.join(path_, name_file)) act2 = converter2.convert_act(27) assert act == act2 # this is ``True`` the converter has properly been saved.
- sample()[source]
Having define a complete set of observation an agent can do, sampling from it is now made easy.
One action amoung the n possible actions is used at random.
- Returns:
res – An id of an action.
- Return type:
int
- save(path, name='action_space_vect.npy')[source]
Save the action space as a numpy array that can be reloaded afterwards with the
IdToAct.init_converter()
function by setting argument all_actions to os.path.join(path, name)The resulting object will be a numpy array of float. Each row of this array will be an action of the action space.
- Parameters:
path (
str
) – The path were to save the action spacename (
str
, optional) – The name of the numpy array stored on disk. By default its “action_space_vect.npy”
Examples
Here is an example of a code that will: make a converter by selecting some action. Save it, and then restore its original state to be used elsewhere.
import grid2op from grid2op.Converter import IdToAct env = grid2op.make("l2rpn_case14_sandbox") converter = IdToAct(env.action_space) # the path were will save it path_ = "/path/where/it/is/saved/" name_file = "tmp_convert.npy" # init the converter, the first time, here by passing some key word arguments, to not consider # redispatching for example converter.init_converter(redispatch=False) converter.save(path_, name_file) # i just do an action, for example the number 27... whatever it does does not matter here act = converter.convert_act(27) converter2 = IdToAct(self.env.action_space) converter2.init_converter(all_actions=os.path.join(path_, name_file)) act2 = converter2.convert_act(27) assert act == act2 # this is ``True`` the converter has properly been saved.
- class grid2op.Converter.ToVect(action_space)[source]
This converters allows to manipulate the vector representation of the actions and observations.
In this converter:
encoded_act are numpy ndarray
transformed_obs are numpy ndarray (read more about these concepts by looking at the documentation of
grid2op.Converter.Converters
)
It is convertible to a gym representation (like the original action space) in the form of a spaces.Box representing a continuous action space (even though most component are probably discrete). Note that if converted to a gym space, it is unlikely the method “sample” will yield to valid results. Most of the time it should generate Ambiguous action that will not be handled by grid2op.
NB the conversion to a gym space should be done thanks to the
grid2op.Converter.GymActionSpace
.Methods:
convert_act
(encoded_act)In this converter encoded_act is a numpy ndarray.
convert_action_from_gym
(gymlike_action)Convert a gym-like action (ie a Ordered dictionary with one key being only "action") to an action compatible with this converter (in this case a vectorized action).
convert_action_to_gym
(action)Convert a an action of this converter (ie a numpy array) into an action that is usable with an open ai gym (ie a Ordered dictionary with one key being only "action")
convert_obs
(obs)This converter will match the observation to a vector, using the
grid2op.Observation.BaseObservation.to_vect()
function.get_gym_dict
(cls_gym)Convert this action space int a "gym" action space represented by a dictionary (spaces.Dict) This dictionary counts only one keys which is "action" and inside this action is the
- convert_act(encoded_act)[source]
In this converter encoded_act is a numpy ndarray. This function transforms it back to a valid action.
- Parameters:
encoded_act (
numpy.ndarray
) – The action, represented as a vector- Returns:
regular_act – The corresponding action transformed with the
grid2op.Action.BaseAction.from_vect()
.- Return type:
grid2op.Action.Action
- convert_action_from_gym(gymlike_action)[source]
Convert a gym-like action (ie a Ordered dictionary with one key being only “action”) to an action compatible with this converter (in this case a vectorized action).
- convert_action_to_gym(action)[source]
Convert a an action of this converter (ie a numpy array) into an action that is usable with an open ai gym (ie a Ordered dictionary with one key being only “action”)
- convert_obs(obs)[source]
This converter will match the observation to a vector, using the
grid2op.Observation.BaseObservation.to_vect()
function.- Parameters:
obs (
grid2op.Observation.Observation
) – The observation, that will be processed into a numpy ndarray vector.- Returns:
transformed_obs – The vector representation of the action.
- Return type:
numpy.ndarray
- get_gym_dict(cls_gym)[source]
Convert this action space int a “gym” action space represented by a dictionary (spaces.Dict) This dictionary counts only one keys which is “action” and inside this action is the
cls_gym represents either
grid2op.gym_compat.LegacyGymActionSpace
orgrid2op.gym_compat.GymnasiumActionSpace
If you still can’t find what you’re looking for, try in one of the following pages:
Still trouble finding the information ? Do not hesitate to send a github issue about the documentation at this link: Documentation issue template