Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop in Mata does not work (linear optimazation for multiple observations)

    Dear all,

    im relatively new to programming in Mata.

    I'm trying to solve linear optimization problems for multiple observations (individuals). As q.setCoefficients() apparently only supports row vectors (not matrices) but each individual faces different prices in the objective function, I thought I could simply loop over each individual separately and save the results in a matrix. For simplicity, I kept the parameters identical over the loop in the following example:

    Code:
    mata:
    
    x = J(2,3,.)
    
    for (i=1; i<=3; i++) {
    
    c = (1, 1, 0)   // Would change wih each individual
    Aec = (1, 1, 1 \ 1, -1, 2)
    bec = (5 \ 8)
    lowerbd = (1, 0, 0)
    upperbd = (., 2, .)
    
    q = LinearProgram()
    
    q.setCoefficients(c)
    
    q.setMaxOrMin("min")
    
    q.setEquality(Aec, bec)
    q.setBounds(lowerbd, upperbd)
    
    q.optimize()
    
    q.parameters()
    
    x[i,.] = q.parameters()  // Store parameter
    
    }
    
    x
    st_matrix("coef", x)
    
    end
    While the code works without the "for()" loop, the program aborts with the loop at "q.setCoefficients(c)" telling me "type mismatch: exp.exp: transmorphic found where struct expected".

    I looked around in the forum and used Google for similar problems. But I was unable to come up with a solution.

    Can anyone help me please? Of course, alternative solutions that allow the coefficients to vary in the objective function would be appreciated too.

    Thank you,

    Sebastian
    Last edited by Sebastian Geiger; 28 Jul 2022, 08:01.

  • #2
    This code works in the sense of not getting the error you report, nor the subsequent error that came to light when that was eliminated.
    Code:
    mata:
    
    q = LinearProgram()
    
    x = J(3,3,.)
    
    for (i=1; i<=3; i++) {
    
    c = (1, 1, 0)   // Would change wih each individual
    Aec = (1, 1, 1 \ 1, -1, 2)
    bec = (5 \ 8)
    lowerbd = (1, 0, 0)
    upperbd = (., 2, .)
    
    q.setCoefficients(c)
    
    q.setMaxOrMin("min")
    
    q.setEquality(Aec, bec)
    q.setBounds(lowerbd, upperbd)
    
    q.optimize()
    
    q.parameters()
    
    x[i,.] = q.parameters()  // Store parameter
    
    }
    
    x
    st_matrix("coef", x)
    
    end
    Code:
    . matrix list coef
    
    coef[3,3]
               c1         c2         c3
    r1          1  .33333333  3.6666667
    r2          1  .33333333  3.6666667
    r3          1  .33333333  3.6666667
    I believe the problem lay in creating multiple instances of the LinearProgram class with the same name. I moved that out of the loop and the program now worked. I couldn't find anything in the documentation to better understand that; my knowledge of class-based programming is sketchy at best.

    Comment

    Working...
    X