Exemplo n.º 1
0
    def _setupDynamicsConstraints(self,endTime,traj):
        # Todo: add parallelization
        # Todo: get endTime right
        g = []
        nicp = 1
        deg = 4
        p = self._dvMap.pVec()
        for k in range(self.nk):
            newton = Newton(LagrangePoly,self.dae,1,nicp,deg,'RADAU')
            newton.setupStuff(endTime)
            
            X0_i = self._dvMap.xVec(k)
            U_i  = self._U[k,:].T

            # guess
            if traj is None:
                newton.isolver.setOutput(1,0)
            else:
                X = C.DMatrix([[traj.lookup(name,timestep=k,degIdx=j) for j in range(1,traj.dvMap._deg+1)] \
                               for name in traj.dvMap._xNames])
                Z = C.DMatrix([[traj.lookup(name,timestep=k,degIdx=j) for j in range(1,traj.dvMap._deg+1)] \
                               for name in traj.dvMap._zNames])
                newton.isolver.setOutput(C.veccat([X,Z]),0)
            _, Xf_i = newton.isolver.call([X0_i,U_i,p])
            X0_i_plus = self._dvMap.xVec(k+1)
            g.append(Xf_i-X0_i_plus)
        return g
Exemplo n.º 2
0
    def _setupDynamicsConstraints(self, endTime, traj):
        # Todo: add parallelization
        # Todo: get endTime right
        g = []
        nicp = 1
        deg = 4
        p = self._dvMap.pVec()
        for k in range(self.nk):
            newton = Newton(LagrangePoly, self.dae, 1, nicp, deg, 'RADAU')
            newton.setupStuff(endTime)

            X0_i = self._dvMap.xVec(k)
            U_i = self._U[k, :].T

            # guess
            if traj is None:
                newton.isolver.setOutput(1, 0)
            else:
                X = C.DMatrix([[traj.lookup(name,timestep=k,degIdx=j) for j in range(1,traj.dvMap._deg+1)] \
                               for name in traj.dvMap._xNames])
                Z = C.DMatrix([[traj.lookup(name,timestep=k,degIdx=j) for j in range(1,traj.dvMap._deg+1)] \
                               for name in traj.dvMap._zNames])
                newton.isolver.setOutput(C.veccat([X, Z]), 0)
            _, Xf_i = newton.isolver.call([X0_i, U_i, p])
            X0_i_plus = self._dvMap.xVec(k + 1)
            g.append(Xf_i - X0_i_plus)
        return g
Exemplo n.º 3
0
	def step(self,t,u,h):
		system = self.system
		noise = np.random.normal(size=[len(system.noise(t,u).T)])
		def residual(v):
			return (system.mass(t+h,v) - system.mass(t,u))/h - system.deterministic(t+h,v) - np.sqrt(h)/h*np.dot(system.noise(t,u),noise)
		N = Newton(residual)
## 		N.xtol = min(N.xtol, h*1e-4)
		result = N.run(u)
		return t+h, result
Exemplo n.º 4
0
    def step(self, t, u, h):
        system = self.system
        noise = np.random.normal(size=[len(system.noise(t, u).T)])

        def residual(v):
            return (
                (system.mass(t + h, v) - system.mass(t, u)) / h
                - system.deterministic(t + h, v)
                - np.sqrt(h) / h * np.dot(system.noise(t, u), noise)
            )

        N = Newton(residual)
        ## 		N.xtol = min(N.xtol, h*1e-4)
        result = N.run(u)
        return t + h, result
Exemplo n.º 5
0
 def _setupDynamicsConstraints(self):
     # Todo: add parallelization
     # Todo: add initialization 
     g = []
     nicp = 10
     deg = 4
     p = self._dvMap.pVec()
     for k in range(self.nk):
         newton = Newton(LagrangePoly,self.dae,1,nicp,deg,'RADAU')
         endTime = 0.05
         newton.setupStuff(endTime)
         
         X0_i = self._dvMap.xVec(k)
         U_i  = self._dvMap.uVec(k)
         _, Xf_i = newton.isolver.call([X0_i,U_i,p])
         X0_i_plus = self._dvMap.xVec(k+1)
         g.append(Xf_i-X0_i_plus)
     return g
Exemplo n.º 6
0
    def _setupDynamicsConstraints(self):
        # Todo: add parallelization
        # Todo: add initialization
        g = []
        nicp = 10
        deg = 4
        p = self._dvMap.pVec()
        for k in range(self.nk):
            newton = Newton(LagrangePoly, self.dae, 1, nicp, deg, 'RADAU')
            endTime = 0.05
            newton.setupStuff(endTime)

            X0_i = self._dvMap.xVec(k)
            U_i = self._dvMap.uVec(k)
            _, Xf_i = newton.isolver.call([X0_i, U_i, p])
            X0_i_plus = self._dvMap.xVec(k + 1)
            g.append(Xf_i - X0_i_plus)
        return g
