# Help: How to have my function named penalty\_method run?

**URL:** <https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 30, 2019, 6:16pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707 "2019-04-30T18:16:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [April 30, 2019, 6:16pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/1 "2019-04-30T18:16:21Z")

</div>

I am trying to get the penalty function to run using a given optimize function at the top of the code. I want the penalty\_method function to run for a certain amount of iterations (n=1000, so i=500), but I cannot figure out the structure. f, g, c, and x0 are given by the function optimize, so I need to integrate my code to run penalty\_method to converge on a minimum. There are two constraints given as a vector by the function optimize, c1 and c2. Am I correctly calling them by saying c(x)[1], and c(x)[2]? In the end, I want the value x’ (the optimized x) extracted from the function optimize.

```julia
function optimize(f, g, c, x0, n, prob)
    x′= x0
    # y_best=f(x')
    i=1
    R = penalty_method(f, p, x, k_max; ρ=1, γ=2)
    while i <= n/2 #fc + 2*gc
        i+=1
        x' = penalty_method(f, p, x', k_max; ρ=1, γ=2)
    end
    return x′
end
    
function penalty_method(f, p, x, k_max; ρ=1, γ=2)
    for k in 1 : k_max
        q1 = c(x)[1]
        q2 = c(x)[2]
        p = x -> f(x) + ρ*(sum(q1>0)+sum(q2>0))
        x = powell(x -> f(x) + ρ*p(x), x)
        ρ *= γ
        if p(x) == 0
            return x
        end
    end
    return x
end
    
function powell(f, x, ϵ)
    n = length(x)
    U = [basis(i,n) for i in 1 : n]
    Δ = Inf
    while Δ > ϵ
        x′ = x
        for i in 1 : n
            d = U[i]
            x′ = line_search(f, x′, d)
        end
        for i in 1 : n-1
            U[i] = U[i+1]
        end
        U[n] = d = x′ - x
        x′ = line_search(f, x, d)
        Δ = norm(x′ - x)
        x = x′
    end
    return x
end

```

 ![18%20AM](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2dadd1e127791c09e3f9ea90a7b0f7958f0dc45.png)  
 ![39%20AM](https://global.discourse-cdn.com/julialang/original/3X/6/0/60bccc896a3f96c4e3d790d712f94ed5d0c3ae32.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 30, 2019, 6:46pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/2 "2019-04-30T18:46:48Z")

</div>

> [@Engineer43](#):
>
> There are two constraints given as a vector by the function optimize, c1 and c2. Am I correctly calling them by saying `c(x)[1]`, and `c(x)[2]`?

You aren’t passing these to your `penalty_method` function, so it will give an error when these are called in that function unless you add them as function arguments.

If `c` is a vector of functions, like `c = [sin, cos]`, then you would call them as `c[1](x)` and `c[2](x)`.

---

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [April 30, 2019, 7:09pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/4 "2019-04-30T19:09:21Z")

</div>

Thank you for your help so far.  
I want both of these equations to be positive to add to the penalty function so would entering q1 and q2 in p inside of the function penalty\_method be incorrect? I am unsure how to add them otherwise. Furthermore, my main concern is on the syntax of the function optimize section. Is the syntax and use of R correct? Essentially I want the penalty method function to run until the number of iterations =1000, and then I want it to spit out the x-value it has reached. Below is the following code that I used to say c(x)[1] and c(x)[2]

function simple1\_constraints(x::Vector)  
return [x[1] + x[2]^2 - 1,  
-x[1] - x[2]]  
end

const simple\_problems = Dict(1=\>(f=simple1, g=simple1\_gradient, c=simple1\_constraints, x0=simple1\_init, n=1000)

---

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [April 30, 2019, 7:16pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/5 "2019-04-30T19:16:33Z")

</div>

I am currently getting the following error:  
(“syntax: invalid assignment location "x’"”)) for line 28 which is the first line (function optimize(f, g, c, x0, n, prob))

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [April 30, 2019, 8:36pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/6 "2019-04-30T20:36:35Z")

</div>

This isn’t an answer to your overall question but you’re getting the invalid assignment location error because

```julia
x'

```

is not a valid variable name. The `'` symbol is used to take the adjoint of a variable. For example:

```julia
julia> x = rand(4)
4-element Array{Float64,1}:
 0.1346318328288365
 0.4288867990992209
 0.7113093810849349
 0.9144179988302221

julia> x'
1×4 LinearAlgebra.Adjoint{Float64,Array{Float64,1}}:
 0.134632 0.428887 0.711309 0.914418

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 1, 2019, 11:49am UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/7 "2019-05-01T11:49:38Z")

</div>

> [@Pbellive](#):
>
> This isn’t an answer to your overall question but you’re getting the invalid assignment location error because `x'` is not a valid variable name.

On the other hand, `x′` is a valid variable name, where `′` ([U+2032](https://www.fileformat.info/info/unicode/char/2032/index.htm)) is typed in the REPL by tab-completing `\prime`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 1, 2019, 12:01pm UTC](https://discourse.julialang.org/t/help-how-to-have-my-function-named-penalty-method-run/23707/8 "2019-05-01T12:01:21Z")

</div>

> [@Engineer43](#):
>
> I want both of these equations to be positive to add to the penalty function so would entering q1 and q2 in p inside of the function penalty\_method be incorrect?

First, from your example, it seems that `c(x)` is a single function that returns a vector of the constraint values. In this case, you want:

```julia
q = c(x)

```

rather than calling `c(x)[1]` and `c(x)[2]`, which calls `c(x)` twice. And, and as I said, you still need to include `c` as an argument to `penalty_method`.

Second of all, `sum(q1>0)+sum(q2>0)` is probably not what you want. That adds 1 for each constraint that it positive.

In many penalty-function optimization methods, like augmented-Lagrangian methods, you add the square of the constraints, i.e. `f(x) + ρ*(q[1]^2 + q[2]^2)` for equality constraints, or `f(x) + ρ*sum(v -> v^2, q)`. If these are inequality constraints, then you probably want `f(x) + ρ*(max(q[1],0)^2 + max(q[2],0)^2)` (still squaring so that it has a continuous derivative), or `f(x) + ρ*sum(v -> max(v, 0)^2, q)`.

You apparently have another bug in your code because you are passing `x -> f(x) + ρ*p(x)` to `powell`, when `p(x)` already includes `f(x)` and `ρ`.
