def rolloutPolicy(dynamics,x,control,dt,numSteps):
    """Returns a state trajectory and control trajectory of length
    numSteps+1 and numSteps, respectively.

    control can either be a callable policy (closed loop) or a list
    of length at least numSteps (open loop).

    A control of None means to terminate.
    """
    xs = [x]
    us = []
    if callable(control):
        while len(xs) <= numSteps:
            u = control(xs[-1])
            if u is None: break
            us.append(u)
            xnext = dynamics.nextState(xs[-1],u,dt)
            #print "After",xs[-1],"control",u,"for time",dt,"result is",xnext
            xs.append(xnext)
    else:
        assert len(control) >= numSteps
        while len(xs) <= numSteps:
            u = control[len(xs)-1]
            if u is None: break
            us.append(u)
            xnext = dynamics.nextState(xs[-1],u,dt)
            xs.append(xnext)
    return xs,us