示例#1
0
文件: tests_1.py 项目: arunj18/psopy
def test_generic(args):
    """Test against the General function"""
    (tol,cons,sol,test_func,low,high,shape) = args
    #if shape == 0:
    #x0 = np.random.uniform(0, 2, (1000, 5))
        #print('here')
    x0 = init_feasible(cons, low=low, high=high, shape=shape)
    t0 = time.time()
    res = minimize_qpso(test_func, x0, tol=tol)
    t1= time.time()
    converged = res.success
    qpso_converged = 0
    qpso_nit = res.nit
    try:
        np.testing.assert_array_almost_equal(sol, res.x, 3)
    except:
        qpso_converged = 1
  #  if high is None:
    #x0 = np.random.uniform(0, 2, (1000, 5))
   # else:
    x0 = init_feasible(cons, low=low, high=high, shape=shape)
    t2= time.time()
    res = minimize(test_func,x0, tol=tol)
    t3 = time.time()
    converged = res.success
    pso_converged = 0
    pso_nit = res.nit
    assert converged, res.message
    try:
        np.testing.assert_array_almost_equal(sol, res.x, 3)
    except:
        pso_converged = 1
    
    return qpso_converged, qpso_nit ,t1-t0, pso_converged , pso_nit , t3-t2
示例#2
0
    def test_constrained(self):
        """Test against the following function::

            y = (x0 - 1)^2 + (x1 - 2.5)^2

        under the constraints::

             x0 - 2.x1 + 2 >= 0
            -x0 - 2.x1 + 6 >= 0
            -x0 + 2.x1 + 2 >= 0
                    x0, x1 >= 0

        """
        cons = ({'type': 'ineq', 'fun': lambda x:  x[0] - 2 * x[1] + 2},
                {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
                {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2},
                {'type': 'ineq', 'fun': lambda x: x[0]},
                {'type': 'ineq', 'fun': lambda x: x[1]})
        x0 = init_feasible(cons, low=0, high=2, shape=(1000, 2))
        options = {'stable_iter': 50}
        sol = np.array([1.4, 1.7])
        res = minimize_qpso(lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2, x0,
                       constraints=cons, options=options)
        converged = res.success
        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
示例#3
0
    def test_unconstrained(self):
        """Test against the Rosenbrock function."""

        x0 = np.random.uniform(0, 2, (1000, 5))
        sol = np.array([1., 1., 1., 1., 1.])
        res = minimize_qpso(rosen, x0)
        converged = res.success
        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
示例#4
0
    def test_rosendisc(self):
        """Test against the Rosenbrock function constrained to a disk."""

        cons = ({'type': 'ineq', 'fun': lambda x: -x[0]**2 - x[1]**2 + 2}, )
        x0 = init_feasible(cons, low=-1.5, high=1.5, shape=(1000, 2))
        sol = np.array([1., 1.])

        def rosen(x):
            return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2

        res = minimize_qpso(rosen, x0)
        converged = res.success

        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
示例#5
0
    def test_ackley(self):
        """Test against the Ackley function."""

        x0 = np.random.uniform(-5, 5, (1000, 2))
        sol = np.array([0., 0.])

        def ackley(x):
            return -20 * np.exp(-.2 * np.sqrt(0.5 * (x[0]**2 + x[1]**2))) - \
                np.exp(.5 * (np.cos(2*np.pi*x[0]) + np.cos(2*np.pi*x[1]))) + \
                np.e + 20

        res = minimize_qpso(ackley, x0)
        converged = res.success

        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
示例#6
0
    def test_levi(self):
        """Test against the Levi function."""

        x0 = np.random.uniform(-10, 10, (1000, 2))
        sol = np.array([1., 1.])

        def levi(x):
            sin3x = np.sin(3*np.pi*x[0]) ** 2
            sin2y = np.sin(2*np.pi*x[1]) ** 2
            return sin3x + (x[0] - 1)**2 * (1 + sin3x) + \
                (x[1] - 1)**2 * (1 + sin2y)

        res = minimize_qpso(levi, x0)
        converged = res.success

        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
示例#7
0
文件: tests_1.py 项目: arunj18/psopy
    fd = open('qpso_levy.csv','a')
    low = -1.5
    high=1.5
    shape = (1000,2)
    sol = np.array([1.,1.])

    
    print("Testing qpso with levy")
    print("rosen")
    fd = open('qpso_levy_rosen.csv','a')  
    wr = csv.writer(fd, dialect='excel')
    for i in range(30):
        print(i)
        cons = ({'type': 'ineq', 'fun': rosen},)
        x0 = init_feasible(cons, low=low, high=high, shape=shape)
        res = minimize_qpso(rosen, x0, options={'levy_rate':1})
        res_p = [res.fun, res.nit, res.nsit, res.status, res.success, res.x[0], res.x[1]]
        wr.writerow(res_p)
    fd.close()
    print("rosen_def")
    fd = open('qpso_levy_rosen_def.csv','a')  
    wr = csv.writer(fd, dialect='excel')
    for i in range(30):
        print(i)
        cons = ({'type': 'ineq', 'fun': rosen_def},)
        x0 = init_feasible(cons, low=low, high=high, shape=shape)
        res = minimize_qpso(rosen_def, x0, options={'levy_rate':1})
        res_p = [res.fun, res.nit, res.nsit, res.status, res.success, res.x[0], res.x[1]]
        wr.writerow(res_p)
    fd.close()
    print("mishra")