java 需要帮助解决一些已知值的最小稀疏线性

brccelvz  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(174)

我有下面描述的问题
x1c 0d1x的数据
我需要找到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库,但无法添加条件来查找最小值。

  1. MPSolver solver = createSolver(solverType);
  2. double infinity = MPSolver.infinity();
  3. MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1");
  4. MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2");
  5. MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3");
  6. MPVariable x4 = solver.makeNumVar(0.0, infinity, "x4");
  7. MPVariable x5 = solver.makeNumVar(0.0, infinity, "x5");
  8. // 0.15 <= x1 <= 0.35
  9. MPConstraint c1 = solver.makeConstraint(-infinity, 0.35);
  10. c1.setCoefficient(x1, 1);
  11. MPConstraint c2 = solver.makeConstraint(0.15, infinity);
  12. c2.setCoefficient(x1, 1);
  13. // 0.1 <= x2 <= 0.3
  14. MPConstraint c3 = solver.makeConstraint(-infinity, 0.3);
  15. c3.setCoefficient(x2, 1);
  16. MPConstraint c4 = solver.makeConstraint(0.1, infinity);
  17. c4.setCoefficient(x2, 1);
  18. // 0.0 <= x3 <= 0.2
  19. MPConstraint c5 = solver.makeConstraint(-infinity, 0.2);
  20. c5.setCoefficient(x3, 1);
  21. MPConstraint c6 = solver.makeConstraint(0.0, infinity);
  22. c6.setCoefficient(x3, 1);
  23. // 0.15 <= x4 <= 0.35
  24. MPConstraint c7 = solver.makeConstraint(-infinity, 0.35);
  25. c7.setCoefficient(x4, 1);
  26. MPConstraint c8 = solver.makeConstraint(0.15, infinity);
  27. c8.setCoefficient(x4, 1);
  28. // 0.1 <= x5 <= 0.3
  29. MPConstraint c9 = solver.makeConstraint(-infinity, 0.3);
  30. c9.setCoefficient(x5, 1);
  31. MPConstraint c10 = solver.makeConstraint(0.1, infinity);
  32. c10.setCoefficient(x5, 1);
  33. // x1 + x2 + x3 + x4 + x5 = 1
  34. MPConstraint c11 = solver.makeConstraint(-infinity, 1.0);
  35. c11.setCoefficient(x1, 1);
  36. c11.setCoefficient(x2, 1);
  37. c11.setCoefficient(x3, 1);
  38. c11.setCoefficient(x4, 1);
  39. c11.setCoefficient(x5, 1);
  40. MPConstraint c12 = solver.makeConstraint(1.0, infinity);
  41. c12.setCoefficient(x1, 1);
  42. c12.setCoefficient(x2, 1);
  43. c12.setCoefficient(x3, 1);
  44. c12.setCoefficient(x4, 1);
  45. c12.setCoefficient(x5, 1);
  46. MPObjective objective = solver.objective();
  47. objective.setCoefficient(x1, 1);
  48. objective.setCoefficient(x2, 1);
  49. objective.setCoefficient(x3, 1);
  50. objective.setCoefficient(x4, 1);
  51. objective.setCoefficient(x5, 1);
  52. objective.setMinimization();

字符串

4urapxun

4urapxun1#

这是一个带有凸目标函数的基本约束优化问题。https://en.wikipedia.org/wiki/Constrained_optimization有很多软件可以帮助你做到这一点。
http://cvxopt.org/documentation/index.html

vwkv1x7d

vwkv1x7d2#

我想为Long Duong先生感谢你。
下面是他的解决方案,帮助我解决我的问题:

  1. #@author: Long Duong
  2. #Using this : http://cvxopt.org/userguide/coneprog.html#quadratic-programming
  3. #Need to install cvxopt using (pip install cvxopt --user)
  4. from cvxopt import matrix, solvers
  5. # Need to MODIFY the value here
  6. # Hold the value of x1,x2,x3,x4,x5
  7. xi = matrix([0.5,0.6,0.7,0.8,0.9])
  8. # Hold the value for a,b,c,d,e
  9. cons = matrix([0.2,0.2,0.2,0.2,0.2])
  10. ### Main part ####
  11. # Ensure the contrain: x1' + x2' + x3' + x4' + x5' = 1
  12. A = matrix([1.0,1.0,1.0,1.0,1.0], (1,5))
  13. b = matrix(1.0)
  14. # Ensure the contrain: cons[i] -0.1 < x'[i] < cons[i] + 0.1
  15. G = matrix([[1.0,0.0,0.0,0.0,0.0],
  16. [-1.0,0.0,0.0,0.0,0.0],
  17. [0.0,1.0,0.0,0.0,0.0],
  18. [0.0,-1.0,0.0,0.0,0.0],
  19. [0.0,0.0,1.0,0.0,0.0],
  20. [0.0,0.0,-1.0,0.0,0.0],
  21. [0.0,0.0,0.0,1.0,0.0],
  22. [0.0,0.0,0.0,-1.0,0.0],
  23. [0.0,0.0,0.0,0.0,1.0],
  24. [0.0,0.0,0.0,0.0,-1.0]]).T
  25. temp = []
  26. for i in range(5):
  27. temp.append(cons[i] + 0.1)
  28. temp.append(-1 * (cons[i] - 0.1))
  29. h = matrix(temp)
  30. # Now need to solve the main function to minimize sum((x'[i]-xi[i])^2)
  31. # P is kind of identity matrix since (x-a)^2 = x^2 - 2ax + a^2
  32. P = 2 * matrix([[1.0,0.0,0.0,0.0,0.0],
  33. [0.0,1.0,0.0,0.0,0.0],
  34. [0.0,0.0,1.0,0.0,0.0],
  35. [0.0,0.0,0.0,1.0,0.0],
  36. [0.0,0.0,0.0,0.0,1.0]])
  37. q = -2 * xi #
  38. # All done
  39. sol=solvers.qp(P, q, G, h, A, b)
  40. print "[RESULT] :"
  41. print sol['x']

字符串

展开查看全部

相关问题