I am using Genetic Algorithm in Matlab for optimization of a computationally expensive fitness function which also has constraints . I am right now imposing constraints in the form of penalty in to the objective function since constraint violation can only be calculated at the end of the function evaluation. I wanted to use nonlcon rather to satisfy the constraints.
But my problem is that the fitness function evaluation is expensive and I can't afford to do it again for checking of constraint violation. I have seen some nested function formulations where using output function I can accumulate all the individual variable values for every generation.
As per what I was thinking, Would it be possible to have sort of a matrix in which I can store all the individual values at the beginning of a generation update that matrix while my fitness evaluations and when I call nonlcon for constraint evaluation, then to look up that updated matrix for constraint violation. While I am trying to implement this, I have some doubts.
1) I remember reading in some forum that outputfcn for Genetic algo can be called either at the beginning of a generation or at the end. By default, it is at the end. I wont be able to execute my method if it calls at the end. Sadly I am not able to find how to call the outputfcn at the beginning rather than the end of a generation.
2) Since my fitness function is computationally expensive, I am using Parallel evaluations. So Would it be possible to implement the above mentioned idea while using parallel option in Matlab or it would create some difficulties?
Are you still looking for an answer? I had a similar problem and solved it here. I use two anonymous functions fitnessFunction and nonlconFunction in the ga which both point to my switchOutput function. They just pass an additional flag which output is requested. In the switchOutput, the expensive calculation is done for the first call with a specific input set and the results are stored. If there is another call with the same input set, the stored results are returned.
With this setup it doesn't matter it which order you call your fitness function and your constraint function. For the first call with a new input set, the results will be calculated and for any subsequent calls with the same inputs the saved results will be returned!
Related
In MATLAB, as far as I know, I should pass the handle of a cost function to the optimization function in order to optimize my problem. In my situation, I do not want to create a cost function and pass its handle to the optimization. I would like to setup an optimization and ask the optimization object for best new set of variables at each iteration. I, myself, calculate the cost and pass its value to the optimization object. I mean the algorithm might be as follows
1- setup the optimization object (optimization method, optimization sense, ...).
2- introduce the variables and their bounds and constraints to the optimization object
3- ask the optimization object for a set of variables
4- implement the variables to the physical black box and obtain the outputs
5- calculate the cost function for the monitored output
6- if the cost function does not satisfy my goal, inform the optimization object about the calculated value of the cost function and go to step 3.
7- end
As far as I checked the functions of MATLAB optimization toolbox, all of them need the handle of the cost function.
I am working on a third party software optimization problem using Scipy optimize.minimize with constraints and bounds (using the SLSQP method). Exactly I am giving inputs to a very complex function (can't write it here) that will launch my software and return me one output I need to minimize.
def func_to_minimize(param):
launch_software(param)
return software_output() # I retrieve the output after the software finish his computation
While working on it, I notice that during the optimization, the algorithm does not always respect the constraints.
However, the software I am trying to optimize cannot be run with certain inputs values (physical law not to be violated), I wrote these equations as constraints in my code. For example the output flow rate can't be greater than the input flow rate.
I would like to know if it is possible to respect these constraints even during the optimization.
One approach you could try is intercept param and check whether it's feasible before sending it to launch_software. If it's not feasible then return np.inf. I'm not sure that this will work with SLSQP, but give it a go.
One approach, that will work, would be to use optimize.differential_evolution. That function examines to see if your constraints are feasible before calculating the objective function; if it's not then your objective function isn't called. However, differential_evolution does ask for orders of magnitude more function evaluations, so if your objective function is expensive then that could be an issue. One amelioration would be vectorisation or parallel computation.
If I know right- for trying to fit a model; some kind of iterative algorithm is used where the goal is to minimize a cost function (e.g. OLS, MSE, RMSE, MMSE).
I know the robustfit() method do the fitting for a regression model using OLS (Ordinary least squares) cost function and then performs an additional weighted regression to provide the final model. Also, I think fitlm() uses RMSE as the cost function.
My first query is: in Matlab, whether the cost function and weight function are same or not.
Also, how to provide my custom cost function (e.g. MSE) while letting MATLAB do the fitting?
I came to know that, robustfit() can take additional/custom weight function. But again I am confused will that be treated as cost function? Or I need to use some other kind of argument for providing my custom cost function?
Just like you said, the cost function is the function you want to minimize to find the correct coefficients of your predictor. For example, a cost function could be cost(a) = sqrt(mean((a).^2));, a usually being y - y_est.
In another side, the weight function, regarding to Robust Regression, is a way to give less influence or to remove accidental measurements for the algorithm. If you look at the shape of a weight function:
You will see that the further residuals are from zero, the less they are taken in account (sometimes even completely removed) . This is a way to avoid mistakes due to outliers.
The way the function estimates error is not open to custom, only the weight function is.
In my model each agent solves a system of ODEs at each tick. I have employed Eulers method (similar to the systems dynamics modeler in NetLogo) to solve these first order ODEs. However, for a stable solution, I am forced to use a very small time step (dt), which means the simulation proceeds very slowly with this method. I´m curious if anyone has advice on a method to solve the ODEs more quickly? I am considering implementing Runge-Kutta (with a larger time step?) as was done here (http://academic.evergreen.edu/m/mcavityd/netlogo/Bouncing_Ball.html). I would also consider using the R extension and using an ODE solver in R. But again, the ODEs are solved by each agent, so I don´t know if this is an efficient method.
I´m hoping someone has a feel for the performance of these methods and could offer some advice. If not, I will try to share what I find out.
In general your idea is correct. For a method of order p to reach a global error level tol over an integration interval of length T you will need a step size in the magnitude range
h=pow(tol/T,1.0/p).
However, not only the discretization error accumulates over the N=T/h steps, but also the floating point error. This gives a lower bound for useful step sizes of magnitude h=pow(T*mu,1.0/(p+1)).
Example: For T=1, mu=1e-15 and tol=1e-6
the Euler method of order 1 would need a step size of about h=1e-6 and thus N=1e+6 steps and function evaluations. The range of step sizes where reasonable results can be expected is bounded below by h=3e-8.
the improved Euler or Heun method has order 2, which implies a step size 1e-3, N=1000 steps and 2N=2000 function evaluations, the lower bound for useful step sizes is 1e-3.
the classical Runge-Kutta method has order 4, which gives a required step size of about h=3e-2 with about N=30 steps and 4N=120 function evaluations. The lower bound is 1e-3.
So there is a significant gain to be had by using higher order methods. At the same time the range where step size reduction results in a lower global error also gets significantly narrower for increasing order. But at the same time the achievable accuracy increases. So one has to knowingly care when the point is reached to leave well enough alone.
The implementation of RK4 in the ball example, as in general for the numerical integration of ODE, is for an ODE system x'=f(t,x), where x is the, possibly very large, state vector
A second order ODE (system) is transformed to a first order system by making the velocities members of the state vector. x''=a(x,x') gets transformed to [x',v']=[v, a(x,v)]. The big vector of the agent system is then composed of the collection of the pairs [x,v] or, if desired, as the concatenation of the collection of all x components and the collection of all v components.
In an agent based system it is reasonable to store the components of the state vector belonging to the agent as internal variables of the agent. Then the vector operations are performed by iterating over the agent collection and computing the operation tailored to the internal variables.
Taking into consideration that in the LOGO language there are no explicit parameters for function calls, the evaluation of dotx = f(t,x) needs to first fix the correct values of t and x before calling the function evaluation of f
save t0=t, x0=x
evaluate k1 = f_of_t_x
set t=t0+h/2, x=x0+h/2*k1
evaluate k2=f_of_t_x
set x=x0+h/2*k2
evaluate k3=f_of_t_x
set t=t+h, x=x0+h*k3
evaluate k4=f_of_t_x
set x=x0+h/6*(k1+2*(k2+k3)+k4)
I need to solve this problem better described at the title. The idea is that I have two nonlinear equations in four variables, together with two nonlinear inequality constraints. I have discovered that the function fmincon is probably the best approach, as you can set everything I require in this situation (please let me know otherwise). However, I'm having some doubts at the implementation stage. Below I'm exposing the complete case, I think it's simple enough to be in its real form.
The first thing I did was to define the objective function in a separate file.
function fcns=eqns(x,phi_b,theta_b,l_1,l_2)
fcns=[sin(theta_b)*(x(1)*x(4)-x(2)*x(3))+x(4)*sqrt(x(1)^2+x(2)^2-l_2^2)-x(2)*sqrt(x(3)^2+x(4)^2-l_1^2);
cos(theta_b)*sin(phi_b)*(x(1)*x(4)-x(2)*x(3))+x(3)*sqrt(x(1)^2+x(2)^2-l_2^2)-x(1)*sqrt(x(3)^2+x(4)^2-l_1^2)];
Then the inequality constraints, also in another file.
function [c,ceq]=nlinconst(x,phi_b,theta_b,l_1,l_2)
c=[-x(1)^2-x(2)^2+l_2^2; -x(3)^2-x(4)^2+l_1^2];
ceq=[];
The next step was to actually run it in a script. Below, since the objective function requires extra variables, I defined an anonymous function f. In the next line, I did the same for the constraint (anonymous function). After that, it's pretty self explanatory.
f=#(x)norm(eqns(x,phi_b,theta_b,l_1,l_2));
f_c=#(x)nlinconst(x,phi_b,theta_b,l_1,l_2);
x_0=[15 14 16 18],
LB=0.5*[l_2 l_2 l_1 l_1];
UB=1.5*[l_2 l_2 l_1 l_1];
[res,fval]=fmincon(f,x_0,[],[],[],[],LB,UB,f_c),
The first thing to notice is that I had to transform my original objective function by the use of norm, otherwise I'd get a "User supplied objective function must return a scalar value." error message. So, is this the best approach or is there a better way to go around this?
This actually works, but according to my research (one question from stackoverflow actually!) you can guide the optimization procedure if you define an equality constraint from the objective function, which makes sense. I did that through the following code at the constraint file:
ceq=eqns(x,phi_b,theta_b,l_1,l_2);
After that, I found out I could use the deal function and define the constraints within the script.
c=#(x)[-x(1)^2-x(2)^2+l_2^2; -x(3)^2-x(4)^2+l_1^2];
f_c=#(x)deal(c(x),f(x));
So, which is the best method to do it? Through the constraint file or with this function?
Additionally, I found in MATLAB's documentation that it is suggested in these cases to set:
f=#(x)0;
Since the original objective function is already at the equality constraints. However, the optimization doesn't go beyond the initial guess obviously (the cost value is already 0 for every solution), which makes sense but leaves me wondering why is it suggested at the documentation (last section here: http://www.mathworks.com/help/optim/ug/nonlinear-systems-with-constraints.html).
Any input will be valued, and sorry for the long text, I like to go into detail if you didn't pick up on it yet... Thank you!
I believe fmincon is well suited for your problem. Naturally, as with most minimization problems, the objective function is a multivariate scalar function. Since you are dealing with a vector function, fmincon complained about that.
Is using the norm the "best" approach? The short answer is: it depends. The reason I say this is norm in MATLAB is, by default, the Euclidean (or L2) norm and is the most natural choice for most problems. Sometimes however, it may be easier to solve a problem (or more physically meaningful) to use an L1 or a more stringent infinity-norm. I defer a thorough discussion of norms to the following superb blog post: https://rorasa.wordpress.com/2012/05/13/l0-norm-l1-norm-l2-norm-l-infinity-norm/
As for why the example on Mathworks is formulated the way it is: they are solving a system of nonlinear equations - not minimizing a function. They first use the standard approach, using fsolve, but then they propose alternate methods of solving the same problem.
One such way is to reformulate solving the nonlinear equations as a minimization problem with an equality constraint. By using f=#(x)0 with fmincon, the objective function f is naturally already minimized, and the only thing that has to be satisfied in this case is the equality constraint - which would be the solution to the system of nonlinear equations. Clever indeed.