How can write a max expression in constraint using problem based optimization?

hi everyone. I want to write the following constraint in matlab using problem based optimization. But this error is appeared: Error using max Invalid data type. First argument must be numeric or logical. My constraint is: cons1 = x == max(a,b); a and b are decision variables.

4 Kommentare

The function max does not support decision variables as inputs, as stated in the error message.
It is difficult to provide any suggest/solution without additional information.
Please share -
> The mathematical definition of the optimization problem.
> The code you have attempted to solve the problem.
tic
costprob = optimproblem;
rng default
% Indices
i = 4; k = 5;
% Parameters
p_ik = [5 5 3 6 3; 4 4 2 4 4; 4 4 3 4 1; 3 6 3 2 5];
% Variables
c_ik = optimvar('c_ik',i,k,'Type','integer','LowerBound',0);
Obj_Fun = optimvar('Obj_Fun',1,1,'Type','integer','LowerBound',0);
% Objective function
objfun1 = Obj_Fun;
costprob.Objective = objfun1;
% Constraints
cons1 = optimconstr(i,k);
for K=1:1
cons1(:,K) = c_ik(:,K)== sum(p_ik(:,K),1);
end
cons2 = optimconstr(i,k);
for I=1:1
cons2(I,:) = c_ik(I,:) == sum(p_ik(I,:),2);
end
cons3 = optimconstr(i,k);
for I=2:i
for K=2:k
cons3(I,K) = c_ik(I,K) == p_ik(I,K)+max(c_ik(I-1,K),c_ik(I,K-1));
end
end
cons4 = Obj_Fun == max(c_ik);
costprob.Constraints.cons1 = cons1;
costprob.Constraints.cons2 = cons2;
costprob.Constraints.cons3 = cons3;
costprob.Constraints.cons4 = cons4;
% Solve
[sol,fval,exitflag] = solve(costprob);
toc
S AsZ
S AsZ am 1 Jan. 2024
Bearbeitet: S AsZ am 1 Jan. 2024
I added my m file at my question
Torsten
Torsten am 1 Jan. 2024
Bearbeitet: Torsten am 1 Jan. 2024
Usually, max(a,b) can be implemented by introducing a new variable p with constraints a <= p, b <= p.
This means a scalar p with c_ik <= p with a new variable p.
This means a matrix pIK with pIK(I-1,K-1) >= c_ik(I-1,K), pIK(I-1,K-1) >= c_ik(I,K-1) (I=2,...,i, K = 2,...k) with a new array pIK.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Produkte

Version

R2019b

Gefragt:

am 1 Jan. 2024

Bearbeitet:

am 1 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by