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

目录

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

相关推荐
阑梦清川2 小时前
数学建模启发式算法篇(一)---遗传算法
算法·数学建模·启发式算法
Matlab程序猿小助手6 小时前
【MATLAB源码-第208期】基于matlab的改进A*算法和传统A*算法对比仿真;改进点:1.无斜穿障碍物顶点2.删除中间多余节点,减少转折。
开发语言·嵌入式硬件·算法·matlab·机器人
IT猿手10 小时前
基于卷积神经网络(CNN)的时间序列预测,15个输入1个输出,可以更改数据集,MATLAB代码
人工智能·深度学习·神经网络·算法·matlab·cnn
丶Darling.11 小时前
代码随想录 | Day38 | 动态规划 :01背包应用 目标和&&一和零
c++·算法·动态规划·记忆化搜索·回溯·递推·01背包
tan77º11 小时前
【AcWing】算法基础课-动态规划
算法·动态规划·1024程序员节
其实吧314 小时前
基于MATLAB的运动车辆跟踪检测系统
开发语言·matlab
梦里水乡85718 小时前
基于MATLAB的农业病虫害识别研究
开发语言·matlab
陈琦煜1 天前
AcWing 1073 树的中心 树形dp (详解)
动态规划·树形dp
墨痕_7771 天前
论文阅读笔记Dense Passage Retrieval for Open-Domain Question Answering
matlab
君臣Andy1 天前
【矩阵的大小和方向的分解】
线性代数·矩阵