How to Fix Error : ("syntax: cannot add method to function argument p")

Hi guys, I am trying to say that p in the function title is a function of x (it’s a penalty function that depends on if the constraints [c] are violated) but I keep getting this error. How can I re-classify p or change it so that p(x) is used in the equation and not just p? Furthermore, c is a 1x2 vector with two equations. Would the way I am using c also mean that it is evaluating c at point x, and then placing that value into p(x)? Thanks!

function penalty_method(f, p, x, k_max, c; ρ=2, γ=8)
for k in 1 : k_max
cs = c(x)
println("k: ", k)
p(z)=sum(c(z) .> 0)
x = hooke_jeeves(x -> f(x) + ρ*p(x), x, 1, .01, 2)
ρ *= γ
println("c(x): ", c(x))
if c(x)[1] <= 0 && c(x)[2] <= 0
return x
end
end
# println(“return!!”)
return x
end

The problem is that you’re using p as a function argument and then also trying to define a generic function p(x) inside the function. Since it doesn’t seem like you’re using the function argument, maybe you need to drop it?

I’ll try that really quickly. Thanks.

TLDR: Ideally, I want the function_optimize to run as few iterations as possible to get the function f to the constrained region, and then minimized within the constrained region.

Here’s a snapshot of what two iterations looks like:
Calling optimize
k: 1
c(x): [1.21346, -2.20998]
k: 2
c(x): [1.21346, -2.20998]
k: 3
c(x): [1.21346, -2.20998]
k: 4
c(x): [1.21346, -2.20998]
k: 5
c(x): [1.21346, -2.20998]
k: 6
c(x): [1.21346, -2.20998]
k: 7
c(x): [1.21346, -2.20998]
k: 8
c(x): [1.21346, -2.20998]
k: 9
c(x): [1.21346, -2.20998]
k: 10
c(x): [1.21346, -2.20998]
Calling optimize
k: 1
c(x): [0.97639, -2.0323]
k: 2
c(x): [0.97639, -2.0323]
k: 3
c(x): [0.97639, -2.0323]
k: 4
c(x): [0.97639, -2.0323]
k: 5
c(x): [0.97639, -2.0323]
k: 6
c(x): [0.97639, -2.0323]
k: 7
c(x): [0.97639, -2.0323]
k: 8
c(x): [0.97639, -2.0323]
k: 9
c(x): [0.97639, -2.0323]
k: 10
c(x): [0.97639, -2.0323]
Calling optimize

My goal is essentially this: Use function_optimize to call the penalty_method function and use the f, g, and c given by the function_optimize which is another file. Within the penalty_method function, I want to use the Hooke-Jeeves algorithm to minimize the penalty function value, which is f(x)+ rho*p(x) where p(x) is either, 0, 1, or 2 since there are two constraints that may be violated. I want that to be minimized and stopped, meaning that both constraints c(x)[1] and c(x)[2] are not violated, so c(x) is equal to zero, and therefore p(x) is. That will give me the x-value of a feasible point that then needs to be transferred to the interior points method function. This function will then use Hooke-Jeeves once again to minimize its value toward to global minimum, while remaining in the appropriate constrained region. However, it appears that Hooke-Jeeves is not pushing the function towards an x-value of feasibility, and the code just keeps resetting and running the optimize function instead. Ideally, I want the function optimize to run as few iterations as possible to get the function f, to the constrained region and minimized.

This is my full code