n_ads = 4; n_pos = 5; click_rates = [0.10 0.08 0.06 0.04; 0.08 0.06 0.04 0.02; 0.06 0.04 0.02 0.01; 0.04 0.02 0.01 0.005]; position_costs = [1 2 3 4 5]; % 定义 MILP 优化变量和限制条件 f = -reshape(click_rates, [], 1); Aeq = zeros(n_pos, n_ads * n_pos); beq = ones(n_pos, 1); for i = 1:n_pos for j = 1:n_ads Aeq(i, (i-1)n_ads+j) = 1; end end A = repmat(position_costs, n_ads, 1) . eye(n_ads * n_pos); b = ones(n_pos, 1) * 10; lb = zeros(n_ads * n_pos, 1); ub = ones(n_ads * n_pos, 1); intcon = 1:(n_ads * n_pos); [x, fval] = intlinprog(f, intcon, A, b, Aeq, beq, lb, ub); disp(reshape(x, n_pos, n_ads)'); 对于此运算,数组的大小不兼容,正确的修改后代码是什么样的
时间: 2024-03-25 07:38:03 浏览: 203
根据您提供的代码,应该是由于 A 矩阵的构造方式有误,导致数组大小不兼容。可以将 A 矩阵的构造方式修改为如下形式:
```
A = repmat(position_costs', n_ads, 1) .* kron(eye(n_ads), ones(n_pos, 1));
```
这样可以确保 A 矩阵的维度与实际数据相符,从而避免数组大小不兼容的错误。修改后的代码如下:
```
n_ads = 4;
n_pos = 5;
click_rates = [0.10 0.08 0.06 0.04; 0.08 0.06 0.04 0.02; 0.06 0.04 0.02 0.01; 0.04 0.02 0.01 0.005];
position_costs = [1 2 3 4 5];
% 定义 MILP 优化变量和限制条件
f = -reshape(click_rates, [], 1);
Aeq = zeros(n_pos, n_ads * n_pos);
beq = ones(n_pos, 1);
for i = 1:n_pos
for j = 1:n_ads
Aeq(i, (i-1)*n_ads+j) = 1;
end
end
A = repmat(position_costs', n_ads, 1) .* kron(eye(n_ads), ones(n_pos, 1));
b = ones(n_pos, 1) * 10;
lb = zeros(n_ads * n_pos, 1);
ub = ones(n_ads * n_pos, 1);
intcon = 1:(n_ads * n_pos);
[x, fval] = intlinprog(f, intcon, A, b, Aeq, beq, lb, ub);
disp(reshape(x, n_pos, n_ads)');
```
阅读全文