inherit some class properties with changed values for A-star algorithm

1 回表示 (過去 30 日間)
Nicolas soto vallejos
Nicolas soto vallejos 2022 年 5 月 26 日
編集済み: prabhat kumar sharma 2023 年 10 月 13 日
Hi I've been meaning to visit this A* algorithm and I'm stuck on the creating nodes part. I thought it would be a prefect oportunity to learn classdef but i'm not so sure its propper for this project.
What i've done: I've made a function that creates an array of randomly generated (x,y) positions for the nodes with obstacles making sure that non of the nodes gets cought inside or on them (still needs to prohibit double values). red stars represents the node positions
What I'm trying to do: I'm trying to create a node class that can contain the properties of it's respective node aswell as the parent and children so that they might be used for simpler "cost evaluation" of the g-value and heuretic value kinda like:
classdef start < handle
properties
name = 'start'
position = [0 0]
g_value = 0
h_value = 2.828
f_value = 2.828
end
end
classdef nodes < dynamicprops & handle
properties
position = []
child = '';
parent = '';
end
properties (Access = private)
baseref;
end
methods
function this = nodes(baseobject)
this.baseref = baseobject;
% take the propertynames from the old and put it in the new
for propname = properties(baseobject)'
metaprop = addprop(this,propname{1});
... take the value in the property "name" from start and put it in current parent
function this = set.parent(this,varargin{:})
this.parent = varargin{:};
end
function varargout = get.parent(this)
varargout{:} = this.baseref.name;
end
end
% calculate the new g-values in a similar fasion as below
% if possible can allways be done "outside" otherwise
this.g_value = this.baseref.g_value + norm(this.position - this.baseref.position);
end
end
So that when used it can store the variable name of it's parent and child
startpos = start
% here A will get 'startpos' as a parent
A = nodes(startpos); A.position = [0 1];
% and B will get 'A' as parent
B = nodes(A); B.position =[1 0]
and if possible to extract property values of the parent or child
>> A.parent
A.parent =
startpos
>> B.parent
B.parent =
A
>> A.g_value
A.g_value =
1
>> B.g_value
B.g_value =
2.414
I'm only intrested in inhereting the g_value of the parent and extracting the position and variable name of the child and parent.

回答 (1 件)

prabhat kumar sharma
prabhat kumar sharma 2023 年 10 月 13 日
編集済み: prabhat kumar sharma 2023 年 10 月 13 日
Hi Nicolas,
I understand that you are facing issue with updating the properties for parent and children, I have tried running your code and I find out that you can store the properties, You can go through the following code for reference.
classdef nodes < dynamicprops & handle
properties
name = '';
position = [];
child = '';
parent = '';
end
properties (Access = private)
baseref;
end
methods
function this = nodes(baseobject)
this.baseref = baseobject;
this.baseref.name = inputname(1)
% Take the property names from the old and put them in the new
for propname = properties(baseobject)'
if ~isprop(this, propname{1}) % Check if property is already defined
metaprop = addprop(this, propname{1});
end
end
end
function set.parent(this, value)
this.parent = value;
end
function value = get.parent(this)
value = this.baseref.name;
end
function value = get.child(this)
value = class(this);
end
% Calculate the new g-values in a similar fashion as below
% If possible, it can always be done "outside" otherwise
function calculateGValue(this)
this.g_value = this.baseref.g_value + norm(this.position - this.baseref.position);
end
end
end
I have used the function “inputname to get the workspace variable name.
For more information on “inputname” you can refer the below documentation:
Here is the below code to check the “parent” and “g_value”.
% Create objects and set positions
startpos = start;
A = nodes(startpos);
A.position = [0 1];
B = nodes(A);
B.position = [1 0];
% Calculate g_values
A.calculateGValue();
B.calculateGValue();
% Access parent and g_value properties
a_parent = A.parent;
b_parent = B.parent;
A_g_value = A.g_value;
B_g_value = B.g_value;
Regards,
Prabhat Sharma

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by