0

I understand what's going on here, I just want tips on how to catch my program before he goes into infinite loop. Here's my code :

while abs(error) > 50:
    if Co > To:
        c = c + 0.1
    else:
        c = c - 0.1

    ##Here I recalculate Co, the function is Co = 1/b type curve
    ##Here I recalculate To, the function is To = e^b type curve

    error = Co - To

The problem is that depending on the problem, I sometimes need to be more precise (I would need to change the iteration line to c+0.00001 for example) because the error jumps from a value below 50 to a value over 50 on each loop. How can I catch such an infinite loop.

4
  • 3
    Please fix the intending. Commented Sep 25, 2013 at 21:07
  • 1
    Based on the indentation of the code you're showing us, the value of error is NOT changing inside the while loop. Did you mean to indent error = C - T four more spaces to the right? Commented Sep 25, 2013 at 21:09
  • Just to clarify, does C = c? Or does the casing matter? Never mind, I skipped over your comments in the code. Commented Sep 25, 2013 at 21:20
  • Excatly, Recalculation Co and To is rather complex and not the issue here. What essentially happens is c has too large an impact on Co and To and i'm looping over and under with my error. Example c = 10.1 Co = 50 To = 200, error = -150, continue c = 10.2 Co = 150 To = 75, error = 75, continue c = 10.1 etc Commented Sep 25, 2013 at 21:21

2 Answers 2

2

I guess this is a question of convergence. My maths foo is failing me a little bit these days but if you're worried you could end up in a position where you may not converge, you could just add some counter to your loop.

limit = 1000
while abs(error) > 50:
    limit -= 1
    if limit == 0:
        raise SomeError
    your_calcs

It's a bit brutal, but at least it solves the issue of it hanging. It sounds as though you can't guarantee this loop will ever finish because of the nature of the changing code / data. If that infinite loop situation happened, I'd like to see an error raised so I could look into it. Depends on your problem domain really.

Sign up to request clarification or add additional context in comments.

2 Comments

No need for the parenthesis after SomeError. You only put those if you give the error an argument. Otherwise, raise SomeError will work just fine.
Not at all brutal. This is the solution for the same problem when it was initially raised in the 1960s. Sometimes a system of functions won't converge. Gotta pull the plug at some point.
0

If I understand what you're asking, I think you're looking for some kind of adaptive learning rate like you might see applied to the gradient descent method in an ANN, as well as a way to stop if things are no longer improving.

The basic idea is to slowly decrease the amount you are perturbing your values when you don't see a change in your abs error while maintaining stability in the overall process. If you've reduced your learning rate and you're still not seeing any improvement, you're done (or at least at some kind of local minima). This method can be a little slower, and there are different ways of calculating the variables I put in (e.g., sigErrChange), so you'll have to toy around with it a bit. There are some other caveats I can't think of off the top of my head, but hopefully this gets the general idea across.

e.g.,

lR = 1.0
updatedLR = False # Have we updated the learning rate this iteration?
while abs(error) > 50 and ( sigErrChange or updatedLR ):
    sigErrChange = False # Has there been a significant improvement in the error? (Probably shouldn't just use a single iteration for this...)

    # Are we adding or subtracting 
    if C > T:
        sign = 1.
    else:
        sign = -1.

    # Should we update the learning rate?
    if (~sigErrChange)
        updatedLR = True
        lR = .95 * lR

    # Calculate our values
    c = c + sign*lR*.001

    C = calcC(c, C, T)
    T = calcT(c, C, T)

    error = C - T

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.