一种一般线性约束下的生成随机数修正方法

目录

MATLAB代码

linear_inequalities_constraints_correction函数

matlab 复制代码
function [X_cor, correction_info] = linear_inequalities_constraints_correction(X_gen, lb, ub, A_ieq, b_ieq, ...
                                    W_multiplicator)
    % LINEAR_INEQUALITIES_CONSTRAINTS_CORRECTION  Correction onto generated random vector.
    %                                             A methodology of correction onto generated random vector,
    %                                             facilitating the corrected vector to range within variable
    %                                             boundaries and to meet linear constraints

    if ~exist('W_multiplicator', 'var')
        W_multiplicator = 1000;
    end
    max_runtime = ceil(W_multiplicator / size(X_gen, 2));

    M = size(A_ieq, 1);
    [N_D, N_P] = size(X_gen);
    
    b_ieq_lb = diag(A_ieq * ((A_ieq > 0)' .* lb + (A_ieq < 0)' .* ub));
    b_ieq_ub = min([diag(A_ieq * ((A_ieq > 0)' .* ub + (A_ieq < 0)' .* lb)), b_ieq], [], 2);
    
    if any(b_ieq < b_ieq_lb)
        X_cor = [];
        correction_info.is_feasible = false(1, N_P);
        correction_info.does_X_cor_exceed.by_bit = true(N_D, N_P);
        correction_info.does_X_cor_exceed.globally = true(1, N_P);
        correction_info.is_inequality_less_than_b.by_bit = false(M, N_P);
        correction_info.is_inequality_less_than_b.globally = false;

    else
        X_cor = X_gen;

        run = 0;
        while 1
            X_cor = X_gen;
            iEq_gen = A_ieq * X_gen;
            does_A_ieq_X_gen_exceed = (iEq_gen > b_ieq);
    
            for indi = 1:N_P
                if any(does_A_ieq_X_gen_exceed(:, indi))
                    flag_A_ieq_X_gen_exceed = find(does_A_ieq_X_gen_exceed(:, indi));
                    row_A_ieq_candi = flag_A_ieq_X_gen_exceed(randi(length(flag_A_ieq_X_gen_exceed)));
                    A_ieq_candi = A_ieq(row_A_ieq_candi, :);
                    Eq_candi_gen = A_ieq_candi * X_gen(:, indi);
                    b_ieq_k_lb = b_ieq_lb(row_A_ieq_candi, :);
                    b_ieq_k_ub = b_ieq_ub(row_A_ieq_candi, :);
                    delta_plus = A_ieq_candi * ((A_ieq_candi > 0)' .* (X_gen(:, indi) - lb) ...
                        + (A_ieq_candi < 0)' .* (X_gen(:, indi) - ub));
                    x_cor_base = zeros(N_D, 1);
    
                    if Eq_candi_gen > b_ieq_k_ub
                        b_eq_k_best = [b_ieq_k_lb + (delta_plus * (b_ieq_lb - A_ieq * ((A_ieq_candi > 0)' ...
                            .* lb + (A_ieq_candi < 0)' .* ub + (A_ieq_candi == 0)' .* X_gen(:, indi)))) ...
                            ./ (A_ieq * ((A_ieq_candi > 0)' .* (X_gen(:, indi) - lb) ...
                            + (A_ieq_candi < 0)' .* (X_gen(:, indi) - ub))), ...
                            b_ieq_k_lb + (delta_plus * (b_ieq_ub - A_ieq * ((A_ieq_candi > 0)' ...
                            .* lb + (A_ieq_candi < 0)' .* ub + (A_ieq_candi == 0)' .* X_gen(:, indi)))) ...
                            ./ (A_ieq * ((A_ieq_candi > 0)' .* (X_gen(:, indi) - lb) ...
                            + (A_ieq_candi < 0)' .* (X_gen(:, indi) - ub)))];
                        b_eq_k_min = max(min(b_eq_k_best, [], 2), [], 1);
                        b_eq_k_max = min(max(b_eq_k_best, [], 2), [], 1);
                        if b_eq_k_min <= b_eq_k_max
                            b_eq_k = b_eq_k_min + rand * (b_eq_k_max - b_eq_k_min);
                        else
                            b_eq_k = b_eq_k_best(row_A_ieq_candi, 1) + rand * (b_eq_k_best(row_A_ieq_candi, 2) ...
                                     - b_eq_k_best(row_A_ieq_candi, 1));
                        end
                        x_cor_base(A_ieq_candi > 0) = (((b_eq_k - b_ieq_k_lb) / delta_plus) ...
                                                      * (X_gen(A_ieq_candi > 0, indi) - lb(A_ieq_candi > 0)) ...
                                                      + lb(A_ieq_candi > 0))';
                        x_cor_base(A_ieq_candi == 0) = (X_gen(A_ieq_candi == 0))';
                        x_cor_base(A_ieq_candi < 0) = (((b_eq_k - b_ieq_k_lb) / delta_plus) ...
                                                      * (X_gen(A_ieq_candi < 0, indi) - ub(A_ieq_candi < 0)) ...
                                                      + ub(A_ieq_candi < 0))';
                    end
                    X_cor(:, indi) = x_cor_base;
                end
            end
    
            for indi = 1:N_P
                X_cor(abs(X_cor(:, indi) - lb) <= 1e-12, indi) = lb(abs(X_cor(:, indi) - lb) <= 1e-12);
                X_cor(abs(X_cor(:, indi) - ub) <= 1e-12, indi) = ub(abs(X_cor(:, indi) - ub) <= 1e-12);
            end
    
            correction_info.is_feasible = all((X_cor - lb >= -1e-12) & (X_cor - ub <= 1e-12)) ...
                                          & all((A_ieq * X_cor - b_ieq) <= 1e-12);
            correction_info.does_X_cor_exceed.by_bit = (X_cor - lb < -1e-12) | (X_cor - ub > 1e-12);
            correction_info.does_X_cor_exceed.globally = any((X_cor - lb < -1e-12) | (X_cor - ub > 1e-12));
            correction_info.is_inequality_less_than_b.by_bit = (A_ieq * X_cor - b_ieq) <= 1e-12;
            correction_info.is_inequality_less_than_b.globally = all((A_ieq * X_cor - b_ieq) <= 1e-12);

            run = run + 1;
            % Feasible, Theorem 5
            if all(correction_info.is_feasible)
                break;
            end
            % Infeasible, Theorems 4 & 5
            if ~any(correction_info.is_feasible) && run >= max_runtime
                X_cor = [];
                correction_info.is_feasible = false(1, N_P);
                correction_info.does_X_cor_exceed.by_bit = true(N_D, N_P);
                correction_info.does_X_cor_exceed.globally = true(1, N_P);
                correction_info.is_equation_equal_to_b.by_bit = false(M, N_P);
                correction_info.is_equation_equal_to_b.globally = false;
                break;
            end

            X_gen = X_cor; 
        end

    end

end

linear_programming_correction函数

matlab 复制代码
function [X_cor, correction_info] = linear_programming_correction(X_gen, lb, ub, A_eq, b_eq, A_ieq, b_ieq, ...
                                    W_multiplicator)
    % LINEAR_PROGRAMMING_CORRECTION  Correction onto generated random vector.
    %                                A methodology of correction onto generated random vector,
    %                                facilitating the corrected vector to range within variable
    %                                boundaries and to meet linear constraints

    if ~exist('W_multiplicator', 'var')
        W_multiplicator = 1000;
    end

    Ab_eq = [A_eq, b_eq];
    
    M_eq = size(A_eq, 1);
    M_ieq = size(A_ieq, 1);
    [N_D, N_P] = size(X_gen);

    % Invoke Python for an accurate computation of the reduced row echelon form of Ab_eq
    Ab_eq_py = py.numpy.array(Ab_eq);
    Ab_eq_RREF_sym = py.sympy.Matrix(Ab_eq_py).rref();
    Ab_eq_RREF_raw = double(py.numpy.asarray(Ab_eq_RREF_sym(1), dtype = 'float'));
    if size(Ab_eq, 1) == 1
        Ab_eq_RREF = Ab_eq_RREF_raw;
    else
        Ab_eq_RREF = reshape(Ab_eq_RREF_raw, size(Ab_eq_RREF_raw, 2), size(Ab_eq_RREF_raw, 3));
    end

    R_A_eq = sum(any(Ab_eq_RREF(:, 1:end - 1), 2), 1);
    R_Ab_eq = sum(any(Ab_eq_RREF, 2), 1);
    A_eq_tilde = Ab_eq_RREF(:, R_A_eq + 1:end - 1);
    b_eq_tilde = Ab_eq_RREF(:, end);
    
    if R_A_eq < R_Ab_eq
        X_cor = [];
        correction_info.is_feasible = false(1, N_P);
        correction_info.does_X_cor_exceed.by_bit = true(N_D, N_P);
        correction_info.does_X_cor_exceed.globally = true(1, N_P);
        correction_info.is_equation_equal_to_b.by_bit = false(M_eq, N_P);
        correction_info.is_equation_equal_to_b.globally = false;
        correction_info.is_inequality_less_than_b.by_bit = false(M_ieq, N_P);
        correction_info.is_inequality_less_than_b.globally = false;
    
    else
        lb_base = lb(R_A_eq + 1:end, :);
        ub_base = ub(R_A_eq + 1:end, :);
        b_eq_tilde_plus = b_eq_tilde - lb(1:R_A_eq, end);
        b_eq_tilde_minus = b_eq_tilde - ub(1:R_A_eq, end);
        if isempty(A_eq)
            A_new = A_ieq;
            b_new = b_ieq;
            [X_cor, correction_info] = linear_inequalities_constraints_correction(X_gen, lb, ub, A_new, ...
                                       b_new, W_multiplicator);
        elseif isempty(A_ieq)
            if R_A_eq == 1
                A_new = [-A_eq; A_eq];
                b_new = [-b_eq; b_eq];
                [X_cor, correction_info] = linear_inequalities_constraints_correction(X_gen, lb, ub, A_new, ...
                                           b_new, W_multiplicator);
            else
                A_new = [-A_eq_tilde; A_eq_tilde];
                b_new = [-b_eq_tilde_minus; b_eq_tilde_plus];
                X_gen_base = X_gen(R_A_eq + 1:end, :);
                [X_cor_base, correction_info] = linear_inequalities_constraints_correction(X_gen_base, lb_base, ...
                                                ub_base, A_new, b_new, W_multiplicator);
                X_cor = [b_eq_tilde - A_eq_tilde * X_cor_base; X_cor_base];
            end
        else
            if R_A_eq == 1
                A_new = [-A_eq; A_eq; A_ieq];
                b_new = [-b_eq; b_eq; b_ieq];
                [X_cor, correction_info] = linear_inequalities_constraints_correction(X_gen, lb, ub, A_new, ...
                                           b_new, W_multiplicator);
            else
                A_new = [-A_eq_tilde; A_eq_tilde; A_ieq(:, R_A_eq + 1:end) - A_ieq(:, 1:R_A_eq) * A_eq_tilde];
                b_new = [-b_eq_tilde_minus; b_eq_tilde_plus; b_ieq - A_ieq(:, 1:R_A_eq) * b_eq_tilde];
                X_gen_base = X_gen(R_A_eq + 1:end, :);
                [X_cor_base, correction_info] = linear_inequalities_constraints_correction(X_gen_base, lb_base, ...
                                                ub_base, A_new, b_new, W_multiplicator);
                X_cor = [b_eq_tilde - A_eq_tilde * X_cor_base; X_cor_base];
            end
        end
        
        if isempty(A_eq)
            correction_info.is_feasible = all((X_cor - lb >= -1e-12) & (X_cor - ub <= 1e-12)) ...
                                          & all((A_ieq * X_cor - b_ieq) <= 1e-12);
            correction_info.does_X_cor_exceed.by_bit = (X_cor - lb < -1e-12) | (X_cor - ub > 1e-12);
            correction_info.does_X_cor_exceed.globally = any((X_cor - lb < -1e-12) | (X_cor - ub > 1e-12));
            correction_info.is_equation_equal_to_b.by_bit = [];
            correction_info.is_equation_equal_to_b.globally = [];
            correction_info.is_inequality_less_than_b.by_bit = (A_ieq * X_cor - b_ieq) <= 1e-12;
            correction_info.is_inequality_less_than_b.globally = all((A_ieq * X_cor - b_ieq) <= 1e-12);
        elseif isempty(A_ieq)
            correction_info.is_feasible = all((X_cor - lb >= -1e-12) & (X_cor - ub <= 1e-12)) ...
                                          & all(abs(A_eq * X_cor - b_eq) <= 1e-12);
            correction_info.does_X_cor_exceed.by_bit = (X_cor - lb < -1e-12) | (X_cor - ub > 1e-12);
            correction_info.does_X_cor_exceed.globally = any((X_cor - lb < -1e-12) | (X_cor - ub > 1e-12));
            correction_info.is_equation_equal_to_b.by_bit = abs(A_eq * X_cor - b_eq) <= 1e-12;
            correction_info.is_equation_equal_to_b.globally = all(abs(A_eq * X_cor - b_eq) <= 1e-12);
            correction_info.is_inequality_less_than_b.by_bit = [];
            correction_info.is_inequality_less_than_b.globally = [];
        else
            correction_info.is_feasible = all((X_cor - lb >= -1e-12) & (X_cor - ub <= 1e-12)) ...
                                          & all(abs(A_eq * X_cor - b_eq) <= 1e-12) & ...
                                          all((A_ieq * X_cor - b_ieq) <= 1e-12);
            correction_info.does_X_cor_exceed.by_bit = (X_cor - lb < -1e-12) | (X_cor - ub > 1e-12);
            correction_info.does_X_cor_exceed.globally = any((X_cor - lb < -1e-12) | (X_cor - ub > 1e-12));
            correction_info.is_equation_equal_to_b.by_bit = abs(A_eq * X_cor - b_eq) <= 1e-12;
            correction_info.is_equation_equal_to_b.globally = all(abs(A_eq * X_cor - b_eq) <= 1e-12);
            correction_info.is_inequality_less_than_b.by_bit = (A_ieq * X_cor - b_ieq) <= 1e-12;
            correction_info.is_inequality_less_than_b.globally = all((A_ieq * X_cor - b_ieq) <= 1e-12);
        end
    end

end

Problem 1

matlab 复制代码
clear;
close all;
clc;

lb = [-3, -1, 2, -4, 0, 1, -6, -5]';
ub = [6, 4, 10, 8, 11, 3, 4, 4]';
X_gen = lb + rand(1, 100) .* (ub - lb);
A_eq = [6, -3, 1, 7, -5, 0, -3, 8];
b_eq = 12;

[X_cor, correction_info] = linear_programming_correction(X_gen, lb, ub, [], [], A_eq, b_eq);

Problem 2

matlab 复制代码
clear;
close all;
clc;

lb = [-5, -3, -6, 0]';
ub = [7, 1, -2, 6]';
X_gen = lb + rand(1, 100) .* (ub - lb);
A_eq = [1, 1, 1, 1; 0, 2, 1, 1];
b_eq = [-1, 1]';

[X_cor, correction_info] = linear_programming_correction(X_gen, lb, ub, A_eq, b_eq, [], []);

Problem 3

matlab 复制代码
clear;
close all;
clc;

lb = [-8, -15, -2, 0, -3, -10]';
ub = [9, 7, 4, 5, 8, 2]';
X_gen = lb + rand(1, 100) .* (ub - lb);
A_eq = [4, 3, 5, -7, 6, -8];
b_eq = -2;
A_ieq = [-7, -4, 8, -5, 0, 1];
b_ieq = 14;

[X_cor, correction_info] = linear_programming_correction(X_gen, lb, ub, A_eq, b_eq, A_ieq, b_ieq);

Problem 4

matlab 复制代码
clear;
close all;
clc;

lb = [-8, -15, -2, 1, -3, -10]';
ub = [9, 7, 11, 7, 8, 2]';
X_gen = lb + rand(1, 100) .* (ub - lb);
A_eq = [4, 3, 5, -7, 6, -8; -7, -4, 8, -5, 0, 8];
b_eq = [-2; 14];
A_ieq = [10, 3, -3, 6, -7, 2; 2, -3, -7, 5, -6, 3];
b_ieq = [9; -8];

[X_cor, correction_info] = linear_programming_correction(X_gen, lb, ub, A_eq, b_eq, A_ieq, b_ieq);

研究目标

(1) 探究满足某些非线性约束的生成随机数修正方法;

(2) 探究随机向量为离散向量和连续-离散混合向量时的生成随机数修正方法;

(3) 将生成随机数修正方法应用到基于启发式算法的优化问题求解中,使得启发式算法能够始终在优化问题的可行域中搜寻问题的解,从而加快启发式优化算法的收敛速度。

相关推荐
火山口车神丶1 小时前
某车企ASW面试笔试题
c++·matlab
今天吃饺子1 小时前
2024年SCI一区最新改进优化算法——四参数自适应生长优化器,MATLAB代码免费获取...
开发语言·算法·matlab
2401_8827275710 小时前
BY组态-低代码web可视化组件
前端·后端·物联网·低代码·数学建模·前端框架
Evand J13 小时前
集合卡尔曼滤波(Ensemble Kalman Filter),用于二维滤波(模拟平面上的目标跟踪),MATLAB代码
matlab·平面·目标跟踪
smppbzyc14 小时前
2024亚太杯数学建模C题【Development Analyses and Strategies for Pet Industry 】思路详解
数学建模·数学建模竞赛·亚太杯·2024亚太杯数学建模·apmcm亚太杯·2024亚太地区数学建模竞赛·亚太杯c题
热心网友俣先生15 小时前
2024年亚太C题第二版本二问题1求解过程+代码运行以及问题2-4超详细思路分析
数学建模
小何数模15 小时前
24 年第十四届APMCM亚太数模竞赛浅析
数学建模
飞滕人生TYF16 小时前
动态规划 详解
算法·动态规划
川川菜鸟18 小时前
2024年亚太地区数学建模C题完整思路
数学建模
我是博博啦19 小时前
matlab例题
人工智能·算法·matlab