Error Using Optimization with Integer Variables and Non-linear Constraints

7 visualizaciones (últimos 30 días)
Hi guys! I would be very grateful if you could help me!
I'm trying to minimize the norm of a vector using optimization algorithms in MATLAB, but I'm getting an error that says "Problems with integer variables and nonlinear equality constraints are not supported."
Could someone help me understand how I can solve this and minimize the norm of the vector?
Here's the implemented code:
wpass = 0.0318*pi;
wstop = 0.15*pi;
pb_ripple = 0.03;
sb_ripple = 0.03;
n = 25;
l = 7;
k = 2^l;
N = 15*n;
w = linspace(0,pi,N);
A = [ones(N,1) 2*cos(kron(w',1:n))];
Ap = A((0 <= w) & (w <= wpass),:);
As = A((wstop <= w) & (w <= pi),:);
H_dac=sinc(w);
H_dac_p=H_dac(1,(0 <= w) & (w <= wpass));
H_dac_s=H_dac(1,(wstop <= w) & (w <= pi));
h = optimvar('h',(n+1), 'Type','integer','LowerBound',-inf,'UpperBound',inf);
norm_h=optimvar('norm_h','Type','integer');
prob = optimproblem('ObjectiveSense','minimize');
prob.Constraints.cons1 = norm_h==norm(cat(1,h(end:-1:2),h),1);
prob.Objective = norm_h;
prob.Constraints.cons2 = Ap*h <= k*(1 + pb_ripple);
prob.Constraints.cons3 = Ap*h >= k*(1 - pb_ripple);
prob.Constraints.cons4 = As*h <= k*(sb_ripple);
prob.Constraints.cons5 = As*h >= -k*(sb_ripple);
sol = solve(prob);

Respuestas (1)

Torsten
Torsten el 28 de Abr. de 2024
Editada: Torsten el 28 de Abr. de 2024
Replace
norm_h=optimvar('norm_h','Type','integer');
prob = optimproblem('ObjectiveSense','minimize');
prob.Constraints.cons1 = norm_h==norm(cat(1,h(end:-1:2),h),1);
prob.Objective = norm_h;
by
%norm_h=optimvar('norm_h','Type','integer');
prob = optimproblem('ObjectiveSense','minimize');
%prob.Constraints.cons1 = norm_h==norm(cat(1,h(end:-1:2),h),1);
prob.Objective = norm(cat(1,h(end:-1:2),h),1);
Since h is integer valued, its 1-norm will also be integer-valued automatically.
The ga solver does not accept integer variables together with nonlinear equality constraints.
I think you can formulate the problem such that "intlinprog" can be used to solve it. This has several advantages over "ga" (which will most probably be chosen by MATLAB as solver for your problem).
  2 comentarios
Evelyn Lima
Evelyn Lima el 29 de Abr. de 2024
Dear Torsten,
Thank you for your reply, but it's still not working.
I'm trying to change the problem a little (using sqrt and .^), like this:
h = optimvar('h',n+1,'Type','integer');
Obj = sqrt(sum(cat(1,h(end:-1:1),h).^2));
prob = optimproblem("Objective",Obj);
x0.h = randi([0 0],n+1,1);
[sol,fval] = solve(prob,x0)
in this format, it works.
But once I add the constraints,
h = optimvar('h',n+1,'Type','integer');
Obj = sqrt(sum(cat(1,h(end:-1:1),h).^2));
prob = optimproblem("Objective",Obj);
prob.Constraints.cons2 = Ap*h <= k*(1 + pb_ripple);
prob.Constraints.cons3 = Ap*h >= k*(1 - pb_ripple);
prob.Constraints.cons4 = As*h <= k*(sb_ripple);
prob.Constraints.cons5 = As*h >= -k*(sb_ripple);
x0.h = randi([0 0],n+1,1);
[sol,fval] = solve(prob,x0)
I get a new error :
"Error using optim.problemdef.OptimizationProblem/solve
Incorrect size(model.obj)"
Torsten
Torsten el 29 de Abr. de 2024
Editada: Torsten el 29 de Abr. de 2024
Which MATLAB version do you use ?
pb_ripple = sb_ripple = 0.03 gives the message that the linear bounds are infeasible.
For pb_ripple = sb_ripple = 0.3, your problem is solved by "ga".
If you use the 1-norm or infinity-norm instead of the 2-norm in your objective function, you can formulate the problem such that MATLAB is able to use "intinprog" instead of "ga" for its solution. This would be much faster and more reliable.
wpass = 0.0318*pi;
wstop = 0.15*pi;
pb_ripple = 0.3;
sb_ripple = 0.3;
n = 5;
l = 7;
k = 2^l;
N = 15*n;
w = linspace(0,pi,N);
A = [ones(N,1) 2*cos(kron(w',1:n))];
Ap = A((0 <= w) & (w <= wpass),:);
As = A((wstop <= w) & (w <= pi),:);
H_dac=sinc(w);
H_dac_p=H_dac(1,(0 <= w) & (w <= wpass));
H_dac_s=H_dac(1,(wstop <= w) & (w <= pi));
h = optimvar('h',n+1,'Type','integer');
Obj = sqrt(sum(cat(1,h(end:-1:1),h).^2));
prob = optimproblem("Objective",Obj);
prob.Constraints.cons2 = Ap*h <= k*(1 + pb_ripple);
prob.Constraints.cons3 = Ap*h >= k*(1 - pb_ripple);
prob.Constraints.cons4 = As*h <= k*(sb_ripple);
prob.Constraints.cons5 = As*h >= -k*(sb_ripple);
x0.h = randi([0 0],n+1,1);
[sol,fval] = solve(prob,x0)
Solving problem using ga. ga stopped because the average change in the penalty function value is less than options.FunctionTolerance and the constraint violation is less than options.ConstraintTolerance.
sol = struct with fields:
h: [6x1 double]
fval = 28.7402
sol.h
ans = 6x1
5 9 9 9 9 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Iniciar sesión para comentar.

Categorías

Más información sobre Surrogate Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by