HIPS / Spearmint

Spearmint Bayesian optimization codebase
Other
1.55k stars 329 forks source link

Running a matlab file with spearmint #58

Open Mehrian opened 8 years ago

Mehrian commented 8 years ago

Dear all, I have a function to optimize that is written in MATLAB, as it is mentioned in instructions, I have changed the language in config.json file to 'MATLAB' and also, the branin.py has changed to branin.m (I've changed the lines with Matlab format as follows:)

function y = branin(x1,x2)

    y = (x1.^2)-x2+1);
end

%print 'Result = %f' % result
%time.sleep(np.random.randint(60))
%eturn result
%Write a function like this called 'main'
function y1 = main(job_id, params)
    print ('Anything printed here will end up in the output directory for job #%d') % job_id)
    print ('params')
   y1 = branin(params['x'], params['y']);
end

when I run this file using main.py in spearmint directory, I get this error in the output folder:

Job launching after 0.15 seconds in submission. Booting up Matlab... Traceback (most recent call last): File "/home/mohammad/Desktop/Bayesian/Spearmint-master/spearmint/launcher.py", line 240, in launch result = matlab_launcher(job) File "/home/mohammad/Desktop/Bayesian/Spearmint-master/spearmint/launcher.py", line 351, in matlab_launcher session.run("cd('%s')" % os.path.realpath(job['expt_dir'])) File "/home/mohammad/anaconda/lib/python2.7/site-packages/pymatlab/matlab.py", line 84, in run error_string))) RuntimeError: Error from Matlab: None Problem executing the function Job failed in 0.75 seconds. (<type 'exceptions.RuntimeError'>, RuntimeError('Error from Matlab: None',), <traceback object at 0x7f779d5ea290>)

I have to mention that initially, it asked me to install 'pymatlab', which I did and there is no more error about that. I really appreciate if anyone can help me to run this matlab file :) Best regards, Mohammad

Mehrian commented 8 years ago

No one can help me with this issue?

grisaitis commented 8 years ago

If you can't get Spearmint to (correctly) call your matlab file, I'd consider creating a python file that wraps the call and returns its output in a way that Spearmint should be able to interpret / process.

I would try reproducing your issue, but I unfortunately don't have matlab.

Sorry to hear about this issue. I can imagine your frustration... been there before.

DanielNScott commented 8 years ago

My solution for dealing with matlab's terrible lack of inter-operability with other languages is usually wrapping calls to it as well. It's easy to get data in (you can pass via command line invocation) but difficult to get data out in any way other than writing to disk. An alternative might be to use mcc and compile matlab to C, but this has always seemed like more hassle than it's worth to me.

DanielNScott commented 8 years ago

If you want I'd be happy to share the simple wrapper I created for calling matlab which has gotten me as far as issue #60

Mehrian commented 8 years ago

Hi Daniel and grisaitis, Thanks for your replies.... It would be great if you can share the wrapper Daniel..thanks. i have a question (which might seem very stupid as I'm new to spearmint!), Is it necessary to have Matlab installed in Linux OR the spearmint package is able to translate the code while we call it?

Also, for another function that I wrote in Python, which works fine with spearmint package, the results are being saved in 'output' folder, but I have problems reading and interpreting the output? Anyone knows how to visualize the output in terms of evaluation in each iteration? (e.g. making plots etc) Thanks in advance, Mohammad

JasperSnoek commented 8 years ago

I'm pretty sure you should have Matlab installed for this to work. It will call Matlab via the command line (so if you can't type 'matlab' in a terminal and have it open Matlab) it won't work.

Everything is being stored to the MongoDB database (i.e. results per iteration, etc.). I used to have a nice script to dump out the results so you could easily plot them with matplotlib, but I'm not sure I have it anymore.

Mehrian commented 8 years ago

Dear Jasper, I do have Matlab installed on the system, but I still get this error: ############################################# Job launching after 0.10 seconds in submission. Booting up Matlab... Traceback (most recent call last): File "/home/mohammad/Desktop/Bayesian/Spearmint-master/spearmint/launcher.py", line 240, in launch result = matlab_launcher(job) File "/home/mohammad/Desktop/Bayesian/Spearmint-master/spearmint/launcher.py", line 351, in matlab_launcher session.run("cd('%s')" % os.path.realpath(job['expt_dir'])) File "/home/mohammad/anaconda/lib/python2.7/site-packages/pymatlab/matlab.py", line 84, in run error_string))) RuntimeError: Error from Matlab: None Problem executing the function Job failed in 0.04 seconds. (<type 'exceptions.RuntimeError'>, RuntimeError('Error from Matlab: None',), <traceback object at 0x7fb52d58a248>) ############################################## The error comes from the matlab_launcher function. Also, would you please let me know where is the MongoDB data base located? Best wishes, Mohammad

DanielNScott commented 8 years ago

Here's a simple matlab interface using the file-system to pass objective values out of matlab.

simple_matlab_interf.zip

Mehrian commented 8 years ago

Hi Daniel, Thank you so much for the wrapper. I'm a bit confused for executing this wrapper! Lets say I consider x^2-y^2 as my function for optimization. Where should I include that in the wrappers and how to call them? (I have tested branin.py example and it worked)

function [ ] = run_job_smnt(job_id,params)
%RUN_JOB_SMNT Summary of this function goes here
%   Detailed explanation goes here

disp(job_id)
disp(params)

obj = sum(params);

fname = ['job_obj_' num2str(job_id) '.txt'];
fileID = fopen(fname,'w');
fprintf(fileID,'%10.5f',obj);
fclose(fileID);

end

Best wishes, Mohammad

DanielNScott commented 8 years ago

You'll need to replace my matlab code with whatever matlab code you want to execute, and once you've got an objective function calculated you can output it to the file system in the same way.