Optimisation in swi prolog

拈花ヽ惹草 提交于 2019-12-05 10:55:50

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.

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

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

    For example:

    ?- my_constraints(S), maximize([x,y], S, Max), variable_value(Max, x, X).
    S = ...,
    Max = ...,
    X = 0.
    
  2. 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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!