integration of MATLAB and NetLogo

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

integration of MATLAB and NetLogo

Ana-36
Hi,
I have made a NetLogo model and now I need to integrate it with MATLAB. The model needs to send some parameters for MATLAB, and MATLAB must return some results for NetLogo. This way I can have a real-time simulation. I have no idea how to integrate them since I am a new user of NetLogo. I would appreciate it if you can help me with this problem.
Anna

Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

bilal_ahmad_21
Try Googling this topic. I think there is a MATLAB-Java interface out there.

Another option may be to use the export-world and import-world commands. You can export practically the entire NetLogo model to a csv file which Matlab can then read and write to. Then you can import the modified world file back into NetLogo. It's not the best solution, but it's a start, I guess.

--- In [hidden email], "Ana" <annahita_j@...> wrote:
>
> Hi,
> I have made a NetLogo model and now I need to integrate it with MATLAB. The model needs to send some parameters for MATLAB, and MATLAB must return some results for NetLogo. This way I can have a real-time simulation. I have no idea how to integrate them since I am a new user of NetLogo. I would appreciate it if you can help me with this problem.
> Anna
>


Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

P-24
In reply to this post by Ana-36
Anna, I want to do the same thing and I do not know how.
Is anyone out there knows how to integrate MATLAB and NetLogo?

Panagiotis


--- In [hidden email], "Ana" <annahita_j@...> wrote:
>
> Hi,
> I have made a NetLogo model and now I need to integrate it with MATLAB. The model needs to send some parameters for MATLAB, and MATLAB must return some results for NetLogo. This way I can have a real-time simulation. I have no idea how to integrate them since I am a new user of NetLogo. I would appreciate it if you can help me with this problem.
> Anna
>


Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

Silicaroach
If it doesn't have to be Matlab but something like it, you could go with R programming language as there is a package and it works very well.

Otherwise, try this thread  for some idea of what it takes.

I went with R since it was much faster figuring out R (here's a handy dictionary) than figuring out how to make the Matlab/Netlogo link.
Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

bogdan_chivoiu
In reply to this post by Ana-36


Hi Anna,

One way to do it, if no specific extension is available, is to use the Shell extension to run Matlab with the parameters from Netlogo.

The Shell extension allows you to run any executable that can run in command line mode (so you need to be able to do that with Matlab). You can use it this way:
- shell:exec <command> (shell:exec <command> <param> ...) => execute command synchronously and report a string of the results it prints to stdout
- shell:fork <command> (shell:fork <command> <param> ...) => execute command asynchronously and discard the results

For example:
show (shell:exec "cmd" "/c" "dir")
or
print (shell:exec "cmd" "/c" "C:/PathToMatlabScript/runMatlabScript.bat")

The Shell extension for version 4.1 is available at:
http://ccl.northwestern.edu/netlogo/4.1/extensions/

The setup of the NetLogo-Matlab interaction could be like this:
- the NetLogo model writes the parameters in text file 1,
- the Matlab script is called from NetLogo (with the Shell extension) to process the text file 1 and outputs another text file 2
- the NetLogo model waits for the text file 2 to be written, and then reads it and uses the values


I hope this works for you. I use this approach to integrate with Python scripts or other command line applications.

Happy modeling,
Bogdan


--- In [hidden email], "Ana" <annahita_j@...> wrote:
>
> Hi,
> I have made a NetLogo model and now I need to integrate it with MATLAB. The model needs to send some parameters for MATLAB, and MATLAB must return some results for NetLogo. This way I can have a real-time simulation. I have no idea how to integrate them since I am a new user of NetLogo. I would appreciate it if you can help me with this problem.
> Anna
>


Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

mbiggs
In reply to this post by Ana-36
I recently wrote an extension for Netlogo that links it with Matlab. Download and documentation are available here.
Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

Alfredo Covaleda
In reply to this post by bilal_ahmad_21
Why don't you try with R. Maybe R does not differ so much from MatLab.
There is a project called NetLogo-R-Extension. I haven't tried but it seems
that works.


http://netlogo-r-ext.berlios.de/

2011/7/18 pgabriel0710 <[hidden email]>

> **
>
>
> Try Googling this topic. I think there is a MATLAB-Java interface out
> there.
>
> Another option may be to use the export-world and import-world commands.
> You can export practically the entire NetLogo model to a csv file which
> Matlab can then read and write to. Then you can import the modified world
> file back into NetLogo. It's not the best solution, but it's a start, I
> guess.
>
>
> --- In [hidden email], "Ana" <annahita_j@...> wrote:
> >
> > Hi,
> > I have made a NetLogo model and now I need to integrate it with MATLAB.
> The model needs to send some parameters for MATLAB, and MATLAB must return
> some results for NetLogo. This way I can have a real-time simulation. I
> have no idea how to integrate them since I am a new user of NetLogo. I
> would appreciate it if you can help me with this problem.
> > Anna
> >
>
>  
>



--
_____________________________________
* **Alfredo Covaleda Vélez
**
 *Ingeniero Agrónomo
* *Universidad Nacional de Colombia

 Tecnólogo en Informática
 Corp. Universitaria Minuto de Dios - UniMinuto

 Celular:  (+57) 311 213 7829
________________________________
Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

newmodturtle
In reply to this post by Ana-36


Hi Anna,

I don't know anything about the use of MATLAB, but I use the R Extension (more concrete the RServe Extension) and it works perfectly fine for me. I send parameters to R, it calculates, in this case, some probabilities for me, and sends it back to NetLogo (on the fly with each tick).
If R can do what you want to do with MATLAB, I'd suggest to try it. BTW the support for the R Extension is great, so if you'd face any problems, there is always help ;)

