fork download
  1. from scipy.optimize import linprog
  2.  
  3. # Coefficients of the objective function (costs)
  4. c = [25000, 28000, 12000]
  5.  
  6. # Coefficients for the carbon percentage constraints (left side)
  7. A_ub = [
  8. [-75, -72, -65], # 0.73 <= (x_A * 75 + x_B * 72 + x_C * 65) / (x_A + x_B + x_C)
  9. [75, 72, 65], # (x_A * 75 + x_B * 72 + x_C * 65) / (x_A + x_B + x_C) <= 0.76
  10. ]
  11.  
  12. # Right-hand side for the carbon percentage constraints
  13. b_ub = [0.73, 0.76]
  14.  
  15. # Coefficients for the sulfur percentage constraints (left side)
  16. A_ub_sulfur = [
  17. [-23, -26, -30], # 0.23 <= (x_A * 23 + x_B * 26 + x_C * 30) / (x_A + x_B + x_C)
  18. [23, 26, 30], # (x_A * 23 + x_B * 26 + x_C * 30) / (x_A + x_B + x_C) <= 0.25
  19. ]
  20.  
  21. # Right-hand side for the sulfur percentage constraints
  22. b_ub_sulfur = [0.23, 0.25]
  23.  
  24. # Combine the two constraints
  25. A_ub_total = A_ub + A_ub_sulfur
  26. b_ub_total = b_ub + b_ub_sulfur
  27.  
  28. # Boundaries for the variables (non-negativity)
  29. x_bounds = [(0, None), (0, None), (0, None)]
  30.  
  31. # Solving the problem using linear programming
  32. result = linprog(c, A_ub=A_ub_total, b_ub=b_ub_total, bounds=x_bounds, method='highs')
  33.  
  34. # Display the result
  35. if result.success:
  36. print(f"Optimal solution:\nCoal A: {result.x[0]} tons\nCoal B: {result.x[1]} tons\nCoal C: {result.x[2]} tons")
  37. else:
  38. print("Optimization failed.")
  39.  
Success #stdin #stdout 1.54s 64888KB
stdin
30 499887702
128990795 137274936
575374246 989051853
471048785 85168425
640066776 856699603
819841327 611065509
704171581 22345022
536108301 678298936
119980848 616908153
117241527 28801762
325850062 478675378
623319578 706900574
998395208 738510039
475707585 135746508
863910036 599020879
340559411 738084616
122579234 545330137
696368935 86797589
665665204 592749599
958833732 401229830
371084424 523386474
463433600 5310725
210508742 907821957
685281136 565237085
619500108 730556272
88215377 310581512
558193168 136966252
475268130 132739489
303022740 12425915
122379996 137199296
304092766 23505143
stdout
Optimal solution:
Coal A: 0.0 tons
Coal B: 0.0 tons
Coal C: 0.0 tons