swi prolog中的优化 [英] Optimisation in swi prolog

查看:100
本文介绍了swi prolog中的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想找到argmax(x,y,z)-1/2(20x ^ 2 + 32xy + 16y ^ 2)+ 2x + 2y.

Say I want to find argmax(x,y,z) -1/2(20x^2+32xy +16y^2)+2x+2y.

受制于: x> = 0,y> = 0,z> = 0和-x-y + z = 0.

subject to: x>=0, y>=0,z>=0 and -x-y+z =0.

我知道被设置为0的偏导数是:

I know the partial derivatives being set to 0 is :

-20x-16y + 2 = 0和-16x-16y + 2 = 0

-20x-16y+2=0 and -16x-16y+2 =0

所以我们可以让x = 0和y = 1/8和z = 1/8.

so we could have x= 0 and y =1/8 and z=1/8.

我该如何在Swi-prolog中做到这一点?我看到有用于线性求解的库单纯形,但这是一个二次问题,而偏导数则不是. (我有点困惑!)

How would I do this in Swi-prolog? I see that there is library simplex for linear solving, but this is a quadratic problem but the partial derivatives are not. (I am a bit confused!)

这就是我所拥有的:

:- use_module(library(simplex)).

my_constraints(S):-
 gen_state(S0),
 constraint([-20*x, -16*y] = 0, S0, S1),
 constraint([-16*x,-16*y] = 0, S1,S2),
 constraint([x] >= 0,S2,S3),
 constraint([y] >= 0,S3,S4),
 constraint([z] >= 0,S4,S5),
 constraint([-x-y+z] = 0,S5,S).

?- my_constraints(S), variable_value(S,x,Val1),variable_value(S,y,Val2).
false.

推荐答案

这里有几个问题.首先,只是为了解决这个问题:library(simplex)仅能处理 linear 约束.因此,是的,至少不能直接—用于解决您的实际问题.

There are several issues here. First, just to get this out of the way: library(simplex) can only handle linear constraints. So yes, it cannot—at least not directly—be used to solve your actual problem.

但是library(simplex)通常无论如何都是有用的,因此我想快速指出以下几点:

But library(simplex) is often useful regardless, and so I want to quickly point out the following:

  1. variable_value/3仅适用于已解决表.这意味着您必须先调用maximize/3.

  1. variable_value/3 only works on the solved tableau. This means that you must have invoked maximize/3 first.

例如:

?- my_constraints(S), maximize([x,y], S, Max), variable_value(Max, x, X).
S = ...,
Max = ...,
X = 0.

  • 请注意,必须将my_constraint/1的最终目标更改为constraint([-1*x, -1*y,z] = 0, S5, S),以符合该库所需的语法.

  • Note that you must change the final goal of my_constraint/1 to constraint([-1*x, -1*y,z] = 0, S5, S) to conform to the syntax required by this library.

    话虽这么说,现在让我们进入问题的核心:有一系列使用线性迭代解决二次优化问题的著名方法.  有关梯度的程序和推理,以使其更接近解决方案.因此,library(simplex)仍可以间接用于解决您的问题.

    That being said, let us now get to the core of the issue: There are well-known ways to iteratively solve quadratic optimization problems, using a series of linear programs and reasoning about gradients to get closer to a solution. Thus, library(simplex) can indirectly still be used to solve your problem.

    尤其是,请查看 其他程序中可用的最陡峭上升方法. .它包括一个用Prolog编写的小型符号导数计算器.是的,它是符号";-)

    In particular, check out the method of steepest ascent available from miscellaneous programs. It includes a small symbolic derivative calculator written in Prolog. Yes, it's "symbolic" ;-)

    完成您的任务后,我得到:

    Plugging in your task, I get:

    
    ?- maximize(- 0.5*(20*x(1)^2 + 32*x(1)*x(2) + 16*x(2)^2) + 2*x(1) + 2*x(2),
       [[-1,0,0],
        [0,-1,0],
        [0,0,-1],
        [-1,-1,1],
        [1,1,-1]],
       [0,0,0,0,0],
       [0,0,0], Max).
    Max = [4.298588509886033e-17, 0.125, 0.12500000000000006] ;
    false.
    

    我希望您可以使用哪种方法,直到浮点运算难以忍受的麻烦为止.

    Which is, up to the unbearable nastiness of floating point arithmetic, something that I hope you can work with.

    这篇关于swi prolog中的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