L1距离约束或距离目标

huangapple go评论54阅读模式
英文:

L1 distance constraint or distance objective

问题

我很新于线性规划和优化问题,正在尝试建立一个简单的线性规划问题。

我想要最小化两个二维点之间的L1距离。其中一个点将被预定义,另一个点应该由求解器确定,通过最小化决策变量上的距离。我已经设置了一个类似这样的简单程序:

from ortools.linear_solver import pywraplp
from ortools.init import pywrapinit

solver = pywraplp.Solver.CreateSolver('GLOP')

# 预定义的点
x_pd = 6
y_pd = 4

# 创建变量 -- 决策变量
x = solver.NumVar(0.0, 12.0, 'xr')
y = solver.NumVar(0.0, 8.0, 'yr')

# 定义约束
solver.Add(x <= 5.0)
solver.Add(x >= 7.0)
solver.Add(y - y_pd <= 0)

solver.Minimize(<目标函数>)  # 可能的目标函数

status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
    print('解决方案:')
    print('目标值 =', solver.Objective().Value())
    print('x =', x.solution_value())
    print('y =', y.solution_value())
else:
    print('问题没有最优解。')

我尝试将 x - x_pd 作为目标函数,但它没有给出我期望的结果。从求解器获得的新点远离了预定义的点。

我希望制定它,使新点接近(或完全等于)预定义的点。

是否有任何我可以参考的材料或帖子?如果我能得到一些关于LP问题的制定和约束编程的材料的指导,那将非常好。

英文:

I am very new to Linear Programming and Optimization problems in general and I am trying to set up a simple LP problem.

I want to minimize the L1 distance between two 2D points. One of them will be predefined and the other point should be determined by the solver by minimizing the distance on the decision variables. I have set up a simple program like this:

rom ortools.linear_solver import pywraplp
from ortools.init import pywrapinit

solver = pywraplp.Solver.CreateSolver(&#39;GLOP&#39;)


# the predefined point
x_pd = 6
y_pd = 4 

# create variables  -- decision variables
x = solver.NumVar(0.0, 12.0, &#39;xr&#39;)
y = solver.NumVar(0.0, 8.0, &#39;yr&#39;)

# define constraints
solver.Add(x&lt;=5.0)
solver.Add(x&gt;=7.0)

#solver.Add(x - x_pd &lt;= 0)       /*** idk if this is correct ***/
solver.Add(y - y_pd &lt;= 0)

solver.Minimize( &lt;objective&gt; )    /*** the possible objective ***/

status = solver.Solve()
if status == pywraplp.Solver.OPTIMAL:
    print(&#39;Solution:&#39;)
    print(&#39;Objective value =&#39;, solver.Objective().Value())
    print(&#39;x =&#39;, x.solution_value())
    print(&#39;y =&#39;, y.solution_value())
else:
    print(&#39;The problem does not have an optimal solution.&#39;)

I tried putting x - x_pd as an objective function but it did not gave the result which I expect. The new point I got from the solver far away to the predefined point.

I want to formulate it such that the new point is close to(or exactly ) the predefined point.

Is there any material or thread I can refer to? It will be great if I can get some guidance to refer some materials for LP problem formulation and constraint programming.

答案1

得分: 2

两点之间的L1距离 z=(zx,zy)p=(px,py)

|px-zx| + |py-zy|

对于线性规划 (LP),您需要对此进行线性化处理。一般来说,这很困难并且需要二进制变量,但如果我们最小化这个距离,我们可以进行如下处理:

min dx+dy
-dx <= px-zx <= dx
-dy <= py-zy <= dy

其中 dx 和 dy 是额外的(非负)变量。您可以将每个约束分为两个不同的不等式。

英文:

The L1 distance between two points z=(zx,zy) and p=(px,py) is

|px-zx| + |py-zy|

For an LP, you need to linearize this. In general, this is difficult and requires binary variables, but if we minimize this distance, we can do:

min dx+dy
-dx &lt;= px-zx &lt;= dx
-dy &lt;= py-zy &lt;= dy

where dx,dy are additional (non-negative) variables. You can split each constraint in two different inequalities.

huangapple
  • 本文由 发表于 2023年2月7日 03:37:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定