0

I am searching for a simple hill climbing Algorithm

It is for a large scale simulation, This is an example of how the "CurrentLocation" is changing.

import time
targ = 1.5  # target of t
location = 1   #starting point
step = 0.9  # step change starting point
a=0
b=0
while abs(targ-location)>0.05:
    if targ>location:
        if b==1: # If I already been at b
            b=0
            a=0
            step = step*(0.9)
            print 'stepChangeA'
            location =location +  abs(location*step)
        else:
            location =location +  abs(location*step)
            a=1
            print 'increase'
    else:
        if a==1:  #If I already been at a
            a=0
            b=0
            step = step*(0.9)
            print 'stepChangeB'
            location =location -  abs(location*step)
        else:
            location = location - abs(location*step)
            b=1
            print 'decrease'
    time.sleep(0.1)  # just so it will be easy to see the change.. 
    print location 

The algorithm that I presented is working, But I feel that there most be something more effective... any advise ?

Thank You

Oren
  • 4,711
  • 4
  • 37
  • 63
  • 1
    I've done similar things with binary and ternary search if that helps. – Rusty Rob Jul 29 '14 at 02:41
  • 1
    your statements will interpret `a` and `b` as as `True`/`False` automatically (no need for `==0`). Not that useful, but it'll save precious keystrokes in the long run. Also interesting but not necessarily useful: The first if else can be a one liner `a,b,step,c,location = (0,0,step*.9,'stepA',location) if b else (1,b,step,'increase',location+abs(location*step))`. – Dan Jul 29 '14 at 05:30
  • 1
    Probably a better fit for [code review](http://codereview.stackexchange.com). – Ioannis Aug 07 '14 at 09:50

0 Answers0