sciencegym.simulations.Simulation_Scale module¶
- class sciencegym.simulations.Simulation_Scale.ScaleDraw(*args: Any, **kwargs: Any)¶
Bases:
SimulationInterface
- close()¶
Close the pygame window and terminate the program.
- convertDensityToGrayscale(density, low=4.0, high=6.0)¶
Gets a value for the density of one box and returns the corresponding grayscale value
- Parameters:
density (float) – density of the box (should be in range of the interval)
low (float) – the minimum value for the density
high (float) – the maximum value for the density
- Returns:
a RGB color
- Return type:
(int, int, int)
- convertDensityToRGB(density, low=4.0, high=6.0, channels=[True, True, True])¶
Gets a value for the density of one box and returns the corresponding color
- Parameters:
density (float) – density of the box (should be in range of the interval)
low (float) – the minimum value for the d ensity
high (float) – the maximum value for the density
channels – an array with 3 entries, where each entry says if the color channel should be used or not.
e.g. if it’s [True, False, True], we want to use the Red and Blue channel, but not the Green channel. :type channels: list[bool, bool, bool] :return: a RGB color :rtype: (int, int, int)
- convertGrayscaleToDensity(RGB, low=4.0, high=6.0)¶
Gets a Grayscale value of an box and returns the corresponding density of the box
- Parameters:
RGB ((int, int, int)) – (red, green, blue) values
low (float) – the minimum value for the density
high (float) – the maximum value for the density
- Returns:
density value
- Return type:
float
- convertRGBToDensity(RGB, low=4.0, high=6.0, channels=[True, True, True])¶
Gets a RGB value of an box and returns the corresponding density of the box
- Parameters:
RGB ((int, int, int)) – (red, green, blue) values
low (float) – the minimum value for the density
high (float) – the maximum value for the density
- Returns:
density value
- Return type:
float
- createBox(pos_x, pos_y=None, density=5.0, boxsize=1.0, index=0)¶
Create a new box on the screen
- Parameters:
pos_x – x position of the box (in pixel coordinates, not as a measurement of the distance to the center of the bar!)
pos_y – y position of the box, if None: use default position
density – density of the box, if not given: use default density = 5.0
boxsize – size of the box, if not given: use default boxsize = 1.0
index – index of the box for the dictionaries, if not given: calculated inside of the function
- Returns:
new (dynamic) box
- deleteAllBoxes()¶
Deletes every single box in the world
- deleteBox(box)¶
Delete a box from the world and from the screen.
- Parameters:
box (Box2D.b2Body) – box object which we want to delete
- getMaxAngle()¶
Only called at start to calculate the biggest possible angle. Use this to have a good termination condition for the learning process.
- Returns:
the value of the maximum possible angle
- Return type:
float
- internal_step(action=None)¶
Simulates the program with the given action and returns the observations
- Parameters:
action (list[float]) – the action(s) the agent chooses as an array of each new positions for each box that should be moved
- Returns:
new state after step, the reward, done, info
- Return type:
tuple[np.ndarray, float, bool, dict]
- moveAlongBar(box, delta_pos, index=0)¶
Take a box and move it along the bar with the given distance
- Parameters:
box (Box2d.body) – box to be moved along the bar
delta_pos (float) – how much should the box be moved (negative value –> left, positive value –> right)
index (int) – index of the box
- Returns:
the moved box
- Return type:
Box2d.b2Body
- moveBox(box, deltaX, deltaY, index=0)¶
Move a box in the world along a given vector (deltaX,deltaY)
- Parameters:
box (Box2d.b2Body) – box to be moved
deltaX (float) – How much do we want the box to be moved to the left or right?
deltaY (float) – How much do we want the box to be moved up or down?
index (int) – need to pass this, because we create a duplicate of the given box and delete the old one
- Returns:
the same box with the new positions
- Return type:
Box2d.b2Body
- moveBoxTo(box, x, y, index=0)¶
Place a box at a specific position on the field
- Parameters:
box (Box2d.b2Body) – Which box should be moved?
x (float) – new x position of the box
y (float) – new x position of the box
index (int) – index of the box
- Returns:
the moved box
- Return type:
Box2d.b2Body
- name = 'Scale'¶
- observation_space¶
self.observation_space = spaces.Box(low=0, high=1. if self.normalize else 255, shape=(self.width, self.height, 3), dtype=np.float32 if self.normalize else np.uint8)
- performActions(actions)¶
Get the new positions of the boxes and translate it into actions.
- Parameters:
actions – new positions of each box that can be moved by the agent
- Returns:
- placeBox(box, pos, index=0)¶
Place a box on the scale
- Parameters:
box
pos
index
- Returns:
- render(mode='human')¶
Render function, which runs the simulation and render it (if wished)
- Parameters:
mode (str) – “human” for rendering, “state_pixels” for returning the pixel array
- Returns:
nothing if mode is human, if mode is “state_pixels”, we return the array of the screen
- Return type:
np.array
- rescaleState(state=None)¶
Returns normalized version of the state
- Parameters:
state (np.ndarray) – the normal state, which is not normalized yet
- Returns:
the new normalized state
- Return type:
np.ndarray
- reset()¶
Reset function for the whole environment. Inside of it, we reset every counter/reward, we reset the boxes, the state and all other necessary things.
- Returns:
the new state (normalized, if wished)
- Return type:
np.ndarray
- resetBoxes()¶
Generate the boxes and place all of the ones that are supposed to be placed randomly
- Returns:
self.boxes, a dictionary with every box as value and its index as key
- Return type:
Dict[int, Box2D.b2Body]
- resetState()¶
Resets and returns the current values of the state
- Returns:
the new state
- Return type:
np.ndarray
- seed(seed=None)¶
Seed function for the random number calculation
- Parameters:
seed (int) – if None: cannot recreate the same results afterwards
- Returns:
the seed
- Return type:
list[int]
- step(action)¶
Actual step function called by the agent
- Parameters:
action (list[float]) – the action(s) the agent chooses as an array of each new positions for each box that should be moved
- Returns:
new state after step, the reward, done, info
- Return type:
tuple[np.ndarray, float, bool, dict]
- sciencegym.simulations.Simulation_Scale.rescale_movement(original_interval, value, to_interval=(-15, 15))¶
Help function to do and to undo the normalization of the action and observation space
- Parameters:
original_interval (list[float, float]) – Original interval, in which we observe the value.
value (float) – Number that should be rescaled.
to_interval (list[float, float]) – New interval, in which we want to rescale the value.
- Returns:
Rescaled value
- Return type:
float