Scilab optim function does not work - scilab

Can someone explain me how the optim function works in Scilab and give me a short example of that.
What I am trying to do is to maximize this function and find the optimal value
> function [f, g, ind]=cost(x, ind)
f= -x.^2
g=2*x
endfunction
// Simplest call
x0 = [1; -1; 1];
[fopt, xopt] = optim(cost, x0)
When I am trying to implement the function, I receive error
Variable returned by scilab argument function is incorrect.
I think I do some very basic mistake but can't understand where.

I think the answer is that -x.^2 does not return a scalar but a vector (x is a vector and x.^2 is an elementwise operation). You probably want to say something like x'*x. The objective function of an optimization problem should always be scalar (otherwise we end up with a multi-objective or multi-criteria problem which is a whole different type of problem).
Minimizing -x'*x is probably not a good idea
The gradient is not correct for f=-x'*x (but see previous point).

Related

Evaluation in definition

I am sorry about the title, but I couldn't find a better one.
Let's define
function test(n)
print("test executed")
return n
end
f(n) = test(n)
Every time we call f we get
f(5)
test executed
5
Is there a way to tell julia to evaluate test once in the definition of f?
I expect that this is probably not going to be possible, in which case I have a slightly different question. If ar=[1,2,:x,-2,2*:x] is there any way to define f(x) to be the sum of ar, i.e. f(x) = 3*x+1?
If you want to compile based on type information, you can use #generated functions. But it seems like you want to compile based on the runtime values of the input. In this case, you might want to do memoization. There is a library Memoize that provides a macro for memoizing functions.

Debugging in Julia Lang

Could someone please help me debug this code? I am almost certain there is nothing wrong but Julia keeps giving me an error. The code is basically implementing the problem statement. I am discretizing,then a function computing the sums to compute Erof, then taking the gradient to compute the gradient step used in gradient descent. The debugger in Julia is a nightmare, please help.
If someone has a clue to what the problem is please let me know.
You can see the error line. It says no method matching colon(::Int64, ::Tuple(Int64)). This means N in for i = 1:N is a tuple but it should not be a tuple. N must be an integer.
N = size(U) in line 3 returns a tuple regardless whether U is a Vector or a Multi-Dimensional Array
With range, you should use an integer. So change your N = size(U) to N = length(U) or add the dimension argument to your size call.

How to make nonsymbolic plot_vector_field in sage?

I have a function f(x,y) whose outcome is random (I take mean from 20 random numbers depending on x and y). I see no way to modify this function to make it symbolic.
And when I run
x,y = var('x,y')
d = plot_vector_field((f(x),x), (x,0,1), (y,0,1))
it says it can't cast symbolic expression to real or rationa number. In fact it stops when I write:
a=matrix(RR,1,N)
a[0]=x
What is the way to change this variable to real numbers in the beginning, compute f(x) and draw a vector field? Or just draw a lot of arrows with slope (f(x),x)?
I can create something sort of like yours, though with no errors. At least it doesn't do what you want.
def f(m,n):
return m*randint(100,200)-n*randint(100,200)
var('x,y')
plot_vector_field((f(x,y),f(y,x)),(x,0,1),(y,0,1))
The reason is because Python functions immediately evaluate - in this case, f(x,y) was 161*x - 114*y, though that will change with each invocation.
My suspicion is that your problem is similar, the immediate evaluation of the Python function once and for all. Instead, try lambda functions. They are annoying but very useful in this case.
var('x,y')
plot_vector_field((lambda x,y: f(x,y), lambda x,y: f(y,x)),(x,0,1),(y,0,1))
Wow, I now I have to find an excuse to show off this picture, cool stuff. I hope your error ends up being very similar.

R optim same function for fn and gr

I would like to use optim() to optimize a cost function (fn argument), and I will be providing a gradient (gr argument). I can write separate functions for fn and gr. However, they have a lot of code in common and I don't want the optimizer to waste time repeating those calculations. So is it possible to provide one function that computes both the cost and the gradient? If so, what would be the calling syntax to optim()?
As an example, suppose the function I want to minimize is
cost <- function(x) {
x*exp(x)
}
Obviously, this is not the function I'm trying to minimize. That's too complicated to list here, but the example serves to illustrate the issue. Now, the gradient would be
grad <- function(x) {
(x+1)*exp(x)
}
So as you can see, the two functions, if called separately, would repeat some of the work (in this case, the exponential function). However, since optim() takes two separate arguments (fn and gr), it appears there is no way to avoid this inefficiency, unless there is a way to define a function like
costAndGrad <- function(x) {
ex <- exp(x)
list(cost=x*ex, grad=(x+1)*ex)
}
and then pass that function to optim(), which would need to know how to extract the cost and gradient.
Hope that explains the problem. Like I said my function is much more complicated, but the idea is the same: there is considerable code that goes into both calculations (cost and gradient), which I don't want to repeat unnecessarily.
By the way, I am an R novice, so there might be something simple that I'm missing!
Thanks very much
The nlm function does optimization and it expects the gradient information to be returned as an attribute to the value returned as the original function value. That is similar to what you show above. See the examples in the help for nlm.

Gradient function in optim of R

I have two functions f(x) and g(x). Here f(x) is the objective function to minimize, and g(x) is the gradient function. My problem is for each trial x, the body of f(x) will compute a complicated matrix A(x), which will also be used in g(x). For the sake of efficiency, I don't want g(x) to repeat the computation of A. I am considering to make A(x) global by defining A <<- ... in the body of f(x). So g(x) can use A(x) directly. Because I don't know how optim in R iterates f(x) and g(x), I am not sure if this strategy is correct and efficient. Any suggestions and comments are welcome. Thanks.
Because you don't know how optim is going to call f and g you are going to have to make sure that any stashed A(x) is from the same x when you need it. It might call f(x1), f(x2), f(x3) and then g(x1).
One solution might be memoisation:
http://cran.r-project.org/web/packages/memoise/index.html
A memoised A(x) will store the return value for given input values and return that when given the same input values without recomputing. Obviously only works for non-stochastic functions (don't call any random number generators).
I'm not sure how you control the size of the cache, but the source code is all there.

Resources