Algorithm for sorting numbers summed by a common and observing criteria in python

I'm trying to make feed optimization calculations for animal feeds, but I'm new to python coding terms.

In fact, what I am trying to achieve is calculating a less expensive combination of n ingredients with groups of k or less that provide sufficient criteria A and B.

My problem is that when the number of ingredients starts to increase, python hangs in the calculations. So, is there a way for python to use more memory or a better algorithm or an existing package for such calculations. I was looking for a network for an answer, but maybe this particular problem has a mathematical name that I don't know about.

What am I doing now:

  • Create a matrix of ingredients: each ingredient has a name, and then P, A, B, C, D ... where P is the price and A, B, C, ... are the values ​​for different nutrients
  • Create a limit matrix: each element has a minimum and maximum value for the entire mix.
  • Create an object vector: what I want to get is now the vector A = X and the vector B = Y, but I would like to indicate in the future C, D, etc.
  • Then I calculate all possible combinations of summing the ingredients for a total amount (usually 1000) and a group of k ingredients.
  • Remove combinations that do not match the limit matrix to get an available combination matrix
  • Multiply the ingredient matrix by the combination matrix to get the final composition matrix
  • Delete all values ​​in the composition matrix that do not match the objective vector for criteria A and B.
  • .

Numpy .

( ) , , .

+3
2

, scipy.optimize - numpy , , . . Ref http://docs.scipy.org/doc/scipy-0.8.x/reference/tutorial/optimize.html

A, I, , AI .

, A ( , ) , , . A ( >= 0), .

+1

- ​​ . , scipy.optimize. :

from scipy import array,dot
from scipy.optimize import fmin_slsqp as fmin

c = array([173.0,184.0,167.0])    # cost or prices
b = array([0.1,0.1,0.1])          # lower nutrient bounds
A = array([[ 0.39, 0.09, 0.77],   # nutrient composition
           [ 0.75, 0.32, 0.15],
           [ 0.32, 0.76, 0.65]])

x = array([0.5,0.5,0.5])          # initial guess

def obj(x):
    # I'm the objective function
    return dot(c,x)

def con(x):
    # I'm the inequality constraints
    return dot(A,x) - b

print fmin(obj,x,f_ieqcons=con)
+1

Source: https://habr.com/ru/post/1794467/


All Articles