需要帮助解决具有一些已知值的最小稀疏线性问题
Need to help solving least sparse linear with some known values
我有下面描述的问题
我需要找到使
成为 x1'、x2'、x3'、x4'、x5' 的值
(x1-x1')^2+(x2-x2')^2+(x3-x3')^2+(x4-x4')^2+(x5-x5')^2 =最小值
和
x1' + x2' + x3' + x4' + x5' = 1
x1 + x2 + x3 + x4 + x5 = 1
注意:我们知道 a、b、c、d、e、x1、x2、x3、x4、x5 的值
在这种情况下有人能帮我吗?
我已经尝试使用 google/or-tools 库,但无法添加条件来找到最小值。
MPSolver solver = createSolver(solverType);
double infinity = MPSolver.infinity();
MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1");
MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2");
MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3");
MPVariable x4 = solver.makeNumVar(0.0, infinity, "x4");
MPVariable x5 = solver.makeNumVar(0.0, infinity, "x5");
// 0.15 <= x1 <= 0.35
MPConstraint c1 = solver.makeConstraint(-infinity, 0.35);
c1.setCoefficient(x1, 1);
MPConstraint c2 = solver.makeConstraint(0.15, infinity);
c2.setCoefficient(x1, 1);
// 0.1 <= x2 <= 0.3
MPConstraint c3 = solver.makeConstraint(-infinity, 0.3);
c3.setCoefficient(x2, 1);
MPConstraint c4 = solver.makeConstraint(0.1, infinity);
c4.setCoefficient(x2, 1);
// 0.0 <= x3 <= 0.2
MPConstraint c5 = solver.makeConstraint(-infinity, 0.2);
c5.setCoefficient(x3, 1);
MPConstraint c6 = solver.makeConstraint(0.0, infinity);
c6.setCoefficient(x3, 1);
// 0.15 <= x4 <= 0.35
MPConstraint c7 = solver.makeConstraint(-infinity, 0.35);
c7.setCoefficient(x4, 1);
MPConstraint c8 = solver.makeConstraint(0.15, infinity);
c8.setCoefficient(x4, 1);
// 0.1 <= x5 <= 0.3
MPConstraint c9 = solver.makeConstraint(-infinity, 0.3);
c9.setCoefficient(x5, 1);
MPConstraint c10 = solver.makeConstraint(0.1, infinity);
c10.setCoefficient(x5, 1);
// x1 + x2 + x3 + x4 + x5 = 1
MPConstraint c11 = solver.makeConstraint(-infinity, 1.0);
c11.setCoefficient(x1, 1);
c11.setCoefficient(x2, 1);
c11.setCoefficient(x3, 1);
c11.setCoefficient(x4, 1);
c11.setCoefficient(x5, 1);
MPConstraint c12 = solver.makeConstraint(1.0, infinity);
c12.setCoefficient(x1, 1);
c12.setCoefficient(x2, 1);
c12.setCoefficient(x3, 1);
c12.setCoefficient(x4, 1);
c12.setCoefficient(x5, 1);
MPObjective objective = solver.objective();
objective.setCoefficient(x1, 1);
objective.setCoefficient(x2, 1);
objective.setCoefficient(x3, 1);
objective.setCoefficient(x4, 1);
objective.setCoefficient(x5, 1);
objective.setMinimization();
这是一个带有凸objective函数的基本约束优化问题。
https://en.wikipedia.org/wiki/Constrained_optimization
有许多软件可以帮助您做到这一点。例如
http://cvxopt.org/documentation/index.html
非常感谢龙阳先生。
以下是帮助我解决问题的他的解决方案:
#@author: Long Duong
#Using this : http://cvxopt.org/userguide/coneprog.html#quadratic-programming
#Need to install cvxopt using (pip install cvxopt --user)
from cvxopt import matrix, solvers
# Need to MODIFY the value here
# Hold the value of x1,x2,x3,x4,x5
xi = matrix([0.5,0.6,0.7,0.8,0.9])
# Hold the value for a,b,c,d,e
cons = matrix([0.2,0.2,0.2,0.2,0.2])
### Main part ####
# Ensure the contrain: x1' + x2' + x3' + x4' + x5' = 1
A = matrix([1.0,1.0,1.0,1.0,1.0], (1,5))
b = matrix(1.0)
# Ensure the contrain: cons[i] -0.1 < x'[i] < cons[i] + 0.1
G = matrix([[1.0,0.0,0.0,0.0,0.0],
[-1.0,0.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0,0.0],
[0.0,-1.0,0.0,0.0,0.0],
[0.0,0.0,1.0,0.0,0.0],
[0.0,0.0,-1.0,0.0,0.0],
[0.0,0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,-1.0,0.0],
[0.0,0.0,0.0,0.0,1.0],
[0.0,0.0,0.0,0.0,-1.0]]).T
temp = []
for i in range(5):
temp.append(cons[i] + 0.1)
temp.append(-1 * (cons[i] - 0.1))
h = matrix(temp)
# Now need to solve the main function to minimize sum((x'[i]-xi[i])^2)
# P is kind of identity matrix since (x-a)^2 = x^2 - 2ax + a^2
P = 2 * matrix([[1.0,0.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0,0.0],
[0.0,0.0,1.0,0.0,0.0],
[0.0,0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,0.0,1.0]])
q = -2 * xi #
# All done
sol=solvers.qp(P, q, G, h, A, b)
print "[RESULT] :"
print sol['x']
我有下面描述的问题
我需要找到使
成为 x1'、x2'、x3'、x4'、x5' 的值(x1-x1')^2+(x2-x2')^2+(x3-x3')^2+(x4-x4')^2+(x5-x5')^2 =最小值
和
x1' + x2' + x3' + x4' + x5' = 1
x1 + x2 + x3 + x4 + x5 = 1
注意:我们知道 a、b、c、d、e、x1、x2、x3、x4、x5 的值
在这种情况下有人能帮我吗?
我已经尝试使用 google/or-tools 库,但无法添加条件来找到最小值。
MPSolver solver = createSolver(solverType);
double infinity = MPSolver.infinity();
MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1");
MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2");
MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3");
MPVariable x4 = solver.makeNumVar(0.0, infinity, "x4");
MPVariable x5 = solver.makeNumVar(0.0, infinity, "x5");
// 0.15 <= x1 <= 0.35
MPConstraint c1 = solver.makeConstraint(-infinity, 0.35);
c1.setCoefficient(x1, 1);
MPConstraint c2 = solver.makeConstraint(0.15, infinity);
c2.setCoefficient(x1, 1);
// 0.1 <= x2 <= 0.3
MPConstraint c3 = solver.makeConstraint(-infinity, 0.3);
c3.setCoefficient(x2, 1);
MPConstraint c4 = solver.makeConstraint(0.1, infinity);
c4.setCoefficient(x2, 1);
// 0.0 <= x3 <= 0.2
MPConstraint c5 = solver.makeConstraint(-infinity, 0.2);
c5.setCoefficient(x3, 1);
MPConstraint c6 = solver.makeConstraint(0.0, infinity);
c6.setCoefficient(x3, 1);
// 0.15 <= x4 <= 0.35
MPConstraint c7 = solver.makeConstraint(-infinity, 0.35);
c7.setCoefficient(x4, 1);
MPConstraint c8 = solver.makeConstraint(0.15, infinity);
c8.setCoefficient(x4, 1);
// 0.1 <= x5 <= 0.3
MPConstraint c9 = solver.makeConstraint(-infinity, 0.3);
c9.setCoefficient(x5, 1);
MPConstraint c10 = solver.makeConstraint(0.1, infinity);
c10.setCoefficient(x5, 1);
// x1 + x2 + x3 + x4 + x5 = 1
MPConstraint c11 = solver.makeConstraint(-infinity, 1.0);
c11.setCoefficient(x1, 1);
c11.setCoefficient(x2, 1);
c11.setCoefficient(x3, 1);
c11.setCoefficient(x4, 1);
c11.setCoefficient(x5, 1);
MPConstraint c12 = solver.makeConstraint(1.0, infinity);
c12.setCoefficient(x1, 1);
c12.setCoefficient(x2, 1);
c12.setCoefficient(x3, 1);
c12.setCoefficient(x4, 1);
c12.setCoefficient(x5, 1);
MPObjective objective = solver.objective();
objective.setCoefficient(x1, 1);
objective.setCoefficient(x2, 1);
objective.setCoefficient(x3, 1);
objective.setCoefficient(x4, 1);
objective.setCoefficient(x5, 1);
objective.setMinimization();
这是一个带有凸objective函数的基本约束优化问题。
https://en.wikipedia.org/wiki/Constrained_optimization
有许多软件可以帮助您做到这一点。例如
http://cvxopt.org/documentation/index.html
非常感谢龙阳先生。
以下是帮助我解决问题的他的解决方案:
#@author: Long Duong
#Using this : http://cvxopt.org/userguide/coneprog.html#quadratic-programming
#Need to install cvxopt using (pip install cvxopt --user)
from cvxopt import matrix, solvers
# Need to MODIFY the value here
# Hold the value of x1,x2,x3,x4,x5
xi = matrix([0.5,0.6,0.7,0.8,0.9])
# Hold the value for a,b,c,d,e
cons = matrix([0.2,0.2,0.2,0.2,0.2])
### Main part ####
# Ensure the contrain: x1' + x2' + x3' + x4' + x5' = 1
A = matrix([1.0,1.0,1.0,1.0,1.0], (1,5))
b = matrix(1.0)
# Ensure the contrain: cons[i] -0.1 < x'[i] < cons[i] + 0.1
G = matrix([[1.0,0.0,0.0,0.0,0.0],
[-1.0,0.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0,0.0],
[0.0,-1.0,0.0,0.0,0.0],
[0.0,0.0,1.0,0.0,0.0],
[0.0,0.0,-1.0,0.0,0.0],
[0.0,0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,-1.0,0.0],
[0.0,0.0,0.0,0.0,1.0],
[0.0,0.0,0.0,0.0,-1.0]]).T
temp = []
for i in range(5):
temp.append(cons[i] + 0.1)
temp.append(-1 * (cons[i] - 0.1))
h = matrix(temp)
# Now need to solve the main function to minimize sum((x'[i]-xi[i])^2)
# P is kind of identity matrix since (x-a)^2 = x^2 - 2ax + a^2
P = 2 * matrix([[1.0,0.0,0.0,0.0,0.0],
[0.0,1.0,0.0,0.0,0.0],
[0.0,0.0,1.0,0.0,0.0],
[0.0,0.0,0.0,1.0,0.0],
[0.0,0.0,0.0,0.0,1.0]])
q = -2 * xi #
# All done
sol=solvers.qp(P, q, G, h, A, b)
print "[RESULT] :"
print sol['x']