Skip to content

Commit 2500fb7

Browse files
committed
Rename all_continuous() method to are_all_continuous() and implement caching.
Uses all_continuous property to cache the current value, which is initialized and maintained by add() and set_params(). The are_all_continuous() method only re-evaluates from scratch if the all_continuous property is empty.
1 parent 53ff1fa commit 2500fb7

5 files changed

Lines changed: 50 additions & 24 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Change history for MP-Opt-Model
55
since version 5.0
66
-----------------
77

8-
#### 4/17/26
9-
- Move `mp.opt_model.is_mixed_integer()` logic into new `all_continuous()`
10-
method of `mp.sm_variable` and make the former a wrapper.
8+
#### 4/20/26
9+
- Move `mp.opt_model.is_mixed_integer()` logic into new
10+
`are_all_continuous()` method of `mp.sm_variable`, and make the former
11+
a simple wrapper.
1112
- Fix bug with parameter caching in `mp.sm_variable`, where calling
1213
`params()` without requesting the variable type would not cache it,
1314
so subsequent requests for the variable time incorrectly returned an

docs/src/MP-Opt-Model-manual/MP-Opt-Model-manual.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6742,7 +6742,7 @@ \subsubsection*{New Features}
67426742
\item Support for the new PDHG LP solver in \gurobi{}.
67436743
\item New methods:
67446744
\begin{itemize}
6745-
\item \code{mp.sm\_variable.all\_continuous()} method returns true if all variables are continuous, false otherwise.
6745+
\item \code{mp.sm\_variable.are\_all\_continuous()} method returns true if all variables are continuous, false otherwise.
67466746
\end{itemize}
67476747
\item New functions:
67486748
\begin{itemize}
@@ -6752,7 +6752,7 @@ \subsubsection*{New Features}
67526752

67536753
\subsubsection*{Bugs Fixed}
67546754
\begin{itemize}
6755-
\item Calling the \code{params()} method of \code{mp.sm\_variable} without less than 4 output arguments would prevent the variable type from being assembled and cached, resulting in an incorrect empty string being returned on subsequent calls with the fourth output argument present.
6755+
\item Calling the \code{params()} method of \code{mp.sm\_variable} with less than 4 output arguments would prevent the variable type from being assembled and cached, resulting in an incorrect empty string being returned on subsequent calls with the fourth output argument present.
67566756
\item
67576757
\emph{Thanks to Fulano.}
67586758
\end{itemize}

lib/+mp/opt_model.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ function clear_cached_params(obj)
536536
% TorF (logical): true or false, indicating whether any of the
537537
% variables are binary or integer
538538

539-
TorF = ~mm.var.all_continuous();
539+
TorF = ~mm.var.are_all_continuous();
540540
end
541541

542542
function TorF = is_solved(mm)

lib/+mp/sm_variable.m

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
% By convention, ``var`` is the variable name used for mp.sm_variable objects.
1212
%
1313
% mp.sm_variable Properties:
14+
% * all_continuous - true if all variables are continuous, false otherwise
1415
% * cache - struct for caching aggregated parameters for the set
1516
%
1617
% mp.sm_variable Methods:
@@ -21,6 +22,7 @@
2122
% * display_soln - display solution values for variables
2223
% * get_soln - fetch solution values for specific named/indexed subsets
2324
% * parse_soln - parse solution for variables
25+
% * are_all_continuous - return true if all variables are continuous, false otherwise
2426
% * varsets_idx - return vector of indices into full :math:`\x` corresponding to ``vs``
2527
% * varsets_len - return the total number of variables specified by ``vs``
2628
% * varsets_x - return subset of :math:`\x` specified by ``vs``
@@ -29,14 +31,15 @@
2931
% See also mp.set_manager, mp.set_manager_opt_model.
3032