Exemplo n.º 7
0
class TestGaussianClass(unittest.TestCase):
    def setUp(self):
        self.newton = Newton(f='x**2 - 9', max_iter=1e6)

    def test_initialization(self):
        self.assertEqual(self.newton.f, 'x**2 - 9', 'incorrect function')
        self.assertEqual(self.newton.max_iter, 1e6,
                         'incorrect maximum number of iterations')

    def test_solution1calc(self):
        self.assertEqual(round(self.newton.find_solution(1000), 2), 3.00,
                         'root incorrect')

    def test_solution2calc(self):
        self.assertEqual(round(self.newton.find_solution(-1000), 2), -3.00,
                         'root incorrect')

    def test_handlingZeroDivision(self):
        self.assertEqual(round(self.newton.find_solution(0), 2), 3.00,
                         'Zero Division error occured')
def test_simple_surface():
    # the class of test surface
    PES = SimpleSurface()
    # dimer algorithm
    ini_position = (np.random.rand(2) - 0.5) * 10
    ini_vector = np.random.rand(2)
    d = Dimer(2, ini_position, ini_vector, whether_print=False)
    position_d, times_d = d.work()  # 得到dimer运行轨迹和每一次的旋转数
    # print('vector', d.vector)
    # quasi newton method
    qN = Newton(PES.get_value, PES.get_diff,
                [position_d[-1, 0], position_d[-1, 1]])
    position_qN, times_qN = qN.bfgs_newton(PES.get_hess)

    position = np.concatenate((position_d, position_qN), axis=0)
    PES.show_point_2d(position)
    plt.plot(position_d[-1, 0], position_d[-1, 1], 'ko')
    PES.show_surface_2d(-5, 5)
    plt.title('Dimer rotates %d times and run %d times \n '
              'Newton runs %d times' % (sum(times_d), len(times_d), times_qN))
    plt.show()
Exemplo n.º 9
0
def solve(x0, method, oracle, visual=False):
    time_start = process_time()
    if method == "BFGS":
        F, x, G = bfgs(oracle, x0, visual=visual)
    if method == "Newton":
        F, x, G = Newton(oracle, x0, visual=visual)
    if method == "PR":
        F, x, G = Polak_Ribiere(oracle, x0, visual=visual)
    if method == "Gradient_F":
        F, x, G = Gradient_F(oracle, x0, visual=visual)
    cpu_time = process_time() - time_start
    print("Temps d'exécution : ", cpu_time)
    return F, x, G
Exemplo n.º 10
0
def run(step, point_type, lambda_p, method):

    f = Function(lambda_p)

    n = f.get_size()  # x size

    # Initial point
    if point_type == "const":
        x0 = np.ones(n)
    else:
        x0 = np.random.uniform(-2, 2, n)

    mxitr = 50000  # Max number of iterations

    tol_g = 1e-8  # Tolerance for gradient

    tol_x = 1e-8  # Tolerance for x

    tol_f = 1e-8  # Tolerance for function

    # Method for step update
    if step == "fijo":
        msg = "StepFijo"

    elif step == "hess":
        msg = "StepHess"

    else:
        msg = "Backtracking"

    step_size = 1  # Gradient step size for "StepFijo" method

    # Estimate minimum point through optimization method chosen
    if method == "gd":
        alg = GD()
    elif method == "newton":
        alg = Newton()
    else:
        print("\n Error: Invalid optimization method: %s\n" % method)
        return

    xs = alg.iterate(x0, mxitr, tol_g, tol_x, tol_f, f, msg, step_size)

    # Print point x found and function value f(x)
    # print("\nPoint x found: ", xs[-1])
    print("\nf(x) =  ", f.eval(xs[-1]))

    plt.plot(np.array(range(n)), xs[-1])
    plt.plot(np.array(range(n)), f.y)
    plt.legend(['x*', 'y'], loc = 'best')
    plt.show()
Exemplo n.º 11
0
def newton_results():
    iterastr = request.form.get("itera")
    x0str = request.form.get("x0")
    funstr = request.form.get("fun")
    dfunstr = request.form.get("dfun")
    tolstr = request.form.get("tol")

    if iterastr == '' or x0str == '' or funstr == '' or dfunstr == '' or tolstr == '':
        return "<h1 style='text-align: center;'>You are missing one or more values</h1>"

    try:
        itera = int(iterastr)
    except:
        return "<h1 style='text-align: center;'>Check iteration value</h1>"

    try:
        x0 = float(x0str)
    except:
        return "<h1 style='text-align: center;'>Check initial value</h1>"

    try:
        fun = sp.sympify(funstr)
    except:
        return "<h1 style='text-align: center;'>Check function entered</h1>"

    try:
        dfun = sp.sympify(dfunstr)
    except:
        return "<h1 style='text-align: center;'>Check </h1>"
    try:
        tol = sp.sympify(tolstr)
    except:
        return "<h1 style='text-align: center;'>Check tolerance entered {{tol}}</h1>"

    try:
        list_a, list_f, list_e, root = Newton(itera, x0, fun, dfun, tol)
    except:
        return "<h1 style='text-align: center;'>Check values entered</h1>"

    funplot = funstr.replace("**", "^")

    it = len(list_a)
    list_it = list(range(0, it))
    return render_template("newton.html",
                           list_a=list_a,
                           list_f=list_f,
                           list_e=list_e,
                           list_it=list_it,
                           root=root,
                           funplot=funplot)
