如何调用numpy求解器以最小化带有约束的obj_func

时间:2018-11-18 05:54:07

标签: python python-3.x numpy linear-programming solver

我正在尝试解决带有约束的线性方程式问题,但是我不知道如何调用求解器。

我使用的是我编写的名为covariancia的函数,但是它没有问题,因此我不会在此处发布。

from funcs import covariancia
from math import sqrt
def main():
    print("Iniciando Moneta...")
    evo_val = []
    with open("evo_val.txt") as my_file:
    evo_val = [line.split() for line in my_file]

    #Montando array de Médias
    medias = []

    for papel in evo_val:
        media_atual = 0

        for valor in papel:
            media_atual += float(valor)

        media_atual /= len(papel)
        medias.append(media_atual)

    #Calculating Covariance
    co_x = co_y = len(evo_val)
    co_var = [[0]*co_y for i in range(co_x)]

    for i in range(len(evo_val)):

        for j in range(len(evo_val)):
            if (j < i):
                co_var[i][j] = co_var[j][i]
            else:
                co_var[i][j] = covariancia(evo_val[i], evo_val[j], medias[i], medias[j])

    #Calculating Correlation
    co_rel = [[0]*co_y for i in range(co_x)]

    for i in range(len(evo_val)):
       for j in range(len(evo_val)):
           if i == j:
               co_rel[i][j] = 1
           elif j < i:
               co_rel[i][j] = co_rel[j][i]
           else:
               co_rel[i][j] = co_var[i][j] / (sqrt(co_var[i][i] * co_var[j][j]))

    #Linear Solver
    func_obj = [0.0 for i in range(co_x)]
    DPRE = 0.0

    RE = sum([func_obj[i]*medias[i] for i in range(len(evo_val))])

    for i in range(co_x):
        for j in range(co_x):
            DPRE += func_obj[i]*func_obj[j]*co_var[i][j]

    const = sum(func_obj) # must be equal to 1

我该怎么办?我需要使用sum(func_obj)必须等于1的约束来最小化(DPRE / RE)。

我怎么称呼求解器?如何告诉求解器我的变量在func_obj数组中?

感谢您的帮助!

0 个答案:

没有答案