cvx_begin
variable A
variable B
maximize(5*A + 3*B)
subject to
A + B <= total_shelf_space
5*A + 3*B <= total_cost
A >= 0
B >= 0
A,B ∈Z
cvx_end
disp(A)
disp(B)
disp(P)
A,B ∈Z
is not a valid CVX statement. You will not find ∈
in the CVX Users’ Guide.
You apparently want A
and B
to be integer variables. If so, declare them to be integer per http://cvxr.com/cvx/doc/basics.html#variables .
cvx_begin
variable A integer
variable B integer
maximize(5*A + 3*B)
subject to
A + B <= total_shelf_space
5*A + 3*B <= total_cost
A >= 0
B >= 0
cvx_end
The message A =cvx real affine expression (scalar)
is because A,B
is the same as just typing A
at the command line (CVX is telling you what A
is), and then typing B
at the command line, and CVX will tell you what that is.
clear;clc;close all;
% 创建决策变量
x = intvar(1,2) ;
total_cost=15;
total_shelf_space=100;
% 添加约束条件
C = [
% x(1) integer
% x(2) integer
5x(1) + 3x(2) <= total_cost
x(2)+x(1) <=total_shelf_space
x(1)>=0
x(2)>=0
];
% 配置
ops = sdpsettings(‘solver’,‘cplex’);
% 目标函数
z = -(5x(1)+3x(2)); % 注意这是求解最大值
% 求解
reuslt = optimize(C,z);
if reuslt.problem == 0 % problem =0 代表求解成功
value(x)
-value(z) % 反转
else
disp(‘求解出错’);
end
这么写可以吗?