How to edit the name of a variable which is an object of a class?

8 ビュー (過去 30 日間)
Nasim
Nasim 2022 年 9 月 20 日
編集済み: Nasim 2022 年 9 月 21 日
Hello All,
I am working on a multi-agent model predictive control problem.
For each agent, I have a separate subclass and superclass, e.g., for 3 agents, I have 3 pairs of classes. I should mention that I have a separate mfile for each class.
In this multi-agent system, agents need to communicate with each and update certain properties after communication. What I need to do is to update these properties in the loop.
After searching online, this question thread was the best thing I could find but I do not know how to use it for the properties of a class.
Here is a minimal demonstration of what I need to do:
In the following code, we have 3 agents that need to communicate with each other. In each time instance, only two agents communicate with each other.
Gridsys1, GridSys2, and GridSys3 are instances of three classes that contain the system information for each agent. I need to update the properties neighbor_ID and neighbor_Y for every two agents that are communicating (agent i and agent j according to the code).
I would appreciate it if you can help me change the following code such that I can use the generic identifiers i and j in updating the properties of every two classes corresponding to agent i and j.
EDIT:
More clealry, I need to be able to change GridSys1.neighbor_ID to GridSysi.neighbor_ID (the subclass related to agent i) and change GridSys2.neighbor_ID to GridSysj.neighbor_ID (the subclass related to agent j). Also, I need to change the numbers in ID2 to IDj (the variable related to agent j), and so on.
I need to add that ID2, ID1, Y_noisy_2 and Y_noisy_1 are outputs of the following optimizer objects.
[GridSys1, ID1, y1, Zeta1, Y_act1, S_hat1,S1, Y_noisy_1, it1, U1] = GridSys1.apply_input();
[GridSys2, ID2, y2, Zeta2, Y_act2, S_hat2,S2, Y_noisy_2, it2, U2] = GridSys2.apply_input();
[GridSys2, ID3, y3, Zeta3, Y_act3, S_hat3,S3, Y_noisy_3, it3, U3] = GridSys2.apply_input();
for i = 1:3
neighbors = find(A(i,:)==1) % A is the adjacny matrix of the multi-agent system to find the neighbors of agent i
neighbors = neighbors(~ismember(neighbors, i))
for j = 1: length(neighbors) % here agent i communicates with agent j
GridSys1.neighbor_ID = ID2;
GridSys2.neighbor_ID = ID1;
GridSys1.neighbor_Y = Y_noisy_2;
GridSys2.neighbor_Y = Y_noisy_1;
[GridSys1, status1] = GridSys1.compute_input(controller1);
[GridSys2, status2] = GridSys2.compute_input(controller2);
[GridSys3, status3] = GridSys3.compute_input(controller3);
end
end
  8 件のコメント
Nasim
Nasim 2022 年 9 月 21 日
@Stephen23 Thank you for sharing this thread!

サインインしてコメントする。

採用された回答

Matt J
Matt J 2022 年 9 月 20 日
編集済み: Matt J 2022 年 9 月 21 日
This might be what you want.
obj={GridSys1,GridSys2,GridSys3}; %list of objects
[I,J]=find(A.*(1-eye(size(A))));
K=numel(I);
for k=1:K
[i,j]=deal(I(k),J(k)]); %the k-th pair
[obj{i}, ID1, y1, Zeta1, Y_act1, S_hat1,S1, Y_noisy_1, it1, U1] = obj{i}.apply_input();
[obj{j}, ID2, y2, Zeta2, Y_act2, S_hat2,S2, Y_noisy_2, it2, U2] = obj{j}.apply_input();
obj{i}.neighbor_ID = ID2;
obj{j}.neighbor_ID = ID1;
obj{i}.neighbor_Y = Y_noisy_2;
obj{j}.neighbor_Y = Y_noisy_1;
end
  1 件のコメント
Nasim
Nasim 2022 年 9 月 21 日
編集済み: Nasim 2022 年 9 月 21 日
Thank you! It was excatly what I needed!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTraining and Simulation についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by