HTH, cheers, Vanessa


--- In [hidden email], "Ana" <annahita_j@...> wrote:
>
> Hi,
> I have made a NetLogo model and now I need to integrate it with MATLAB. The model needs to send some parameters for MATLAB, and MATLAB must return some results for NetLogo. This way I can have a real-time simulation. I have no idea how to integrate them since I am a new user of NetLogo. I would appreciate it if you can help me with this problem.
> Anna
>


Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

sastre52
In reply to this post by mbiggs


mbiggs,

I downloaded your extension and I have it up and running using the 5.0.4 netlogo version and the R2012b version of matlab.  All I want netlogo to do is instruct matlab to run a certain m.file.  This m.file is where i created all the input data that gets read into netlogo through a txt file and then I run the simulation with this input data.  I need to the have an integrated program because i want to run monte carlo simulations (about 3000 of them) and I don't think it is practical to run matlab and then run netlogo manually 3000 times.  I used the example code that you give

        matlab:eval "a = 1 + 1"
        matlab:eval "c = a / 32.2"
        matlab:eval "b = {'efg' ; 'hi12'}"


        matlab:eval "initCobraToolbox()" ----> DOESN'T WORK

However, the last command does not work i get the following error from matlab:

eval done
Received cmd: 1
state = 1
command = initCobraToolbox()
empty command stream.


This is also the same error I get when I type in the command:

matlab:eval "testing.m"  ;; test.m is the m file that i created to generate the input data

I've also tried other permutations in case it's a syntax error.  I've tried:
matlab:eval "testing"
matlab:eval "testing.m()"
matlab:eval "data=testing.m"  ; data is a global variable in my netlogo code

none of these have worked.  Please help!







--- In [hidden email], mbiggs <mb3ad@...> wrote:

>
> I recently wrote an extension for Netlogo that links it with Matlab. Download
> and documentation are available  here
> <https://github.com/mbi2gs/netlogo-matlab-extension/wiki>  .
>
>
>
> --
> View this message in context: http://netlogo-users.18673.n6.nabble.com/integration-of-MATLAB-and-NetLogo-tp4871658p5002916.html
> Sent from the NetLogo-Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

NetLogo-Users mailing list
In reply to this post by bogdan_chivoiu
Hi Bogdan:
I notice that you have call python in Netlogo, how to do it? I am confused with use shell to do it, would you show me some code regarding how to run shell to call python script in netlogo?

Thanks very much!
Reply | Threaded
Open this post in threaded view
|

Re: integration of MATLAB and NetLogo

bogdan_chivoiu
Hi lishanghua,

I'm not sure if the response is still useful, but here is an example on how to run a Python script from Netlogo, after you install the "shell" extension.

1. Make a Netlogo model with just one button that calls the "runPythonScript" procedure.

2. Use the following code in the Netlogo model:

extensions [ shell ]

to runPythonScript

  set-current-directory "C:/../pathToFolderWithPythonScript/"
  let netlogoVar 10
  let pythonOutput (shell:exec "cmd" "/c" "c:/Python27/python.exe testScript.py" (word netlogoVar))
  print pythonOutput
 
end


3. Make a Python script called testScript.py such as this one (calculating 2 raised to the power "exponent"):

import math, sys

def computePower2(exponent):
    result = math.pow(2, float(exponent))
    print result

if __name__ == '__main__':
    computePower2(sys.argv[1])


4. When you click the button in the Netlogo model, you should see 1024 (2^10) printed in the Command Center.
This was tested with Netlogo 5.x and Python 2.7, but it should work with newer versions too.

I hope this is useful.
Bogdan