OpenMDAO handling errors within groups - genetic-algorithm

I am trying to run an optimization with SimpleGADriver in OpenMDAO. My model includes a cycle (subsystem) with a nonlinear solver, that contains a component that on occasions fails to compute its response. Is there any way to make this driver continue to the next iteration (iteration of the driver) on encountering this error, i.e., is there any way to break the nonlinear solver's solution and proceed with the next iteration of the driver?

Assuming that your component knows when it fails to compute its response, it can raise an om.AnalysisError. When the SimpleGADriver detects this, it will mark this as a failed point and move on to the next point to evaluate in the current generation. However, if you have nested nonlinear solvers (i.e., solvers in more than one group in the hierarchy above your component), then you also need to set reraise_child_analysiserror to True in those solvers.
I looked through our documentation, and there isn't an example of this, but you can see AnalysisError usage in this test:
https://github.com/OpenMDAO/OpenMDAO/blob/master/openmdao/test_suite/components/ae_tests.py
You can also see a nested solver case with reraise_child_analysiserror in this test:
https://github.com/OpenMDAO/OpenMDAO/blob/3460c29adba3b280fba84addd16a56036b9332ad/openmdao/solvers/nonlinear/tests/test_nonlinear_block_gs.py#L24

Related

Modelica events and hybrid modelling

I would like to understand the general idea behind hybrid modelling (in particular state events) from a numerical point of view (although I am not a mathematician :)). Given the following Modelica model:
model BouncingBall
constant Real g=9.81
Real h(start=1);
Real v(start=0);
equation
der(h)=v;
der(v)=-g;
algorithm
when h < 0 then
reinit(v,-pre(v));
end when;
end BouncingBall;
I understand the concept of when and reinit.
The equation in the when statement are only active when the condition become true right?
Let's assume that the ball would hit the floor at exactly 2sec. Since I am using multi-step solver does that mean that the solver "goes beyond 2 seconds", recognizes that h<0 (lets assume at simulation time = 2.5sec , h = -0.7). What does this mean "The time for the event is searched using a crossing function? Is there a simple explanation(example)?
Is the solver now going back? Taking a smaller step-size?
What does the pre() operation mean in that context?
noEvent(): "Expressions are taken literally instead of generating crossing functions. Since there is no crossing function, there is no requirement tat the expression can be evaluated beyond the event limit": What does that mean? Given the same example with the bouncing ball: The solver detects at time 2.5 that h = 0.7. Whats the difference between with and without noEvent()?
Yes, the body of when is only executed at events.
Simple view: The solver takes steps, and then uses a continuous extension to generate a (smooth) interpolation formula for the previous step. That interpolation formula can be used to generate a plot, and also for finding the first point where h has crossed zero (likely 2.000000001). An event iteration is then done at that interpolated point - and afterwards the solver is restarted.
I wouldn't say that the solver goes back. It takes a partial step and then continues forward. Some solvers need to reduce the step-size a lot after the event - others don't.
pre(x) is set to the value of x before the event.
noEvent(h<0) basically means evaluate the expression as written without all the bells-and-whistles of crossing functions. You cannot use when noEvent(h<0) then
There are many additional point:
If you are familiar with Sturm-sequences or control theory you might realize that it is not necessary to interpolate a formula to determine if it crossed zero or not in an interval (and some tools use that). The fact that the function is not necessarily smooth makes it a bit more complicated, and also means that derivative-tests cannot be used.
How much the solver is reset depends on the kind of solver. One-step solvers (Runge-Kutta) can be restarted directly as if virtually nothing happened, whereas multi-step solvers (BDF/Adams - such as dassl/lsodar/cvode) need to start with lower order and smaller step-size.

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, ...)

metafor() non-negative sampling variance

I am trying to learn meta regression using the metafor() package. In running
one of the mixed regression models, I received an error indicating
"There are outcomes with non-positive sampling variances."
I am at lost as to how to proceed with this error. I understand that certain
model statistics (e.g., I^2 and QE) cannot be computed with due to the
presence of non-positive sampling variances. However, I am not sure whether
these results can be interpreted similarly as we would have otherwise. I
also tried using other estimators and/or the unweighted option; the error
still persists.
Any suggestions would be much appreciated.
First of all, to clarify: You are getting a warning, not an error.
Aside from that, I can't think of many situations where it is reasonable to assume that the sampling variance is really equal to 0 in a particular study. I would first question whether this really makes sense. This is why the rma() function is generating this warning message -- to make the user aware of this situation and question whether this really is intended/reasonable.
But suppose that we really want to go through with this, then you have to use an estimator for tau^2 that can handle this (e.g., method="REML" -- which is actually the default). If the estimate of tau^2 ends up equal to 0 as well, then the model cannot be fitted at all (due to division by zero -- and then you get an error). If you do end up with a positive estimate of tau^2, then the results should be okay (but things like the Q-test, I^2, or H^2 cannot be computed then).

