nikosdi
nikosdi

Reputation: 2158

Inter-process communication on MATLAB

I want to create a MATLAB program to simulate the behavior of some agents. Each of these agents is capable of communicating with each other and decide on is next state. I could implement the program is a traditional language that I am familiar with like java,python or C++ and use threads to simulate each of the agents.

Now I want to try the implementation on MATLAB to make use of the MATLAB plot-functions and its mathematical tools. Is it possible to create such a simulation in MATLAB, or maybe better is it strait-forward? I am aware of the parallelism toolbox but I am not sure if MATLAB is a good choice for such an application. I can also make the simulation no-parallel but it is not that interesting. This is part of an assignment and I would like to know if it is a good idea to start such a simulation on MATLAB to get more familiar with it. If it is not strait-forward I can switch easily to python.

Upvotes: 2

Views: 1373

Answers (3)

Bastian
Bastian

Reputation: 203

Here's what I would do:

  1. Write an agent class in matlab with parameters you need, methods for setting and getting (or write subsref-methods) and methods for "decision making"
  2. Fill an array with instances of the class
  3. Either make an array containing an instance index followed by its predecessors, i.e. if, say agent 4 follows agents 1, 2, and 3 and agent 5 follows agents 1,2, and 4, the vector would look like: [4 1 2 3 5 1 2 4] and so on. Or make a parent-child matrix. You could also add a parameter storing the predecessors in the instances. If every agent is connected with each other, you don't even need this feature.
  4. Now you would run sequentially. All agents update their inputs, all agents compute their response and set their outputs.

As you can see, this is not parallel but sequential. However, I can't really see the adantage of parallel processing here. The toolbox is no help, since it only allows "workers" depending on how many cores you have at your disposal. Basically, even if you use the parallel processing toolbox, you won't have much of an advantage since it is meant to parallelize loops. for example, in a genetic algorithm, you can compute the cost function for every pool member independently, thus you can use the toolbox. In algorithms where one loop execution depends on the computations of the prior loop execution, you cannot use the toolbox.

Hope this helps.

Upvotes: 1

bdecaf
bdecaf

Reputation: 4732

As mentioned earlier you can't really have multiple processes in matlab.

But for the agents you can make them if their classes inherit from handles. Then you can give them a method to receive messages.

But keep in mind they will not run in parallel.

Upvotes: 1

tashuhka
tashuhka

Reputation: 5126

Matlab interprets the code sequentially. Hence, in order to solve your problem, you will need a loop that iterates each sampling time and evaluates the state of all the agents in a pre-defined order.

TimeMax  = 10;
TimeStep = 0.1;
time_counter = 0;
while time_counter<TimeMax  
  time_counter = time_counter + TimeStep; 
  % Update all the agents sequentially
end

This is not very efficient. Thus, I would suggest you to use Simulink, that supports parallel computations more naturally. Then, you can export the results to Matlab and do all the fancy plots that you wish.

Upvotes: 0

Related Questions