def test_2_targets_feasible(self, optimization_variables_avg):
     l, Q, A = optimization_variables_avg
     l2 = l[::-1]
     l = np.vstack([l, l2])
     m = 2e-3
     m1 = 4e-3
     f = [1e-2, 1e-2]
     x = optimization_methods.optimize_focality(l,
                                                Q,
                                                f,
                                                max_el_current=m,
                                                max_total_current=m1)
     x_sp = optimize_focality(l,
                              Q,
                              f,
                              max_el_current=m,
                              max_total_current=m1)
     assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
     assert np.all(np.abs(x) <= m + 1e-4)
     assert np.isclose(np.sum(x), 0)
     assert np.allclose(l.dot(x), f)
     assert np.allclose(x.dot(Q.dot(x)),
                        x_sp.dot(Q.dot(x_sp)),
                        rtol=1e-4,
                        atol=1e-5)
 def test_both_limit_angle_Q_iteration(self,
                                       optimization_variables_avg_QCQP):
     l, Q, A, Q_in = optimization_variables_avg_QCQP
     # l, Q, A = optimization_variables_avg
     # Q_in = 6 * np.eye(len(l)) + np.outer(l, l)
     max_angle = 20
     m = 2e-3
     m1 = 4e-3
     f = .01
     x = optimization_methods.optimize_focality(l,
                                                Q,
                                                f,
                                                max_el_current=m,
                                                max_total_current=m1,
                                                Qin=Q_in,
                                                max_angle=max_angle)
     x_sp = optimize_focality(l,
                              Q,
                              f,
                              max_el_current=m,
                              max_total_current=m1,
                              Qin=Q_in,
                              max_angle=max_angle)
     assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
     assert np.all(np.abs(x) <= m + 1e-4)
     assert np.isclose(np.sum(x), 0)
     assert np.isclose(l.dot(x), f)
     assert np.arccos(
         l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
     assert np.allclose(x.dot(Q.dot(x)),
                        x_sp.dot(Q.dot(x_sp)),
                        rtol=1e-4,
                        atol=1e-5)
    def test_both_constraints_feasible(self, optimization_variables_avg):
        l, Q, A = optimization_variables_avg
        m = 2e-3
        m1 = 4e-3
        f = 2e-2
        x = optimization_methods.optimize_focality(l,
                                                   Q,
                                                   f,
                                                   max_el_current=m,
                                                   max_total_current=m1)

        x_sp = optimize_focality(l,
                                 Q,
                                 f,
                                 max_el_current=m,
                                 max_total_current=m1)

        assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
        assert np.all(np.abs(x) <= m + 1e-4)
        assert np.isclose(np.sum(x), 0)
        assert np.isclose(l.dot(x), f)
        assert np.allclose(x.dot(Q.dot(x)),
                           x_sp.dot(Q.dot(x_sp)),
                           rtol=1e-4,
                           atol=1e-5)
 def test_bound_constraints_feasible(self, optimization_variables_avg):
     l, Q, A = optimization_variables_avg
     m = 2e-3
     f = 2e-2
     x = optimization_methods.optimize_focality(l, Q, f, max_el_current=m)
     x_sp = optimize_focality(l, Q, f, m)
     assert np.all(np.abs(x) <= m)
     assert np.isclose(np.sum(x), 0)
     assert np.isclose(l.dot(x), f)
     assert np.allclose(x.dot(Q.dot(x)),
                        x_sp.dot(Q.dot(x_sp)),
                        rtol=1e-4,
                        atol=1e-5)
    def test_limit_nr_electrodes_infeasible_comp(self,
                                                 optimization_variables_avg):
        l, Q, A = optimization_variables_avg
        m = 2e-3
        m1 = 4e-3
        f = 2e3
        n = 3
        x = optimization_methods.optimize_focality(l,
                                                   Q,
                                                   f,
                                                   max_el_current=m,
                                                   max_total_current=m1,
                                                   max_active_electrodes=n)

        assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
        assert np.linalg.norm(x, 0) <= n
        assert np.all(np.abs(x) <= m + 1e-4)
        assert np.isclose(np.sum(x), 0)
 def test_2_targets_limit_nr_electrodes(self, optimization_variables_avg):
     l, Q, A = optimization_variables_avg
     l2 = l[::-1]
     l = np.vstack([l, l2])
     m = 2e-3
     m1 = 4e-3
     f = np.array([1e-2, 1e-2])
     n = 3
     x = optimization_methods.optimize_focality(l,
                                                Q,
                                                f,
                                                max_el_current=m,
                                                max_total_current=m1,
                                                max_active_electrodes=n)
     assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
     assert np.linalg.norm(x, 0) <= n
     assert np.all(np.abs(x) <= m + 1e-4)
     assert np.isclose(np.sum(x), 0)
     assert np.allclose(l.dot(x), f)
    def test_both_limit_angle_infeasible_field(
            self, optimization_variables_avg_QCQP):
        l, Q, A, Q_in = optimization_variables_avg_QCQP
        max_angle = 15
        m = 2e-3
        m1 = 4e-3
        f = 2
        x = optimization_methods.optimize_focality(l,
                                                   Q,
                                                   f,
                                                   max_el_current=m,
                                                   max_total_current=m1,
                                                   Qin=Q_in,
                                                   max_angle=max_angle)

        assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
        assert np.all(np.abs(x) <= m + 1e-4)
        assert np.isclose(np.sum(x), 0)
        assert np.arccos(
            l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
    def test_both_limit_angle_reduce_target(self, optimization_variables_avg):
        l, Q, A = optimization_variables_avg
        Q_in = 5e1 * np.eye(len(l))
        max_angle = 20
        m = 2e-3
        m1 = 4e-3
        f = .02
        x = optimization_methods.optimize_focality(l,
                                                   Q,
                                                   f,
                                                   max_el_current=m,
                                                   max_total_current=m1,
                                                   Qin=Q_in,
                                                   max_angle=max_angle)

        assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
        assert np.all(np.abs(x) <= m + 1e-4)
        assert np.isclose(np.sum(x), 0)
        assert np.arccos(
            l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
    def test_limit_nr_angle_change_Q(seld, optimization_variables_avg_QCQP):
        l, Q, A, Q_in = optimization_variables_avg_QCQP
        max_angle = 15
        m = 2e-3
        m1 = 4e-3
        f = .01
        n = 4
        x = optimization_methods.optimize_focality(l,
                                                   Q,
                                                   f,
                                                   max_el_current=m,
                                                   max_total_current=m1,
                                                   Qin=Q_in,
                                                   max_angle=max_angle,
                                                   max_active_electrodes=n)

        assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
        assert np.all(np.abs(x) <= m + 1e-4)
        assert np.isclose(np.sum(x), 0)
        assert np.linalg.norm(x, 0) <= n
        assert np.arccos(
            l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)