Exemplo n.º 12
0
def newton_results():
    itera = request.form.get("itera")
    x0 = request.form.get("x0")
    fun = request.form.get("fun")
    dfun = request.form.get("dfun")
    tol = request.form.get("tol")

    if itera == '' or x0 == '' or fun == '' or dfun == '' or tol == '':
        return "<h1 style='text-align: center;'>Check Values Entered</h1>"

    list_a, list_f, list_e, root = Newton(itera, x0, fun, dfun, tol)

    it = len(list_a)
    list_it = list(range(0, it))
    return render_template("newton.html",
                           list_a=list_a,
                           list_f=list_f,
                           list_e=list_e,
                           list_it=list_it,
                           root=root)
Exemplo n.º 13
0
def test_Newton():
    from sympy import symbol, diff, lambdify

    x = symbol.Symbol('x')  # определяем математический символ x
    f_expr = x**2 - 4  # символьное выражение для f(x)
    dfdx_expr = diff(f_expr, x)  # вычисляем символьно f'(x)

    # Преобразуем f_expr и dfdx_expr в обычные функции Python
    f = lambdify(
        [x],  # аргумент f
        f_expr)
    dfdx = lambdify([x], dfdx_expr)

    tol = 1e-3
    computed, no_iterations = Newton(f, dfdx, x=1, eps=tol)
    expected = 2.
    success = np.abs(computed - expected) <= tol
    msg = 'expected = {},\n computed = {}, Число итераций = {}'.format(
        expected, computed, no_iterations)
    assert success, msg
Exemplo n.º 14
0
import sys
import itertools

# Mainlib
from score_regressor import get_best
from newton import Newton
from newton.knn import *
from newton.forest import *

app = Flask(__name__)
CORS(app)
setattr(sys.modules['__main__'], 'gower_distance', gower_distance)

MODEL_CACHE = 5
sisrec = Newton(np.array([i for i in range(1, 12)]),
                'newton/forest/serialized', 'newton/knn/serialized',
                'newton/data', MODEL_CACHE, 4, 5)


@app.route("/get_prediction", methods=["GET", "POST", "PUT"])
def get_predicition():
    # Para evitar problemas con Chrome
    # resp.headers['Access-Control-Allow-Origin'] = '*'
    # Data viene en formato json -> {}
    # {uid}

    if request.method == "GET":
        result = 'GET request is not allowed'

    elif request.method == "POST":
        # Diccionario de data que viene en el request
Exemplo n.º 15
0
from newton import Newton

f = '2*x**2 - 50'
newton = Newton(f)

newton.find_solution(1)
Exemplo n.º 16
0
def get_random_uniform_in_range(x_rng: np.array, y_rng: np.array) -> np.array:
    p = np.random.rand(2)

    p[0] *= x_rng[1] - x_rng[0]
    p[0] += x_rng[0]

    p[1] *= y_rng[1] - y_rng[0]
    p[1] += y_rng[0]

    return p


if __name__ == "__main__":

    opt = Newton(obj_func=obj_func,
                 gradient_func=gradient,
                 reg_inv_hessian=reg_inv_hessian)
    opt.log.setLevel(logging.INFO)

    x_rng = [-2, 2]
    y_rng = [-1, 1]

    fig_dir = "figures"
    Path(fig_dir).mkdir(parents=True, exist_ok=True)

    # params_init = get_random_uniform_in_range(x_rng, y_rng)
    # params_init = np.array([-1.55994695, -0.31833122])
    params_init = np.array([-1.1, -0.5])

    converged, no_opt_steps, final_update, traj, line_search_factors = opt.run(
        no_steps=10, params_init=params_init, tol=1e-8, store_traj=True)
Exemplo n.º 17
0
def f(x):
    return x**2 + 4 * np.sin(x)


left = int(input('Enter left border: '))
right = int(input('Enter right border: '))
n = int(input('Enter number of points: '))

bf = Bruteforce(left, right, n, f)
bf.FirstStep()
bf.plots()

