How do I include or make a discrete variable for an optimization problrm?
1 回表示 (過去 30 日間)
古いコメントを表示
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1524136/image.png)
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
0 件のコメント
回答 (2 件)
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 ...)
0 件のコメント
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with Optimization Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!