How do I include or make a discrete variable for an optimization problrm?

3 次查看(过去 30 天)
This is the code I made and I got a values: x1=3 x2=8 x3=3 x4=3 x5=0 x6=0 x7=11
but x1 result is not in the group of numbers {2,5,7,8}
please help here is the code
clear all;
clc;
% Define the objective function coefficients
f = [2; -6; 3; 4; 7; 3; 2];
% Define the inequality constraints in matrix form
A = [0 -1 -5 -3 1 -2 -3;
6 3 2 -1 -2 2 0;
-7 -4 3 -4 3 -2 -1];
% Define the right-hand side of the inequality constraints
b = [-65; 73; -66];
% Define the lower and upper bounds for each variable
lb = [2; 2; 3; 3; 0; 0; -Inf];
ub = [8; 8; 6; 6; Inf; Inf; Inf];
% Define integer variables (1 for discrete variables, 0 for continuous)
intcon = [1 2 3 4]; % Only x1 and x2 are discrete
% Solve the mixed-integer linear programming problem
options = optimoptions('intlinprog', 'Display', 'off');
[x, fval, exitflag] = intlinprog(f, intcon, A, b, [], [], lb, ub, options);
% Display the optimal solution and objective function value
if exitflag == 1
disp('Optimal Solution:')
disp(x);
disp('Optimal Objective Function Value:')
disp(fval);
else
disp('No optimal solution found.');
end

回答(2 个)

Bruno Luong
Bruno Luong 2023-10-29
编辑:Bruno Luong 2023-10-29
To restrict x1 to { 2, 5, 7, 8 }
You might make slack variables
x1 = 2 + 3*y1 + 2*y2 + 1*y3;
with constrants
y1, y2, y3 integers in {0, 1} and 0 <= y3 <= y2 <= y1
So the same thing for x2, x3, x4, then optimize wrt (x5, x6, x7, y1, y2 ...)

Torsten
Torsten 2023-10-29
编辑:Torsten 2023-10-29
Make a loop over the 4*4*3*3 = 144 possible combinations of x1,x2,x3 and x4 and only optimize with respect to x5, x6 and x7.
In this case, you can use "linprog" to solve the subproblems.
Of course - if problems become larger - this way of solving will have its limits.

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by