Exemplo n.º 1
0
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
Exemplo n.º 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)
Exemplo n.º 3
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(rosen, x0)
        converged = res.success

        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
Exemplo n.º 4
0
    def test_mishra(self):
        """Test against the Mishra Bird function."""

        cons = ({'type': 'ineq', 'fun': lambda x: 25 - np.sum((x + 5)**2)}, )
        x0 = init_feasible(cons, low=-10, high=0, shape=(1000, 2))
        sol = np.array([-3.130, -1.582])

        def mishra(x):
            cos = np.cos(x[0])
            sin = np.sin(x[1])
            return sin*np.e**((1 - cos)**2) + cos*np.e**((1 - sin)**2) + \
                (x[0] - x[1])**2

        res = minimize(mishra, x0)
        converged = res.success

        assert converged, res.message
        np.testing.assert_array_almost_equal(sol, res.x, 3)
chain_length = 4
chain_lenth_str = str(chain_length)
number_couplings = chain_length - 1
initial = create_basis_state(chain_length, 1)
final = create_basis_state(chain_length, chain_length).transpose()
basic_couplings = [1] * (chain_length - 1)
if number_couplings % 2 == 0:
    number_variable_couplings = int(number_couplings / 2)
if number_couplings % 2 == 1:
    number_variable_couplings = int((number_couplings + 1) / 2)
cons = []
for i in range(number_variable_couplings):
    cons.append({'type': 'ineq', 'fun': lambda x: x[i]})
cons = tuple(cons)
initial_couplings = init_feasible(cons,
                                  low=1.,
                                  high=5.,
                                  shape=(100, number_variable_couplings))
result = return_fidelities2(chain_length, initial, final, initial_couplings,
                            cons)
final_couplings = turn_symmetric_couplings(result.x, chain_length, initial,
                                           final)
normaliser = final_couplings[0]
final_couplings = np.divide(final_couplings, final_couplings[0])
print(final_couplings)
plot_fidelity_overtime2(chain_length, initial, final, basic_couplings,
                        (10 * chain_length) / np.average(initial_couplings),
                        'b', 'Unoptimised Couplings')
plot_fidelity_overtime2(chain_length, initial, final, final_couplings,
                        (10 * chain_length) / np.average(final_couplings), 'r',
                        'Optimised Couplings')
plt.legend(loc='best')
Exemplo n.º 6
0
    mp.freeze_support()
    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()