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

**URL:** <https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061>\
**Category:** New to Julia\
**Created:** [August 16, 2020, 8:30pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061 "2020-08-16T20:30:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [August 16, 2020, 8:30pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/1 "2020-08-16T20:30:43Z")

</div>

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

```julia
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:

```julia
  @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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 16, 2020, 9:02pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/2 "2020-08-16T21:02:04Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [August 16, 2020, 9:34pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/3 "2020-08-16T21:34:29Z")

</div>

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: [An Introduction to Structural Econometrics in Julia | Julia/Economics](https://juliaeconomics.com/2016/02/09/an-introduction-to-structural-econometrics-in-julia/)

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [August 16, 2020, 10:36pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/4 "2020-08-16T22:36:01Z")

</div>

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/c/8c697f549237270afa185523758f4c6fe07dea5c.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 17, 2020, 5:05am UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/5 "2020-08-17T05:05:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [August 17, 2020, 5:23pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/6 "2020-08-17T17:23:21Z")

</div>

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

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

```

But it returns **MethodError: no method matching**

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [August 17, 2020, 8:02pm UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/7 "2020-08-17T20:02:46Z")

</div>

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](https://jump.dev/JuMP.jl/v0.19.0/quickstart/) guide that may be sufficient to resolve this issue. There are also other sections that describe constraints and such.

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [August 18, 2020, 3:33am UTC](https://discourse.julialang.org/t/how-to-efficiently-figure-out-correct-ways-of-using-packages-and-syntax/45061/8 "2020-08-18T03:33:02Z")

</div>

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.
