Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gymnasium api improvements #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions model/gym-interface/cpp/spaces.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ OpenGymBoxSpace::GetSpaceDescription()
boxSpacePb.add_shape(*i);
}

for (const auto& low : m_lowVec)
{
boxSpacePb.add_lows(low);
}
for (const auto& high : m_highVec)
{
boxSpacePb.add_highs(high);
}

boxSpacePb.set_dtype(m_dtype);
desc.mutable_space()->PackFrom(boxSpacePb);
return desc;
Expand Down
2 changes: 2 additions & 0 deletions model/gym-interface/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ message BoxSpace {
float high = 2;
Dtype dtype = 3;
repeated uint32 shape = 4;
repeated float lows = 5;
repeated float highs = 6;
}

message TupleSpace {
Expand Down
14 changes: 10 additions & 4 deletions model/gym-interface/py/ns3ai_gym_env/envs/ns3_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def _create_space(self, spaceDesc):
elif spaceDesc.type == pb.Box:
boxSpacePb = pb.BoxSpace()
spaceDesc.space.Unpack(boxSpacePb)
low = boxSpacePb.low
high = boxSpacePb.high
low = np.array(boxSpacePb.lows) if boxSpacePb.lows else boxSpacePb.low
high = np.array(boxSpacePb.highs) if boxSpacePb.highs else boxSpacePb.high
shape = tuple(boxSpacePb.shape)
mtype = boxSpacePb.dtype

Expand Down Expand Up @@ -267,9 +267,15 @@ def send_actions(self, actions):
def get_state(self):
obs = self.get_obs()
reward = self.get_reward()
done = self.is_game_over()
terminated = False
truncated = False
if self.is_game_over():
if self.gameOverReason == 1:
terminated = True # end because the agent reached its final state
else:
truncated = True # end because the simulation ended (for this agent)
extraInfo = {"info": self.get_extra_info()}
return obs, reward, done, False, extraInfo
return obs, reward, terminated, truncated, extraInfo

def __init__(self, targetName, ns3Path, ns3Settings=None, shmSize=4096):
if self._created:
Expand Down