fmincon不会满足非线性约束

时间:2017-12-15 18:26:54

标签: matlab mathematical-optimization

我读过一篇关于带有能量函数的软色彩分割的论文,必须针对某些约束进行优化。用于包裹柔和的色彩分割。您可以定义一定数量的图层,每个图层代表一些颜色(具有平均值和变化)。这些图层中的每个像素都具有rgb颜色和alpha值。

如上所述,本文介绍了一种能量函数,必须针对每层的alpha值和颜色值进行优化:

F_s = \ sum_i \ alpha_i D_i(\ mathbf {u_i})+ \ sigma \ left(\ frac {\ sum_i \ alpha_i} {\ sum_i \ alpha_i ^ 2} - 1 \ right)

function obj = costFunction(x, rows, columns, mean)
 [alphaLayers, layerColors] = unfoldFeatures(x, rows, columns, size(mean, 1));

 sigma = 10;

 [dimModel, ~] = size(mean);

 distance = layerColors - reshape(mean, 1, 1, 3, dimModel);

 unmixingEnergy = ( alphaLayers .* (sqrt( sum((distance.^2), 3))) );
 unmixingEnergy = sum( unmixingEnergy(:) )  ;
 sparsityTerm =  sigma * ( (sum(alphaLayers(:)) / (sum(alphaLayers(:) .^2))) - 1 );

 obj = unmixingEnergy + sparsityTerm;
end

约束很简单,每个像素的alpha值之和必须为1,并且每个图层的组合必须重现原始图像。

function [C, Ceq]= equalityConstrains(x, rows, columns, dimModel, originalPicture)
[alphaLayer, layerColors] = unfoldFeatures(x, rows, columns, dimModel);
  alphaConstraint = (sum(alphaLayer, 3) - 1);
  temp = zeros(size(originalPicture), 'double');
  for i = 1:dimModel
     temp = temp + layerColors(:, :, :, i) .* alphaLayer(:, :, i);
  end
  colorConstraint = abs(temp(:) - originalPicture(:)) * -1;
  %colorConstraint = colorConstraint) * -1; %fmincon needs the constraint to be zero or negative
  Ceq  = [ (alphaConstraint(:));    
         (colorConstraint(:))      
        ];  
   C = [];
end

我使用fmincon作为优化器,调用如下:

x0 = foldFeature(alphaLayer, layerColors);
phi = @(x) costFunction(x, r, c , double((mean)));
A = [];
b = [];
Aeq = [];
beq = [];
lb = zeros(1, length(x0), 'double') - eps;
ub = ones(1, length(x0), 'double') + eps;
nonlcon = @(x)equalityConstrains(x, r, c, m, uniqueImage);
[x, fval, exitflag, lambda] = fmincon(phi, x0, A, b, Aeq, beq, lb, ub, nonlcon, options);

我现在的问题是,虽然优化器减少了前2-6次迭代的成本函数,但是不满足约束条件。在迭代6之后,成本增加并且仍然违反约束。 这是错误的方法还是我需要使用另一个algortihm?我试过sqp和内点。我的照片缩小到100到100像素。初始值的限制是可以的。折叠和展开功能是前进的:

function folded = foldFeature(alphaLayers, layerColors)
  folded = [alphaLayers(:);layerColors(:)];  
end 


function [alphaLayers, layerColors] = unfoldFeatures(x, rows, columns, dimModel)
  alphaLayersSize = rows*columns*dimModel;
  alphaLayers = reshape(x(1:alphaLayersSize), rows, columns, dimModel);
  layerColors = reshape(x(alphaLayersSize+1:end), rows, columns, 3, dimModel);
end 

0 个答案:

没有答案