def __init__(self, gridobj, controler_backend): """ Parameters ---------- gridobj: :class:`grid2op.Space.Gridobject` Structure of the powergrid envbackend: :class:`grid2op.Backend.Backend` An instanciated backend to perform some computation on a powergrid, before taking some actions. """ legal_act = AlwaysLegal() self.action_space = ActionSpace(gridobj=gridobj, actionClass=VoltageOnlyAction, legal_action=legal_act) self.backend = controler_backend.copy()
def _create_opponent(self): if not self.__is_init: raise EnvError( "Impossible to create an opponent with a non initialized environment!" ) if not issubclass(self.opponent_action_class, BaseAction): raise EnvError( "Impossible to make an environment with an opponent action class not derived from BaseAction" ) try: self.opponent_init_budget = float(self.opponent_init_budget) except Exception as e: raise EnvError( "Impossible to convert \"opponent_init_budget\" to a float with error {}" .format(e)) if self.opponent_init_budget < 0.: raise EnvError( "If you want to deactive the opponent, please don't set its budget to a negative number." "Prefer the use of the DontAct action type (\"opponent_action_class=DontAct\" " "and / or set its budget to 0.") if not issubclass(self.opponent_class, BaseOpponent): raise EnvError( "Impossible to make an opponent with a type that does not inherit from BaseOpponent." ) self.opponent_action_space = ActionSpace( gridobj=self.backend, legal_action=AlwaysLegal, actionClass=self.opponent_action_class) self.compute_opp_budg = UnlimitedBudget(self.opponent_action_space) self.opponent = self.opponent_class(self.opponent_action_space) self.oppSpace = OpponentSpace(compute_budget=self.compute_opp_budg, init_budget=self.opponent_init_budget, opponent=self.opponent) self.oppSpace.init() self.oppSpace.reset()
def init_backend(self, init_grid_path, chronics_handler, backend, names_chronics_to_backend, actionClass, observationClass, rewardClass, legalActClass): if not isinstance(rewardClass, type): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(rewardClass))) if not issubclass(rewardClass, BaseReward): raise Grid2OpException( "Parameter \"rewardClass\" used to build the Environment should derived form the grid2op.BaseReward class, " "type provided is \"{}\"".format(type(rewardClass))) self.rewardClass = rewardClass self.actionClass = actionClass self.observationClass = observationClass # backend self.init_grid_path = os.path.abspath(init_grid_path) if not isinstance(backend, Backend): raise Grid2OpException( "Parameter \"backend\" used to build the Environment should derived form the grid2op.Backend class, " "type provided is \"{}\"".format(type(backend))) self.backend = backend self.backend.load_grid( self.init_grid_path) # the real powergrid of the environment self.backend.load_redispacthing_data( os.path.split(self.init_grid_path)[0]) self.backend.load_grid_layout(os.path.split(self.init_grid_path)[0]) self.backend.assert_grid_correct() self.init_grid(backend) self._has_been_initialized( ) # really important to include this piece of code! if self._thermal_limit_a is None: self._thermal_limit_a = self.backend.thermal_limit_a else: self.backend.set_thermal_limit(self._thermal_limit_a) *_, tmp = self.backend.generators_info() # rules of the game if not isinstance(legalActClass, type): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should be a type " "(a class) and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(legalActClass, BaseRules): raise Grid2OpException( "Parameter \"legalActClass\" used to build the Environment should derived form the " "grid2op.BaseRules class, type provided is \"{}\"".format( type(legalActClass))) self.game_rules = RulesChecker(legalActClass=legalActClass) self.legalActClass = legalActClass # action helper if not isinstance(actionClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(actionClass, BaseAction): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should derived form the " "grid2op.BaseAction class, type provided is \"{}\"".format( type(actionClass))) if not isinstance(observationClass, type): raise Grid2OpException( "Parameter \"actionClass\" used to build the Environment should be a type (a class) " "and not an object (an instance of a class). " "It is currently \"{}\"".format(type(legalActClass))) if not issubclass(observationClass, BaseObservation): raise Grid2OpException( "Parameter \"observationClass\" used to build the Environment should derived form the " "grid2op.BaseObservation class, type provided is \"{}\"". format(type(observationClass))) # action affecting the grid that will be made by the agent self.helper_action_player = ActionSpace( gridobj=self.backend, actionClass=actionClass, legal_action=self.game_rules.legal_action) # action that affect the grid made by the environment. self.helper_action_env = ActionSpace( gridobj=self.backend, actionClass=CompleteAction, legal_action=self.game_rules.legal_action) self.helper_observation = ObservationSpace( gridobj=self.backend, observationClass=observationClass, rewardClass=rewardClass, env=self) # handles input data if not isinstance(chronics_handler, ChronicsHandler): raise Grid2OpException( "Parameter \"chronics_handler\" used to build the Environment should derived form the " "grid2op.ChronicsHandler class, type provided is \"{}\"". format(type(chronics_handler))) self.chronics_handler = chronics_handler self.chronics_handler.initialize( self.name_load, self.name_gen, self.name_line, self.name_sub, names_chronics_to_backend=names_chronics_to_backend) self.names_chronics_to_backend = names_chronics_to_backend # test to make sure the backend is consistent with the chronics generator self.chronics_handler.check_validity(self.backend) # reward function self.reward_helper = RewardHelper(self.rewardClass) self.reward_helper.initialize(self) for k, v in self.other_rewards.items(): v.initialize(self) # controler for voltage if not issubclass(self.voltagecontrolerClass, BaseVoltageController): raise Grid2OpException( "Parameter \"voltagecontrolClass\" should derive from \"ControlVoltageFromFile\"." ) self.voltage_controler = self.voltagecontrolerClass( gridobj=self.backend, controler_backend=self.backend) # create the opponent # At least the 3 following attributes should be set before calling _create_opponent # self.opponent_action_class # self.opponent_class # self.opponent_init_budget self._create_opponent() # performs one step to load the environment properly (first action need to be taken at first time step after # first injections given) self._reset_maintenance() do_nothing = self.helper_action_env({}) *_, fail_to_start, info = self.step(do_nothing) if fail_to_start: raise Grid2OpException( "Impossible to initialize the powergrid, the powerflow diverge at iteration 0. " "Available information are: {}".format(info)) # test the backend returns object of the proper size self.backend.assert_grid_correct_after_powerflow() # for gym compatibility self.action_space = self.helper_action_player # this should be an action !!! self.observation_space = self.helper_observation # this return an observation. self.reward_range = self.reward_helper.range() self.viewer = None self.metadata = {'render.modes': ["human", "rgb_array"]} self.spec = None self.current_reward = self.reward_range[0] self.done = False # reset everything to be consistent self._reset_vectors_and_timings()
def _action_setup(self): return ActionSpace(self.gridobj, legal_action=self.game_rules.legal_action, actionClass=TopologyAction)
def _action_setup(self): return ActionSpace(self.gridobj, legal_action=self.game_rules.legal_action, actionClass=PowerLineSet)