Exemplo n.º 1
0
class MatlabCaller:
    """
    bridging class with matlab
    """

    def __init__(self, port=14001, id=None):
        if id is None:
            id = numpy.random.RandomState(1234)

        # Initialise MATLAB
        self.mlab = Matlab(port=port, id=id)

    def start(self):
        # Start the server
        self.mlab.start()

    def stop(self):
        self.mlab.stop()
        os.system('pkill MATLAB')


    def call_fn(self, path=None, params=None):
        """
        path: the path to your .m function
        params: dictionary contains all parameters
        """
        assert path is not None
        assert isinstance(params, dict)

        return self.mlab.run(path, params)['result']
class mc_mat(gym.Env):
    def __init__(self):
        self.mlab = Matlab(matlab='/Applications/MATLAB_R2014a.app/bin/matlab')
        self.mlab.stop()
        self.mlab.start()

        self.action_space = spaces.Discrete(4)
        self.observation_space = spaces.Discrete(4)
        self.round = 2

        self.LAM = round(random.uniform(0.1, 20), 3)
        self.D = round(np.random.uniform(5, 40), 2)
        self.N_n = np.random.randint(5, 20)
        self.N_0 = round(random.uniform(5, 20), 3)
        # self.observation = np.array([self.LAM, self.D, self.N_n, self.N_0])#.reshape(1,4)
        self.observation = (self.LAM, self.D, self.N_n, self.N_0
                            )  #.reshape(1,4)

        self._reset()

    def _step(self, action):
        assert self.action_space.contains(action)

        res = self.mlab.run(
            '/Users/zonemercy/Documents/MATLAB/mc_gym/Copy_of_oraginal.m', {
                'arg1': 1,
                'arg2': self.LAM,
                'arg3': self.D,
                'arg4': self.N_0,
                'arg5': self.N_n
            })

        # esb = res['result']
        esb = [round(elm, self.round) for elm in res['result']]
        result = np.where(esb == np.min(esb))[0]

        if action in result:
            reward = 10
            done = True
        else:
            reward = 0
            done = False

        # return self.observation, reward, done, {"action": action, "result": result,'esb':esb}
        return self.observation, reward, done, {}

    def _reset(self):
        self.LAM = round(random.uniform(0.1, 20), 3)
        self.D = round(np.random.uniform(5, 40), 2)
        self.N_n = np.random.randint(5, 20)
        self.N_0 = round(random.uniform(5, 20), 3)
        # self.observation = np.array([self.LAM, self.D, self.N_n, self.N_0])#.reshape(1,4)
        self.observation = (self.LAM, self.D, self.N_n, self.N_0)

        return self.observation

    def _configure(self, nround=3, close_mat=False):
        self.round = nround
        if close_mat == True:
            self.mlab.stop()
Exemplo n.º 3
0
from pymatbridge import Matlab

# Initialise MATLAB
mlab = Matlab(port=4000)
# Start the server
mlab.start()
# Run a test function: just adds 1 to the argument a
for i in range(10):
    print mlab.run('~/Sites/python-matlab-bridge/test.m', {'a': i})['result']
# Stop the MATLAB server
mlab.stop()
Exemplo n.º 4
0
from pymatbridge import Matlab
mlab = Matlab(matlab='/Applications/MATLAB_R2014a.app/bin/matlab')
mlab.start()
res = mlab.run('/Users/yanguango/Lab/SpamFilter/Project/src/PCA.m', {'arg1': 3, 'arg2': 5})
print res['result']
Exemplo n.º 5
0
            scaled_for_color = math.ceil(32 * pixel_normalized_val + 32)
            if scaled_for_color == 0:
                scaled_for_color = 1
            group["intelligent_icon"][row][column] = scaled_for_color
    if add_struct_comma_to_expr:
        matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "," 
    else:
        add_struct_comma_to_expr = True   
    matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "struct('title', '" + group_name + "', 'patches', ["
    add_pixel_comma_to_expr = False
    #flatten the bitmap into a vector of pixels (see matlab/show_icons.m)
    for row in range(row_pixel_count):
        for column in range(row_pixel_count):
            if add_pixel_comma_to_expr:
                matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + ","
            else:
                add_pixel_comma_to_expr = True 
            matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + str(group["intelligent_icon"][row][column])
    matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "])"
matlab_intelligent_icon_visualization_data_expr = matlab_intelligent_icon_visualization_data_expr + "]"

print("rendering intelligent icon grid")
#initialise matlab bridge
mlab = Matlab(port=4000, matlab=matlab_path)
mlab.start()
#i cant imagine more ghetto way to do this, but it seems to be the only way to pass this data through some bug in the data parsing part of the bridge (Which i didnt write, for the record!!!)
icon_data_expr_encoded = base64.b64encode(matlab_intelligent_icon_visualization_data_expr)
print(icon_data_expr_encoded)
mlab.run(base_dir + "/matlab/show_icons.m", {"encoded": icon_data_expr_encoded})

print("done without errors")
Exemplo n.º 6
0
'''
Created on 25 sep 2015

@author: fragom
'''
from pymatbridge import Matlab

if __name__ == '__main__':
    mlab= Matlab(matlab='C:/Program Files/MATLAB/R2012b/bin/matlab.exe')
    mlab.start()
    for i in range(10):
        print mlab.run('C:/IDE/python-matlab-bridge/test.m',{'a':i})['result']
    mlab.stop()