Accord.net: Why can I not set the upper/lower bounds for some optimization methods? - limit

I'm using the Nelder-Mead optimizer from Accord.net. There are properties on this class named UpperBound and LowerBound, but they are get-only.
In another optimizer class BFGS, it is possible to set these properties.
The thing I am optimizing definitely has boundaries, as it's a physical thing mounted on servos. So, two questions:
Why can I not set the limits for Nelder-Mead?
If an optimization method is unbounded, what is a good way to handle this inside my objective function?

Related

Fortran performance, allocate at each subroutine call or declare a global variable and allocate once

I have a subroutine which allocates four vectors with size = 9, and does some operations on it. These vectors are only used inside this subroutine. The subroutine is called thousands of times during my program run.
Is it worth it to allocate the vectors each time the subroutine is called or it is better to create a global variable and only allocate the vectors once?
For very big working arrays (large 3D arrays) I do declare (global) module variables and then have a clean-up subroutine in the module handy.
However, for vectors of size 9 that really is not necessary. Allocating these on the stack as local variables costs nothing. A local variable of size 9, be it explicit or automatic, costs zero to allocate on the stack. The Fortran standard does not let you decide where the allocation happens but the compilers typically do. And stack tends to be the default.
Static storage is also possible (effectively forced by the save attribute) but seems unnecessary here.
Global variables aren't recommended in this case due to two reasons:
These variables are only used within the scope of a singular subroutine, setting up a global variable for this purpose is very bad code style.
Allocating global variables for specific operations is bad for the expandability of your code.
In this case, a parent subroutine can help: Do not define the vectors within a global scope, but instead, set up a parent subroutine that recieves these vectors as arguments, and let this parent subroutine call the respective (sub-)subroutines. Your vectors are defined within the scope of the parent routine, which means the parent subroutine can pass the vectors to the (sub-)subroutines without needing to access any global references. This would make your code both style-conform and (relatively) more efficient, compared to re-allocating the same vectors every time your subroutine is called.

Generate a Random number in Uppaal

My question is Can I generate a random number in Uppaal?
I would like to generate a number from a range of values. Even more, I would like to generate not just integers I would like to generate double values as well.
for example: double [7.25,18.3]
I found this question that were talking about the same. I tried it.
However, I got this error: syntax error unexpected T_SELECT.
It doesn't work. I'm pretty new in Uppaal world, I would appreciate any help that you can provide me.
Regards,
This is a common and misunderstood question in Uppaal.
Simple answer:
double val; // declaration
val = random(18.3-7.25)+7.25; // use in update, works in SMC (Uppaal v4.1)
Verbose answer:
Uppaal supports symbolic analysis as well as statistical and the treatment and possibilities are radically different. So one has to decide first what kind of analysis is needed. Usually one starts with simple symbolic analysis and then augment with stochastic features, sometimes stochastic behavior needs also to be checked symbolically.
In symbolic analysis (queries A[], A<>, E<>, E[] etc), random is synonymous with non-deterministic, i.e. if the model contains some "random" behavior, then verification should check all of them any way. Therefore such behavior is modelled as non-deterministic choices between edges. It is easy to setup a set of edges over an integer range by using select statement on the edge where a temporary variable is declared and its value can be used in guards, synchronization and update. Symbolic analysis supports only integer data types (no floating point types like double) and continuous ranges over clocks (specified by constraints in guards and invariants).
Statistical analysis (via Monte-Carlo simulations, queries like Pr[...](<> p), E[...](max: var), simulate, etc) supports double types and floating point functions like sin, cos, sqrt, random(MAX) (uniform distribution over [0, MAX)), random_normal(mean, dev) etc. in addition to int data types. Clock variables can also be treated as floating point type, except that their derivative is set to 1 by default (can be changed in the invariants which allow ODEs -- ordinary differential equations).
It is possible to create models with floating point operations (including random) and still apply symbolic analysis provided that the floating point variables do not influence/constrain the model behavior, and act merely as a cost function over the state space. Here are systematic rules to achieve this:
a) the clocks used in ODEs must be declared of hybrid clock type.
b) hybrid clock and double type variables cannot appear in guard and invariant constraints. Only ODEs are allowed over the hybrid clocks in the invariant.

Optimization Toolbox (fmincon) - How to set logical constraints?