Prolog Smart Graph Algorithm

I am a university student who is being taught Prolog. I am only 3 weeks into the course and this is my first assignment.
The goal of the assignment is to create a graph searching algorithm that returns a path from goal to end and remembers that certain nodes are not reachable from specific nodes. The base code we must have is:
street(genoa,turin).
street(genoa,busalla).
street(milan,turin).
street(milan,rome).
street(milan,genoa).
street(genoa,rome).
street(rome,napoli).
actionStreet(A,B):- street(A,B); street(B,A).
giveSolution(actionStreet,genoa,X,Steps).
The first part of the assignment is to create a graph solving algorithm that does not have the destination in the solution more than once and I believe this code does this:
street(genoa,turin).
street(genoa,busalla).
street(milan,turin).
street(milan,rome).
street(milan,genoa).
street(genoa,rome).
street(rome,napoli).
actionStreet(A,B):- street(A,B);street(B,A).
isConnected(P,A,B):-call(P,A,B).
giveSolution(P,Start,End,Steps):-trip(P,Start,End,[Start],Steps).
trip(P,Node,Node,_,[Node]).
trip(P,Start,Finish,Visited,[Start|Path]):-
isConnected(P,Start,X),
not(member(X,Visited)),
trip(P,X,Finish,[X|Visited]).
So the bit I am stuck on is that to solve the overall problem you need to keep track of certain nodes that are unreachable from specific nodes. I think this can be done using a dynamic database, that stores the nodes and the other node that is not reachable from the first node.
Therefore when the trip(P,Start,Finish,Visited,[Start|Path]):- predicate fails, the node X and the node End being looked at should be put in the database. I thought this could be done by using this change to the trip(P,Start,Finish,Visited,[Start|Path]):- predicate code:
trip(P,Start,Finish,Visited,[Start|Path]):-
(isConnected(P,Start,X),
not(member(X,Visited)),
trip(P,X,Finish,[X|Visited])-> true;assert(cannot(X,Finish))).
However, the if then predicate does not work in the way it would in Java. Can someone please help and explain why this code fragment does not do exactly what the first version does, except add to the database if those conditions are fail?
You are inverting the 'logical truth' of your predicate. that is, when the original trip/5 succeeded, now it's failing, and when it was failing, it now succeeds and remember cannot(X,Finish). And X should be uninstantiated at that point...
Controlling execution flow of Prolog can be tricky, since it may depend on the presence of other clauses, where they are located, what these clauses do... I don't claim much merit on this clue

Detecting infinite loop in brainfuck program

