【问题标题】:Using anonymous functions in a loop在循环中使用匿名函数
【发布时间】:2017-11-25 14:52:06
【问题描述】:

我想使用动态编程来编程解决线性程序。

假设我的目标函数中有 3 个变量。然后我想一次考虑每个变量,并通过使用fminsearch 或其他东西找到最佳值。

现在我的代码有三个不同的匿名函数来解决每个变量。我想循环执行此操作,以提高效率。如何在循环中使用匿名函数?

Func1 = @(x) 2*x(1) 
Func2 = @(y) 3*y(1)
Func3 = @(z) 4*z(1)

我想做这样的事情:

Func(i) = @(x(i)) k(i)*x(i)

示例:

max z = 3x1+5x2
s.t.
x1 <=4
2x2  <=12
3x1 + 2x2  <=18 
x1>=0
x2>=0

到目前为止我尝试了什么:

func1 = @(x) -3*x(1);
[x1,minZ] = fmincon(func1,x0,A,b,[],[],lb,ub,[],options)
func2 = @(x) -5*x(1)-3*x1; %substituting from previous solution for x1...
[x2,minZ] = fmincon(func2,x0,A2,b2,[],[],lb2,ub2,[],options)

【问题讨论】:

    标签: matlab loops for-loop dynamic anonymous-function


    【解决方案1】:
    x0 = 0;
    
    fun_b2 = @(x) 18 - (3 * x);
    fun_fm = '@(x)(-5*{x2})-(3*{x1})';
    opts = optimset('Algorithm','active-set','TolFun',1e-6);
    
    % 1st parameter of the row: A as column vector
    % 2nd parameter of the row: B first value (the second one is evaluated into the for loop
    % 3rd parameter of the row: a column vector containing the upper and lower bounds
    params = {
        [1 3] 4 [0 Inf];
        [2 2] 12 [0 3]
    };
    params_size = size(params,1);
    
    res = NaN(params_size,1);
    
    for i = 1:params_size
        % take the function in string form and replace the current parameter placeholder with x(i)
        fun = fun_fm;
        fun = strrep(fun,['{x' num2str(i) '}'],'x(1)');
    
        % replace the previous parameter placeholders with the known values
        for j = 1:i
            fun = strrep(fun,['{x' num2str(j) '}'],num2str(res(j)));
        end
    
        % replace the next parameter placeholders with 0
        if (i < params_size)
            for j = (i+1):params_size
                fun = strrep(fun,['{x' num2str(j) '}'],'0');
            end
        end
    
        % transform the string into a function handle
        fun = str2func(fun);
    
        % transpose the first parameter (A) to row vector
        A = params{i,1}.';
    
        % take the first B value and calculate the second one with the function
        b1 = params{i,2};
        b2 = fun_b2(x1);
    
        bounds = params{i,3};
    
        [xi,min_z] = fmincon(fun,x0,A,[b1 b2],[],[],bounds(1),bounds(2),[],opts);
    
        res(i) = xi;
    end
    

    【讨论】:

    • 这不让我解决三遍。我必须在三个不同的步骤中求解 x y 和 z。
    • 好的,你可以编辑提供完整的算法吗,这很不清楚。
    • % 最大 z = 3x1+5x2 % s.t. % x1 =0 % x2>=0 func1 = @(x) -3*x(1); x0=0; A = [1;3]; b = [4 18];磅 = [0]; ub = [inf]; options = optimset('Algorithm','active-set','TolFun', 1e-6 ); [x1,minZ] = fmincon(func1,x0,A,b,[],[],lb,ub,[],options) func2 = @(x) -5*x(1)-3*x1; %用先前的解决方案代替 x1... % 并解决 x2 变量 x0=0; A2 = [2;2]; b2 = [12 ;18-3*x1];磅2 = [0]; ub2 = [3]; [x2,minZ] = fmincon(func2,x0,A2,b2,[],[],lb2,ub2,[],options)
    • 不太清楚,但我知道我明白你在找什么……等我编辑我的答案。
    • 好的,我找到了一个解决方案,它可以工作。告诉我你是否需要让你的函数更加动态,因为它可能需要很长时间才能详细说明。
    猜你喜欢
    • 2012-12-08
    • 2011-08-05
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多