Skip to content

Commit 25a5751

Browse files
committed
Add clear_cached_params() method to mp.set_manager_opt_model and mp.opt_model.
1 parent d073478 commit 25a5751

File tree

12 files changed

+73
-19
lines changed

12 files changed

+73
-19
lines changed

CHANGES.md

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

8+
#### 12/11/25
9+
- Add `clear_cached_params()` method to `mp.set_manager_opt_model` and
10+
`mp.opt_model`, to clear aggregated parameters cached by the `params()`
11+
method.
812

913
#### 12/10/25
1014
- Add support for the Gurobi's new PDHG LP solver in `qps_gurobi()`.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3453,6 +3453,13 @@ \subsection{Miscellaneous Methods}
34533453

34543454
\subsubsection{Public Methods}
34553455

3456+
\subsubsection*{\code{clear\_cached\_params}}
3457+
\label{sec:clear_cached_params}
3458+
\begin{Code}
3459+
mm.clear_cached_params()
3460+
\end{Code}
3461+
The \code{clear\_cached\_params} method clears any parameters that were cached by the \code{params()} method of each of the set type objects in the \code{var}, \code{lin}, \code{qcn}, and \code{qdc} properties.
3462+
34563463
\subsubsection*{\code{copy}}
34573464
\label{sec:copy}
34583465
\begin{Code}
@@ -3780,6 +3787,12 @@ \subsubsection{MP Set Manager Math Model Methods}
37803787