3133
% MP-Opt-Model
32-
% Copyright (c) 2008-2024, Power Systems Engineering Research Center (PSERC)
34+
% Copyright (c) 2008-2026, Power Systems Engineering Research Center (PSERC)
3335
% by Ray Zimmerman, PSERC Cornell
3436
%
3537
% This file is part of MP-Opt-Model.
3638
% Covered by the 3-clause BSD License (see LICENSE file for details).
3739
% See https://github.com/MATPOWER/mp-opt-model for more info.
3840

3941
properties
42+
all_continuous = [];
4043
% struct for caching aggregated parameters for variables
4144
cache = [];
4245
end %% properties
@@ -173,6 +176,14 @@
173176
obj.data.vu = subsasgn(obj.data.vu, sc, vu); %% upper bound
174177
obj.data.vt = subsasgn(obj.data.vt, sc, vt); %% variable type
175178
end
179+
180+
%% update all_continuous
181+
if isempty(obj.all_continuous)
182+
obj.all_continuous = obj.are_all_continuous();
183+
else
184+
obj.all_continuous = obj.all_continuous && all(vt == 'C');
185+
end
186+
176187
if ~isempty(obj.cache) %% clear cache of aggregated params
177188
obj.clear_cached_params();
178189
end
@@ -214,7 +225,7 @@
214225
%
215226
% See also add, set_params.
216227

217-
if nargout > 3 || ~obj.all_continuous()
228+
if nargout > 3 || ~obj.are_all_continuous()
218229
have_vt = 1;
219230
else
220231
have_vt = 0;
@@ -472,6 +483,15 @@
472483
if is_all && dN
473484
obj.set_params_update_dims(dN, name, idx);
474485
end
486+
487+
%% update all_continuous
488+
if u.vt
489+
if isempty(obj.all_continuous)
490+
obj.all_continuous = obj.are_all_continuous();
491+
else
492+
obj.all_continuous = obj.all_continuous && all(p.vt == 'C');
493+
end
494+
end
475495
end
476496

477497
function obj = display_soln(obj, soln, varargin)
@@ -702,34 +722,39 @@
702722
end
703723
end
704724

705-
function TorF = all_continuous(obj)
706-
% Return true of all variables are continuous, false otherwise.
725+
function TorF = are_all_continuous(obj)
726+
% Return true if all variables are continuous, false otherwise.
707727
% ::
708728
%
709-
% TorF = var.all_continous();
729+
% TorF = var.are_all_continuous();
710730
%
711731
% Output:
712732
% TorF (logical) : true if all variables are continuous,
713733
% false otherwise.
714734

715-
TorF = true;
716-
if obj.get_N()
717-
for k = 1:length(obj.order)
718-
t = obj.data.vt.(obj.order(k).name);
719-
if iscell(t)
720-
for j = 1:length(t(:))
721-
if any(t{j} ~= 'C')
735+
if isempty(obj.all_continuous)
736+
%% evaluate and cache value
737+
TorF = true;
738+
if obj.get_N()
739+
for k = 1:length(obj.order)
740+
t = obj.data.vt.(obj.order(k).name);
741+
if iscell(t)
742+
for j = 1:length(t(:))
743+
if any(t{j} ~= 'C')
744+
TorF = false;
745+
break;
746+
end
747+
end
748+
else
749+
if any(t ~= 'C')
722750
TorF = false;
723751
break;
724752
end
725753
end
726-
else
727-
if any(t ~= 'C')
728-
TorF = false;
729-
break;
730-
end
731754
end
732755
end
756+
else %% use cached value in all_continous property
757+
TorF = obj.all_continuous;
733758
end
734759
end
735760

lib/mpomver.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
v = struct( 'Name', 'MP-Opt-Model', ...
2727
'Version', '5.1-dev', ...
2828
'Release', '', ...
29-
'Date', '17-Apr-2026' );
29+
'Date', '20-Apr-2026' );
3030
if nargout > 0
3131
if nargin > 0
3232
rv = v;

0 commit comments

Comments
 (0)