Skip to content

Commit

Permalink
Add pypop7/benchmarks/gymnasium.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Evolutionary-Intelligence committed Jul 5, 2024
1 parent 65b17d8 commit a191848
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions pypop7/benchmarks/gymnasium.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import gymnasium as gym


class Cartpole(object): # linear neural network
class Cartpole(object):
"""Control of Cartpole via a linear neural network.
"""
def __init__(self):
Expand All @@ -24,7 +24,7 @@ def __call__(self, x):
fitness : float
negative reward (for minimization rather than maximization).
"""
fitness = 0
fitness = 0.0
self.observation, _ = self.env.reset()
for i in range(1000):
action = np.matmul(x.reshape(self.action_dim, -1), self.observation[:, np.newaxis])
Expand All @@ -39,26 +39,28 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class Ant(object): # linear neural network
class Ant(object):
"""Control of Ant via a linear neural network.
"""
def __init__(self):
self.env = gym.make('Ant-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of Ant via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
negative reward (for minimization rather than maximization).
"""
fitness = 0
fitness = 0.0
self.observation, _ = self.env.reset()
for i in range(1000):
action = np.matmul(x.reshape(self.action_dim, -1), self.observation[:, np.newaxis])
Expand All @@ -69,20 +71,22 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class HalfCheetah(object): # linear neural network
class HalfCheetah(object):
"""Control of HalfCheetah via a linear neural network.
"""
def __init__(self):
self.env = gym.make('HalfCheetah-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of HalfCheetah via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
Expand All @@ -99,20 +103,22 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class Hopper(object): # linear neural network
class Hopper(object):
"""Control of Hopper via a linear neural network.
"""
def __init__(self):
self.env = gym.make('Hopper-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of Hopper via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
Expand All @@ -129,20 +135,22 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class Humanoid(object): # linear neural network
class Humanoid(object):
"""Control of Humanoid via a linear neural network.
"""
def __init__(self):
self.env = gym.make('Humanoid-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of Humanoid via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
Expand All @@ -159,20 +167,22 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class Swimmer(object): # linear neural network
class Swimmer(object):
"""Control of Swimmer via a linear neural network.
"""
def __init__(self):
self.env = gym.make('Swimmer-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of Swimmer via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
Expand All @@ -189,20 +199,22 @@ def __call__(self, x):
return fitness # for minimization (rather than maximization)


class Walker2d(object): # linear neural network
class Walker2d(object):
"""Control of Walker2d via a linear neural network.
"""
def __init__(self):
self.env = gym.make('Walker2d-v2')
self.observation, _ = self.env.reset()
self.action_dim = np.prod(self.env.action_space.shape) # for action probability space
self.action_dim = np.prod(self.env.action_space.shape)

def __call__(self, x):
"""Control of Walker2d via a linear neural network.
Parameters
----------
x : ndarray
input vector.
Returns
-------
fitness : float
Expand All @@ -216,4 +228,4 @@ def __call__(self, x):
fitness -= reward
if terminated or truncated:
return fitness # for minimization (rather than maximization)
return fitness # for minimization (rather than maximization)
return fitness # for minimization (rather than maximization)

0 comments on commit a191848

Please sign in to comment.