MATPOWER / most

MOST – MATPOWER Optimal Scheduling Tool, for steady-state power systems scheduling problems.
https://matpower.org/
Other
31 stars 11 forks source link

opt_model is a handle-class, create independent copies #16

Closed lordleoo closed 4 years ago

lordleoo commented 4 years ago

the MOST object opt_model is a handle class that is, if you say: mdi2.om = mdi.om; and change something on mdi2.om, you'll notice that the same change happened on mdi.om that's because mdi2.om and mdi.om are just handles, that point MATLAB to the same 1 object in matlab's memory. this can be annoying.

now consider this case: you've already built mdi without solving it, i.e:

mdi0 = loadmd(....);
mdo0 = most(mdi0,mpoption(most_opts,'most.build_model',1,'most.solve_model',0));

after that, you did some changes on mdo0, (added user-defined functions and variables), and proceeded to build mdo1:

mdo1 = most(mdo0,mpoption(most_opts,'most.build_model',0,'most.solve_model',1));

After that, any changes you carry out on mdo1 are going to affect mdo0 as well that's because inside MOST.m, for the case when most.build_model = 0, most.solve_model = 1; we fixed that old bug by saying: om = mdi.om;

Mathworks introduced a new method for handle classes to allow creating independent duplicates of a handle class. This is only in recent versions of matlab. the command would look like: mdi2.om = copy(mdi.om);

back to MOST.m code, instead of om = mdi.om; we can: om = copy(mdi.om); to isolate the two objects.

using the copy(handle_class) method requires making a very small change on the class definition in opt_model, instead of: classdef opt_model < handle it should be: classdef opt_model < matlab.mixin.Copyable % this here was handle. handle classes can NOT be copied

rdzman commented 4 years ago

Do you know in which version of MATLAB copy() and matlab.mixin.Copyable were introduced?

I'm guessing it does not work in Octave, but do you know if Octave has anything comparable?

Unfortunately, compatibility with relatively recent older MATLAB versions is important, and compatibility with Octave is critical.

lordleoo commented 4 years ago

This page here from Matlab documentation says it was introduced in 2011a

https://www.mathworks.com/help/matlab/ref/matlab.mixin.copyable.copy.html

I never used octave before.

rdzman commented 4 years ago

It looks like Octave does not (yet) implement the matlab.mixin.Copyable class, according to bug 51317 in their bug tracker.

However, the opt_model constructor can copy an opt_model object, and that does work in Octave. So we can simply use that.

Also, I've included it a little later in the code so that the output object is always a copy of the input object.