classdef MyEnvironment < rl.env.MATLABEnvironment
properties
XGrid = 10
YGrid = 10
GridSize = 20.0
XMax = 200
YMax = 200
MaxAngle = 359.9999
MinAngle = 0
Ts = 0.25
MaxD = 50
MaxDistance = 12.50
MinDistance = 0
FixDuration = 30
SimuDuration = 30
no_of_steps = 120
CovRange = 50
PenaltyForGoingOutside = -100
end
properties
State = zeros(4,1)
end
properties(Access = protected)
IsDone = false
end
methods
function this = MyEnvironment()
ObservationInfo = rlNumericSpec([4 1]);
ObservationInfo.Name = 'Grid States';
ObservationInfo.Description = 'Profit, Distance, x, y';
ActionInfo = rlNumericSpec([2 1],'LowerLimit',[0;0],'UpperLimit',[12.5;359.9999]);
ActionInfo.Name = 'dist;angle';
this = this@rl.env.MATLABEnvironment(ObservationInfo,ActionInfo);
Total_Grids = this.XGrid*this.YGrid;
Grid(Total_Grids) = struct();
G = 1;
for i = 1:this.XGrid
for j = 1:this.YGrid
Grid(G).X = (this.GridSize/2)+ (j-1)*(this.GridSize);
Grid(G).Y = (this.GridSize/2)+ (i-1)*(this.GridSize);
G = G + 1;
end
end
Profits = randi([0,20],100,1);
G = 1;
for i = 1:Total_Grids
Grid(G).Profit = Profits(G);
G = G+1;
end
updateActionInfo(this);
end
function [Observation,Reward,IsDone,LoggedSignals] = step(this,Action)
LoggedSignals = [];
persistent n
if isempty(n)
n = 1;
else
n = n+1;
end
[dist,angle] = getMovement(this,Action);
Profit = this.State(1);
Distance = this.State(2);
x = this.State(3);
y = this.State(4);
CosTheta = cosd(angle);
SinTheta = sind(Theta);
x_new = x + dist*CosTheta;
y_new = y + dist*SinTheta;
P = 0;
for k = 1: this.Total_Grids
if sqrt((x_new-this.Grid(k).X)^2 + (y_new-this.Grid(k).Y)^2)<= this.CovRange
P = P + this.Grid(k).Profit;
end
end
new_Profit = P;
dist_Traveled = dist;
delta_profit = new_Profit-Profit;
delta_dist = dist;
Observation = [new_profit, dist_traveled, x_new, y_new];
this.State = Observation;
if n == this.no_of_steps
this.IsDone = true;
end
if (x_new > this.XMax || y_new > this.YMax)
penalty = this.PenaltyForGoingOutside;
else
penalty = 0;
end
Reward = 10*(delta_profit/delta_dist)+ penalty;
end
function InitialObservation = reset(this)
P0 = 0;
D0 = 0;
X0 = 0;
Y0 = 0;
InitialObservation = [P0;D0;X0;Y0];
this.State = InitialObservation;
end
end
methods
function [dist,angle] = getMovement(this,action)
if ~ismember(action,this.ActionInfo.Elements)
error('Action must be limited by the valid range');
end
[dist,angle] = action;
end
function updateActionInfo(this)
this.ActionInfo.Elements = this.MaxForce*[-1 1];
end
end
end
0 Comments
Sign in to comment.