I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is, the program turns out to have an infinite loop in a sizeable number of cases, and hence the GA gets stuck at the point.
So, I need a mechanism to detect infinite loops and avoid executing that code in bf.
One obvious (trivial) case is when I have
[]
I can detect this and refuse to run that program.
For the non-trivial cases, I figured out that the basic idea is: to determine how one iteration of the loop changes the current cell. If the change is negative, we're eventually going to reach 0, so it's a finite loop. Otherwise, if the change is non-negative, it's an infinite loop.
Implementing this is easy for the case of a single loop, but with nested loops it becomes very complicated. For example, (in what follows (1) refers to contents of cell 1, etc. )
++++ Put 4 in 1st cell (1)
>+++ Put 3 in (2)
<[ While( (1) is non zero)
-- Decrease (1) by 2
>[ While( (2) is non zero)
- Decrement (2)
<+ Increment (1)
>]
(2) would be 0 at this point
+++ Increase (2) by 3 making (2) = 3
<] (1) was decreased by 2 and then increased by 3, so net effect is increment
and hence the code runs on and on. A naive check of the number of +'s and -'s done on cell 1, however, would say the number of -'s is more, so would not detect the infinite loop.
Can anyone think of a good algorithm to detect infinite loops, given arbitrary nesting of arbitrary number of loops in bf?
EDIT: I do know that the halting problem is unsolvable in general, but I was not sure whether there did not exist special case exceptions. Like, maybe Matlab might function as a Super Turing machine able to determine the halting of the bf program. I might be horribly wrong, but if so, I would like to know exactly how and why.
SECOND EDIT: I have written what I purport to be infinite loop detector. It probably misses some edge cases (or less probably, somehow escapes Mr. Turing's clutches), but seems to work for me as of now.
In pseudocode form, here it goes:
subroutine bfexec(bfprogram)
begin
Looping through the bfprogram,
If(current character is '[')
Find the corresponding ']'
Store the code between the two brackets in, say, 'subprog'
Save the value of the current cell in oldval
Call bfexec recursively with subprog
Save the value of the current cell in newval
If(newval >= oldval)
Raise an 'infinite loop' error and exit
EndIf
/* Do other character's processings */
EndIf
EndLoop
end
Alan Turing would like to have a word with you.
http://en.wikipedia.org/wiki/Halting_problem
When I used linear genetic programming, I just used an upper bound for the number of instructions a single program was allowed to do in its lifetime. I think that this is sensible in two ways: I cannot really solve the halting problem anyway, and programs that take too long to compute are not worthy of getting more time anyway.
Let's say you did write a program that could detect whether this program would run in an infinite loop. Let's say for the sake of simplicity that this program was written in brainfuck to analyze brainfuck programs (though this is not a precondition of the following proof, because any language can emulate brainfuck and brainfuck can emulate any language).
Now let's say you extend the checker program to make a new program. This new program exits immediately when its input loops indefinitely, and loops forever when its input exits at some point.
If you input this new program into itself, what will the results be?
If this program loops forever when run, then by its own definition it should exit immediately when run with itself as input. And vice versa. The checker program cannot possibly exist, because its very existence implies a contradiction.
As has been mentioned before, you are essentially restating the famous halting problem:
http://en.wikipedia.org/wiki/Halting_problem
Ed. I want to make clear that the above disproof is not my own, but is essentially the famous disproof Alan Turing gave back in 1936.
State in bf is a single array of chars.
If I were you, I'd take a hash of the bf interpreter state on every "]" (or once in rand(1, 100) "]"s*) and assert that the set of hashes is unique.
The second (or more) time I see a certain hash, I save the whole state aside.
The third (or more) time I see a certain hash, I compare the whole state to the saved one(s) and if there's a match, I quit.
On every input command ('.', IIRC) I reset my saved states and list of hashes.
An optimization is to only hash the part of state that was touched.
I haven't solved the halting problem - I'm detecting infinite loops while running the program.
*The rand is to make the check independent of loop period
Infinite loop cannot be detected, but you can detect if the program is taking too much time.
Implement a timeout by incrementing a counter every time you run a command (e.g. <, >, +, -). When the counter reaches some large number, which you set by observation, you can say that it takes very long time to execute your program. For your purpose, "very long" and infinite is a good-enough approximation.
As already mentioned this is the Halting Problem.
But in your case there might be a solution: The Halting Problem is considering is about the Turing machine, which has unlimited memory.
In case you know that you have a upper limit of memory (e.g. you know you dont use more than 10 memory cells), you can execute your programm and stop it. The idea is that the computation space bounds computation time (as you cant write more than one cell at one step). After you executed as much steps as you can have different memory configurations, you can break. E.g. if you have 3 cells, with 256 conditions, you can have at most 3^256 different states, and so you can stop after executing that many steps. But be careful, there are implicit cells, like the instruction pointer and the registers. You do it even shorter, if you save every state configuration and as soon as you detect one, which you already had, you have an infite loop. This approach is definitly much better in the run time, but therefor needs much more space (here it might be suitable to hash the configurations).
This is not the halting problem, however, it is still not reasonable to try to detect halting even in such a limited machine as a 1000 cell BF machine.
Consider this program:
+[->[>]+<[-<]+]
This program will not repeat until it has filled up the entire of memory which for just 1000 cells will take about 10^300 years.
If I remember correctly, the halting problem proof was only true for some extreme case that involved self reference. However it's still trivial to show a practical example of why you can't make an infinite loop detector.
Consider Fermat's Last Theorem. It's easy to create a program that iterates through every number (or in this case 3 numbers), and detects if it's a counterexample to the theorem. If so it halts, otherwise it continues.
So if you have an infinite loop detector, it should be able to prove this theorem, and many many others (perhaps all others, if they can be reduced to searching for counterexamples.)
In general, any program that involves iterating through numbers and only stopping under some condition, would require a general theorem prover to prove if that condition can ever be met. And that's the simplest case of looping there is.
Off the top of my head (and I could be wrong), I would think it would be a little bit difficult to detect whether or not a program has an infinite loop without actually executing the program itself.
As the conditional execution of portions of the program depends on the execution state of the program, it will be difficult to know the particular state of the program without actually executing the program.
If you don't require that a program with an infinite loop be executed, you could try having an "instructions executed" counter, and only execute a finite number of instructions. This way, if a program does have an infinite loop, the interpreter can terminate the program which is stuck in an infinite loop.

Resources