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

2 views (last 30 days)
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

Answers (2)

Bruno Luong
Bruno Luong on 29 Oct 2023
Edited: Bruno Luong on 29 Oct 2023
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 on 29 Oct 2023
Edited: Torsten on 29 Oct 2023
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.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!