Undefined operator '-' for input arguments of type 'cell' in code that worked ~2 years ago

1 view (last 30 days)
I'm trying to revive an optimization utility that I coded up a couple of years ago. The complete code relies on reading in some data files so I can't post a complete minimal working (or non-working) example, but I'll try to be as thorough as I can in hopes this is an easy fix. I'm not a frequent MatLab user, so I imagine things may have changed since I wrote this....
The very short version is that I get the subject error on this line of code
c(i)= ((1-G) .* X(:,i)) - X(:,i+1);
This is in a function that handles the constraints for an optimization problem. X is passed in, and represents a candidate solution, so I don't understand how it's of type cell.
Here's a longer walk through the code
%we used file dialog boxes to get the location of the input file
[num, txt, raw] = xlsread(openFile,xlSheet,xlRange);
%start gathering up the problem parameters
%requirements matrix
R = num(1:numActivities,3:7);
%weights matrix
W = num(1:numActivities,2);
%available funding
maxFunds = num(1,19:23);
%intial solution - all zeros
x0 = zeros(numActivities,5);
%lower bounds is the same
lb = x0;
%upper bounds is the requirements
ub = R;
%need to ask for a turbulence factor
G = inputdlg('Please enter the turbulence factor. This is a number between 0 and 1.','Turbulence Factor',1,{'0.15'});
%I told you this was old code...
%at work version 2014b, and then the options are
%MaxFunEvals, MaxIter
%at home version 2017a, and the options are
%MaxFunctionEvaluations, MaxIterations
if verLessThan('matlab','9.2.0')
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunEvals',100000,'MaxIter',100000);
else
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',100000,'MaxIterations',100000);
end
p = gcp('nocreate'); % if no pool, don't create a new one
if isempty(p)
parpool;
end
ms=MultiStart('UseParallel',true);
%In reality there are several different ways of handling the constraints
%and the objective function. I use dialogs to get the selections from the
%user and there would be an if statement here to use the right
%@function_names
%For simplicity I'm just including this one...it throws the error
problem = createOptimProblem('fmincon','objective',@base_objective,'nonlcon',@turbulence_constraints,'x0',x0,'lb',lb,'ub',ub,'options',options);
[x, fval] = run(ms,problem,100)
% we have to go ahead and declare all of the functions
% and then use the ones we want
% base constraints
function [ c, ceq ] = base_constraints(X)
%base_constraints multi-year constraints function
% no turbulence
% matrix X is the decision variables (funding levels)
% matrix R is the requirements
% ub and lb take care of our upper and lower bounds for X
% so all we really need to ensure is that the totals
% don't exceed each year's funding
S=sum(X);
c=[];
ceq = [S(1) - maxFunds(1);
S(2) - maxFunds(2);
S(3) - maxFunds(3);
S(4) - maxFunds(4);
S(5) - maxFunds(5)];
end
% turbulence constraints
function [ c, ceq ] = turbulence_constraints(X)
% G is the turbulence factor
% year N can't be a decrease of more than G% from year N-1
% matrix X is the decision variables (funding levels)
% matrix R is the requirements
% ub and lb take care of our upper and lower bounds for X
% so all we really need to ensure is that the totals
% don't exceed each year's funding
% and that the turbulence requirement is met
S=sum(X);
c=zeros(4,1);
for i = 1:4
c(i)= ((1-G) .* X(:,i)) - X(:,i+1);
end
ceq = [S(1) - maxFunds(1);
S(2) - maxFunds(2);
S(3) - maxFunds(3);
S(4) - maxFunds(4);
S(5) - maxFunds(5)];
end
I hope that's enough info to be useful in figuring this out...any help is most appreciated!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2021
G = inputdlg('Please enter the turbulence factor. This is a number between 0 and 1.','Turbulence Factor',1,{'0.15'});
inputdlg() always returns a cell array, never a numeric value. It has never returned a numeric value.
c(i)= ((1-G) .* X(:,i)) - X(:,i+1);
with your spacing, that just might be inside a nested function and so G might be a shared variable here. But G is a cell array, not a number.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!