s1 = Secant.find(f, a=-2, b=-1)
s2 = Secant.find(f, a=-1, b=1)
b1 = Bisect.find(f, a=-2, b=-1)
b2 = Bisect.find(f, a=-1, b=1)

print("\nSecant: {} {}".format(s1, s2))
print("Bisect: {} {}".format(b1, b2))


def dfunc(x):
    return 2 * x + 4 * cos(x)


newton = Newton(f, dfunc, x=2, max_iterations=50, eps=10e-7)
root = newton.calculate()

print("Newton: {}".format(root))
Exemplo n.º 18
0
 def setUp(self):
     self.newton = Newton(f='x**2 - 9', max_iter=1e6)
Exemplo n.º 19
0
from newton import Newton

# Ejercicio 1
newton = Newton(
    value_range=[0, 10],
    f=lambda l: l * (20 - 2 * l)**2,
    fp=lambda l: 12 * l**2 - 160 * l + 400,
    fpp=lambda l: 24 * l - 160,
)
newton.calculate([3])
Exemplo n.º 20
0
def run(step, point_type, method, function):

    # Validate function
    if function == "rosenbrock":
        n = 100
        f = Rosenbrock(n)
    elif function == "wood":
        n = 4
        f = Wood()
    elif function == "mnist":
        f = MNIST()
        n = f.get_size()
    else:
        print("\n Error, invalid function")
        quit()

    # Initial point
    if point_type == "const":
        if function == "rosenbrock":
            x0 = np.ones(n)
            x0[0] = -1.2
            x0[-2] = -1.2
        elif function == "mnist":
            x0 = np.ones(n)
        else:
            x0 = np.array([-3, -1, -3, -1])
    elif point_type == "rand":
        x0 = np.random.uniform(-1, 1, n)
    else:
        print("\n Error, invalid type of point")
        quit()

    mxitr = 50000  # Max number of iterations

    tol_g = 1e-8  # Tolerance for gradient

    tol_x = 1e-8  # Tolerance for x

    if function == "mnist":
        tol_f = 1e-3  # Tolerance for function
    else:
        tol_f = 1e-8  # Tolerance for function

    # Method for step update
    if step not in ["cubic", "barzilai", "zhang"]:
        print("\n Error, invalid step method")
        quit()

    # Estimate minimum point through optimization method chosen
    if method == "gd":
        alg = GD()
    elif method == "newton":
        alg = Newton()
    else:
        print("\n Error: Invalid optimization method: %s\n" % method)
        quit()

    xs = alg.iterate(x0, mxitr, tol_g, tol_x, tol_f, f, step, function)

    # Print point x found and function value f(x)
    # print("\nPoint x found: ", xs[-1])
    print("\nf(x) =  ", f.eval(xs[-1]))

    plt.plot(np.array(range(n)), xs[-1])
    plt.legend(['x*'], loc='best')
    plt.show()

    if function == "mnist":
        im = xs[-1][:-1].reshape(28, -1)
        plt.imshow(im, cmap=plt.cm.gray)
        plt.show()
Exemplo n.º 21
0
from fractal import Fractal
from julia import Julia
from mandelbrot import Mandelbrot
from newton import Newton
from pheonix import Pheonix

# Static constant fractal instances.
MANDELBROT = Mandelbrot("Mandelbrot Set")
NEWTON = Newton("Newton Fractal")
JULIA = Julia("Julia Set")
PHEONIX = Pheonix("Pheonix Fractal")


def getFractal(fractal_id):
    return Fractal.FRACTALS[fractal_id]


def getFractals():
    return Fractal.FRACTALS
Exemplo n.º 22
0
            print("Formalizing function by tensorflow...")

            sess = tf.Session()
            if args.methods[0] == "1":
                print("Option 1: Applying Bisection technique...")
                model = Bisection(sess, func, x_tensorflow,
                                  DEFAULT_SETTINGS["lbound"],
                                  DEFAULT_SETTINGS["rbound"],
                                  DEFAULT_SETTINGS["step"],
                                  DEFAULT_SETTINGS["floating_point"])
                model.find_valid_intervals()
                model.find_roots()
            elif args.methods[0] == "2":
                print("Option 2: Applying Newton's technique...")
                model = Newton(sess, func, x_tensorflow,
                               DEFAULT_SETTINGS["lbound"],
                               DEFAULT_SETTINGS["rbound"],
                               DEFAULT_SETTINGS["step"],
                               DEFAULT_SETTINGS["floating_point"],
                               DEFAULT_SETTINGS["iterator"])
                model.find_valid_intervals()
                model.find_roots()
            if len(model.roots) > 0:
                print("Approximately root of the function are:")
                for iter, i in enumerate(model.roots):
                    print("root %d:%f" % (iter, i))
            print()
            print("#" * 20)
            print("Press q to quit, or any button to start")
            key = input()