37813788
See the online \href{https://matpower.org/doc/mpom/}{\mpom{} Reference Manual} for the implementing subclass for details.
37823789

3790+
\subsubsection*{\code{clear\_cached\_params}}
3791+
\begin{Code}
3792+
sm.clear_cached_params()
3793+
\end{Code}
3794+
The \code{clear\_cached\_params} method clears any parameters that were cached by the \code{params()} method.
3795+
37833796
\subsubsection*{\code{params}}
37843797
\begin{Code}
37853798
[...] = sm.params()
@@ -3872,6 +3885,7 @@ \subsubsection{Methods}
38723885
name & description \\
38733886
\midrule
38743887
\code{get\_set\_types} & return list of property names of set types managed by this class \\
3888+
\code{clear\_cached\_params} & clear cached parameters \\
38753889
\code{copy} & duplicate the object \\
38763890
\code{display} & displays the object \\
38773891
\code{display\_soln} & displays solution values \\
@@ -6723,6 +6737,7 @@ \subsubsection*{New Features}
67236737
\begin{itemize}
67246738
\item New \code{fix\_integer} option for \code{miqps\_master()} and \code{mp.opt\_model.solve()}. Set to true to fix any integer variables to their initial values, as specified in \code{x0} or \code{opt.x0}.
67256739
\item Add \code{relax\_integer} option to \code{miqps\_master()} and move the implementation from \code{mp.opt\_model.solve()}.
6740+
\item Add \code{clear\_cached\_params()} method to \code{mp.set\_manager\_opt\_model} and \code{mp.opt\_model}, to clear aggregated parameters cached by the \code{params()} method.
67266741
\item Support for the new HiPO interior point LP solver in the \highs{} optimizer.
67276742
\item Support for the new PDHG LP solver in \gurobi{}.
67286743
\item New functions:

lib/+mp/opt_model.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
% * copy - make a duplicate of the object
3232
% * to_struct - convert object data *to* a struct
3333
% * from_struct - copy object data *from* a struct
34+
% * clear_cached_params - clears parameters cached by set type objects
3435
% * get_idx - return ``idx`` struct for vars, constraints, costs
3536
% * get_userdata - used to retrieve values of user data
3637
% * problem_type - return string identifying type of mathematical program
@@ -256,6 +257,24 @@
256257
end
257258
end
258259

260+
function clear_cached_params(obj)
261+
% Clear cached parameters.
262+
% ::
263+
%
264+
% mm.clear_cached_params()
265+
%
266+
% Clears any parameters that were cached by set types in each of
267+
% the :attr:`var`, :attr:`lin`, :attr:`qcn` and :attr:`qdc`
268+
% properties by calling the
269+
% :meth:`clear_cached_params() <mp.set_manager_opt_model.clear_cached_params>`
270+
% method on each.
271+
272+
obj.lin.clear_cached_params();
273+
obj.var.clear_cached_params();
274+
obj.qcn.clear_cached_params();
275+
obj.qdc.clear_cached_params();
276+
end
277+
259278
function varargout = get_idx(mm, varargin)
260279
% Return ``idx`` struct for vars, constraints, costs.
261280
% ::

lib/+mp/set_manager_opt_model.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
% mp.set_manager_opt_model Methods:
1414
% * set_manager_opt_model - constructor
1515
% * params - *(abstract)* return set-type-specific parameter data
16+
% * clear_cached_params - clears parameters cached by params() method
1617
% * set_params - *(abstract)* modify set-type-specific parameter data
1718
% * display_soln - display solution values
1819
% * has_parsed_soln - return true if parsed solution is available
@@ -68,6 +69,21 @@
6869
% See also mp.set_manager.add, set_params.
6970
end
7071

72+
function clear_cached_params(obj)
73+
% Clear cached parameters.
74+
% ::
75+
%
76+
% sm.clear_cached_params()
77+
%
78+
% For subclasses that cache aggregated parameters in the
79+
% :attr:`cache` property, this method clears that cache, forcing
80+
% a rebuild on the next call to params().
81+
82+
try
83+
obj.cache = [];
84+
end
85+
end
86+
7187
function obj = set_params(obj, name, idx, params, vals)
7288
% Modify parameter data.
7389
% ::

lib/+mp/sm_lin_constraint.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
obj.data.vs = subsasgn(obj.data.vs, sc, vs);
206206
end
207207
if ~isempty(obj.cache) %% clear cache of aggregated params
208-
obj.cache = [];
208+
obj.clear_cached_params();
209209
end
210210
end
211211

@@ -579,7 +579,7 @@
579579
end
580580

581581
%% clear cached parameters
582-
obj.cache = [];
582+
obj.clear_cached_params();
583583

584584
%% update dimensions and indexing, if necessary
585585
dN = N - N0;

lib/+mp/sm_quad_constraint.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
obj.data.vs = subsasgn(obj.data.vs, sc, vs);
265265
end
266266
if ~isempty(obj.cache) %% clear cache of aggregated params
267-
obj.cache = [];
267+
obj.clear_cached_params();
268268
end
269269
end
270270

@@ -588,7 +588,7 @@
588588
end
589589

590590
%% clear cached parameters
591-
obj.cache = [];
591+
obj.clear_cached_params();
592592

593593
%% update dimensions and indexing, if necessary
594594
dMB = MB - MB0;

lib/+mp/sm_quad_cost.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
obj.data.vs = subsasgn(obj.data.vs, sc, vs);
256256
end
257257
if ~isempty(obj.cache) %% clear cache of aggregated params
258-
obj.cache = [];
258+
obj.clear_cached_params();
259259
end
260260
end
261261

@@ -599,7 +599,7 @@
599599
end
600600

601601
%% clear cached parameters
602-
obj.cache = [];
602+
obj.clear_cached_params();
603603

604604
%% update dimensions and indexing, if necessary
605605
dN = N - N0;

lib/+mp/sm_quad_cost_legacy.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
obj.data.vs = subsasgn(obj.data.vs, sc, vs);
239239
end
240240
if ~isempty(obj.cache) %% clear cache of aggregated params
241-
obj.cache = [];
241+
obj.clear_cached_params();
242242
end
243243
end
244244

@@ -590,7 +590,7 @@
590590
end
591591

592592
%% clear cached parameters
593-
obj.cache = [];
593+
obj.clear_cached_params();
594594

595595
%% update dimensions and indexing, if necessary
596596
dN = N - N0;

lib/+mp/sm_variable.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
obj.data.vt = subsasgn(obj.data.vt, sc, vt); %% variable type
175175
end
176176
if ~isempty(obj.cache) %% clear cache of aggregated params
177-
obj.cache = [];
177+
obj.clear_cached_params();
178178
end
179179
end
180180

@@ -465,7 +465,7 @@
465465
end
466466

467467
%% clear cached parameters
468-
obj.cache = [];
468+
obj.clear_cached_params();
469469

470470
%% update dimensions and indexing, if necessary
471471
dN = N - N0;

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', '10-Dec-2025' );
29+
'Date', '11-Dec-2025' );
3030
if nargout > 0
3131
if nargin > 0
3232
rv = v;

0 commit comments

Comments
 (0)