0

I'm trying to solve a linear equation problem with constraints but I have no idea of how to call the solver.

I'm using a function that I wrote called covariancia but there's no problem with it, so I'll not post it here.

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

What must I do? I need to minimize (DPRE/RE) with the constraints that sum(func_obj) must be equal to 1.

How can I call the solver? How can I tell the solver that my variables are in the array func_obj?

Thanks for the help!

  • Did you have a look at the documentation on [NumPy minimization](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html)? This may be of help here... – sophros Nov 18 '18 at 15:27
  • @sophros Yes i did. The problem is i'm not used to work with optmizations and research problems, linear algs, etc... i don't know which func of the linalg should I use, i don't know how to tell the variables, the constraints. I didn't find and example that's close enough to my problem i guess. – Thomas Leick Nov 18 '18 at 16:32

0 Answers0