Call Matlab from Python to optimize

Introduction

Have you ever wanted to optimize a function created in Matlab with the optimization software (Optune this time) in python to make it feel good? I thought I could do something, so I tried it.

Operating environment

Matlab R2018a Win10 python 3.6 (Anaconda)

Advance preparation

It is premised that python contains 3.5 or 3.6 (R2020 may be OK with a little further ver?) I put 3.6 in pyenv.

--Allow matlabengine to be loaded http://yusuke-ujitoko.hatenablog.com/entry/2019/08/06/212756
Open the anaconda prompt with "administrator authority" referring to here and type the following command At this time, anaconda3 \ "envs \ py36" is for those who have not installed anaconda in the virtualenv environment. Is it unnecessary?

cd C:\Program Files\MATLAB\R2018a\extern\engines\python
python setup.py install --prefix="C:\Users\****\anaconda3\envs\py36"

**** is the username. Now python and matlab are linked.

Execution code

First, the matlab function to execute is as follows. Put this in the same folder as the following python code.

function y = func(x)
y = (x-3)^2;

After that, start Spyder and execute the following command on spyder

import sys 
sys.path.append("C:\\Users\\****\\anaconda3\\envs\\py36lib\\site-packages")

import matlab.engine
import optuna


def objective(trial):
    x = trial.suggest_uniform('x', -10, 10)
    #Now read the Matlab function and return the score for x
    score =  eng.func(x)
    return score

#Main function from here
eng = matlab.engine.start_matlab()
study = optuna.create_study()
study.optimize(objective, n_trials=1000)

Execution result

The Matlab function func is called on python as shown below, and the result is optimized by Optune. (In this case, the minimum value of 3 can be predicted with 1000 tries). This time it's a simple function, so you don't have to search for it, but it would be easier if you could optimize the complex functions you created in the past. As for variables, python's "x = trial.suggest_uniform ('x', -10, 10)" is simply increased to x1, x2 ... Even those who are good at Matlab and not good at python can optimize various things.

abridgement
[I 2020-09-21 20:52:03,537] Trial 997 finished with value: 0.611554463759686 and parameters: {'x': 2.217980522135359}. Best is trial 551 with value: 3.107692752820123e-07.
[I 2020-09-21 20:52:03,549] Trial 998 finished with value: 1.5139869920248414 and parameters: {'x': 4.230441787336907}. Best is trial 551 with value: 3.107692752820123e-07.
[I 2020-09-21 20:52:03,560] Trial 999 finished with value: 0.5050438774658341 and parameters: {'x': 3.7106643915842654}. Best is trial 551 with value: 3.107692752820123e-07.

study.best_value
Out[14]: 3.107692752820123e-07

study.best_params
Out[15]: {'x': 2.9994425331621684}

in conclusion

There was no space to paste the sushi license. Optuna is amazing. But I'm sure Matlab also has a function (toolbox) that can do this ... Please let me know.

Recommended Posts

Call Matlab from Python to optimize
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Call Python library for text normalization from MATLAB
An easy way to call Java from Python
Call CPLEX from Python (DO cplex)
Post from Python to Slack
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
I wanted to use the Python library from MATLAB
[Python] How to call a c function from python (ctypes)
Call C language functions from Python to exchange multidimensional arrays
Call a Python function from p5.js.
Call C from Python with DragonFFI
Create folders from '01' to '12' with python
Post from python to facebook timeline
[Lambda] [Python] Post to Twitter from Lambda!
Call popcount from Ruby / Python / C #
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
Call python from nim with Nimpy
How to access wikipedia from python
Python to switch from another language
Go language to see and remember Part 8 Call GO language from Python
Call C / C ++ from Python on Mac
How to call Python or Julia from Ruby (experimental implementation)
Call c language from python (python.h)
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
Simple code to call a python program from Javascript on EC2
Call dlm from python to run a time-varying coefficient regression model
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Send a message from Python to Slack
Private Python handbook (updated from time to time)
I want to use jar from python
Convert from katakana to vowel kana [python]
Push notification from Python server to Android
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert from Markdown to HTML in Python
Call a command from Python (Windows version)
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Updated to Python 2.7.9
C language to see and remember Part 2 Call C language from Python (argument) string
Sum from 1 to 10
sql from python
C language to see and remember Part 1 Call C language from Python (hello world)
MeCab from Python
C language to see and remember Part 4 Call C language from Python (argument) double
"Backport" to python 2
C language to see and remember Part 5 Call C language from Python (argument) Array
Send a message from Slack to a Python server