Conversion of Python code to Matlab.
8 views (last 30 days)
Show older comments
Could someone please convert the code below from Python to Matlab?
--------------------------------------------------------------------------------------------------------------------------
from .reward_function_base import BaseRewardFunction
class EventDrivenReward(BaseRewardFunction):
"""
EventDrivenReward
Achieve reward when the following event happens:
- Shot down by missile: -200
- Crash accidentally: -200
- Shoot down other aircraft: +200
"""
def __init__(self, config):
super().__init__(config)
def get_reward(self, task, env, agent_id):
"""
Reward is the sum of all the events.
Args:
task: task instance
env: environment instance
Returns:
(float): reward
"""
reward = 0
if env.agents[agent_id].is_shotdown:
reward -= 200
elif env.agents[agent_id].is_crash:
reward -= 200
for missile in env.agents[agent_id].launch_missiles:
if missile.is_success:
reward += 200
return self._process(reward, agent_id)
--------------------------------------------------------------------------------------------------------------------------
0 Comments
Answers (1)
Jayanti
on 15 Oct 2024
Hi Joydev,
In MATLAB, importing classes from other files is handled differently than in Python. Each class is typically defined in its own file with the same name as the class. If “BaseRewardFunction” is defined in a separate file named “BaseRewardFunction.m”, MATLAB will automatically recognize it when you create an instance of “EventDrivenReward”, assuming both files are in the same directory or in MATLAB's path.
Now to derive “EventDrivenReward” from parent class “BaseRewardFunction” use the below syntax.
classdef EventDrivenReward < BaseRewardFunction
You can refer to the following link for subclass syntax.
According to your code it will have two methods which will be constructor of “EventDrivenReward” class and “get_reward”.
Below code declares a constructor method for the “EventDrivenReward” class. The @ symbol will invoke the constructor of the superclass on the object being created.
function obj = EventDrivenReward(config)
obj@BaseRewardFunction(config);
end
Now to declare method named “get_reward” that accepts input as “obj”, “task”, “env”, “agent_id” and return output “reward” use below syntax:
function reward = get_reward(obj, task, env, agent_id)
You can refer the following official MathWorks documentation on class constructor methods and functions for more information:
You can refer to below code to implement "for" loop.
for i = 1:length(env.agents(agent_id).launch_missiles)
missile = env.agents(agent_id).launch_missiles(i);
if missile.is_success
reward = reward + 200;
end
end
“Length” returns the number of elements along the largest dimension of the input. Refer to below official MathWorks documentation on “length” for more details:
Hope it help!
0 Comments
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!