Hello all :) I'm pretty new to Optimization and barely understand it (was about ready to slit my wrist after figuring out how to write Objective Functions without any formal learning on the matter), and need a little help on a work project.
How would I go about setting a logical constraint when using the Optimization Toolbox, fmincon specifically (using Trust Region Reflective algorithm)?
I am optimizing 5 values (lets call it matrix OptMat), and I want to optimize with the constraint such that
max(OptMat)/min(OptMat) > 10
I assume this will optimize the 5 values of OptMat as low as possible, while keeping the above constraint in mind so that if a set of values for OptMat is found with a lower OF in which it breaks the constraint it will NOT report those values and instead report the next lowest OF where OptMat values meet the above constraint?
For the record, my lower bounds are [0,0,0,0,0]. I'm not sure how to enter it into upper bounds as it only accepts doubles and that would be logical. I tried the Active Set Algorithm and that enabled the Nonlinear Constraint Function box and I think I'm on the right track with that. If so, I'm not sure what the syntax for entering my desired constraint. Another method^that ^may ^or ^may ^not ^work I could think of is using this as an Upper Boundary.
[min(OptMat)*10, min(OptMat)*10, min(OptMat)*10, min(OptMat)*10, min(OptMat)*10]
Again, I'm using the GUI Optimization Toolbox. I haven't looked too much into command line optimization (though I will need to write it command line eventually) and I think I read somewhere that you can set the Upper Boundary and it does not have to be double?
Thank you so very much for the help, if someone is able. I apologize if this is a really nooby question.
What you are looking for are nonlinear constraints, fmincon can handle it (I only know the command, not the GUI) with the argument nonlcon. For more information look at this guide http://de.mathworks.com/help/optim/ug/fmincon.html
How would you implement this? First create a function
function [c, ceq] = mycondition(x)
c = -max(x)/min(x)/10;
ceq = 0;
I had to change the equation to match the correct formalism, i.e. c(x)<=0 is needed.
Maybe you could also create an anonymous function, I'm not sure (http://de.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html).
Then use this function to feed the fmincon function using the # sign, i.e. at the specific location write
fmincon(...., #mycondition, ...)

Fastest data structure with default values for undefined indexes?

I'm trying to create a 2d array where, when I access an index, will return the value. However, if an undefined index is accessed, it calls a callback and fills the index with that value, and then returns the value.
The array will have negative indexes, too, but I can overcome that by using 4 arrays (one for each quadrant around 0,0).
You can create a Matrix class that relies on tuples and dictionary, with the following behavior :
from collections import namedtuple
2DMatrixEntry = namedtuple("2DMatrixEntry", "x", "y", "value")
matrix = new dict()
defaultValue = 0
# add entry at 0;1
matrix[2DMatrixEntry(0,1)] = 10.0
# get value at 0;1
key = 2DMatrixEntry(0,1)
value = {defaultValue,matrix[key]}[key in matrix]
Cheers
This question is probably too broad for stackoverflow. - There is not a generic "one size fits all" solution for this, and the results depend a lot on the language used (and standard library).
There are several problems in this question. First of all let us consider a 2d array, we say this is simply already part of the language and that such an array grows dynamically on access. If this isn't the case, the question becomes really language dependent.
Now often when allocating memory the language automatically initializes the spots (again language dependent on how this happens and what the best method is, look into RAII). Though I can foresee that actual calculation of the specific cell might be costly (compared to allocation). In that case an interesting thing might be so called "two-phase construction". The array has to be filled with tuples/objects. The default construction of an object sets a bit/boolean to false - indicating that the value is not ready. Then on acces (ie a get() method or a operator() - language dependent) if this bit is false it constructs, else it just reads.
Another method is to use a dictionary/key-value map. Where the key would be the coordinates and the value the value. This has the advantage that the problem of construct-on-access is inherit to the datastructure (though again language dependent). The drawback of using maps however is that lookup speed of a value changes from O(1) to O(logn). (The actual time is widely different depending on the language though).
At last I hope you understand that how to do this depends on more specific requirements, the language you used and other libraries. In the end there is only a single data structure that is in each language: a long sequence of unallocated values. Anything more advanced than that depends on the language.

Checking what choices Mathematica makes when you specify "Automatic"

So I'm doing some benchmarking of a method for numerical optimization in Mathematica and I'm getting some inconsistent results when I use the Method->Automatic specification with FindMinimum. What I want to do is check what method it is choosing. I know I can use AbsoluteOptions[] to extract the choices from a some output, like a Graphics object, but this doesn't work with FindMinimum, ie
AbsoluteOptions[ Findminimum[ f[x], {x, 0.}, Method->Automatic ], Method ]
doesn't work. Can I do this?
I don't think there is a general way to find what method is used by numerical functions, other than reading the documentation. The documentation on unconstrained optimization is pretty good, though. There it says:
With Method -> Automatic, Mathematica uses the "quasi-Newton" method
unless the problem is structurally a sum of squares, in which case
the Levenberg-Marquardt variant of the "Gauss-Newton" method is used.
When given two starting conditions in each variable, the "principal axis"
method is used.
Additionally, for constrained problems the interior point method is used. If the problem is linear (objective and constraints) linear programming will be used. (LP itself has, I think, three methods that are used, depending on size and other features.)

Resources