How to efficiently figure out correct ways of using packages and syntax?

My question is a bit general: how to efficiently figure out correct ways of using packages and syntax. I illustrate the difficulty that I am having using the following example:

I was trying to learn the code shared by someone else. A part of his code is

using DataFrames
using JuMP
using Ipopt
N = size(df)[1]
m = Model(solver=IpoptSolver())     # define empty model solved by Ipopt algorithm
@defVar(m, c[i=1:N] >= 0)              # define positive consumption for each agent
@defVar(m, 0 <= l[i=1:N] <= 1)       # define leisure in [0,1] for each agent
@addConstraint(m, c[i=1:N] .== (1.0-t)*(1.0-l[i]).*w[i] + e[i] )        # each agent must satisfy the budget constraint
@setNLObjective(m, Max, sum{ g*log(c[i]) + (1-g)*log(l[i]) , i=1:N } )  # maximize the sum of utility 

This code does not work primarily because the new JuMP version has different syntax. Although I understand what the author wants in each line of code, I don’t see how to modify them to the updated version. What I tried so far are the following:

  @variable(model, c[i=1:N] >= 0)       # define positive consumption for each agent
  @variable(model, 0 <= l[i=1:N] <= 1)  # define leisure in [0,1] for each agent
  @constraint(model, constraint, c == (1.0-t)*(1.0-l)*w + e )       # each agent must satisfy the budget constraint
 @NLobjective(model, Max, transpose(ones(N))*(g*log.(c) + (1-g)*log.(l)) )  # maximize the sum of utility 

The first 2 lines are fine, but the last 2 lines do not work. The errors are “t is not defined” and “transpose used in nonlinear expression”.

In such a case, where should I learn from and how should I search for a solution?

I may be wrong, but I am not sure if the error there is from distinct JuMP versions. JuMP probably did not export a global t binding (or a w, or a e), nor it seems that any of the previous macros would introduce such bindings into scope. Are you really sure this code worked as it is in some version of Julia and its packages (JuMP, Ipopt, and DataFrames)?

1 Like

Sorry if I did not explain clearly. This part of code is building an optimization function, and (g,t,w,e) will be input arguments. I believe that the code worked in earlier versions of Julia, as they are shared in an instructional website: https://juliaeconomics.com/2016/02/09/an-introduction-to-structural-econometrics-in-julia/

After further digging into the problem, I found that the issue is mainly on JuMP versions. Below is a very simple constrained optimization. The syntax m = Model(solver=IpoptSolver()) is obsolete. However, even if I replace it with the new syntax, I still get this error message:

To be fair, it looks like that tutorial is 5 years old and was written for Julia 0.3 - you’re probably better off rewriting this from scratch following the maths provided on the website and the current JuMP docs

2 Likes

Yeah that’s fair. The problem is that I struggled in finding updated syntax that implements the same things. For example, @addConstraint(m, c[i=1:N] .== (1.0-t)*(1.0-l[i]).*w[i] + e[i] ) no longer works and what’s the best way to search for the new syntax that allows “one constraint for each i” ?
I tried

  @constraint(model, c == (1.0-t)*(1.0-l)*w + e )

But it returns MethodError: no method matching

There are a few ways to efficiently figure out syntax. One is from the error message:

solve has been replaced with optimize!

This suggests that there may be a new syntax. Given this hint, some information can be gained from help. Just type ?optimize! will retrieve some basic documentation. (If you just use optimize! for solve that may be enough.)

Another good source of information is from the latest documentation. When I web search julia jump the first hit takes me to the latest, stable docs. There is a Quick Start guide that may be sufficient to resolve this issue. There are also other sections that describe constraints and such.

2 Likes

Thanks! Yeah this error message is informative and easy to respond to. However, many errors messages are confusing and do not point to anything (e.g. the one above). Since the Julia community is still small, it is also harder to find related answered questions online.