Exemple #1
0
 def test_curvature(self) -> None:
     x = cp.Variable(3)
     expr = cp.length(x)
     self.assertEqual(expr.curvature, s.QUASICONVEX)
     expr = -cp.length(x)
     self.assertEqual(expr.curvature, s.QUASICONCAVE)
     expr = cp.ceil(x)
     self.assertEqual(expr.curvature, s.QUASILINEAR)
     self.assertTrue(expr.is_quasilinear())
Exemple #2
0
 def test_min(self) -> None:
     x = cp.Variable(2)
     expr = cp.min(cp.ceil(x))
     problem = cp.Problem(cp.Maximize(expr),
                          [x[0] >= 11.9, x[0] <= 15.8, x[1] >= 17.4])
     self.assertTrue(problem.is_dqcp())
     problem.solve(SOLVER, qcp=True)
     self.assertAlmostEqual(problem.objective.value, 16.0)
     self.assertLess(x[0].value, 16.0)
     self.assertGreater(x[0].value, 14.9)
     self.assertGreater(x[1].value, 17.3)
Exemple #3
0
    def test_flip_bounds(self) -> None:
        x = cp.Variable(pos=True)
        problem = cp.Problem(cp.Maximize(cp.ceil(x)), [x <= 1])
        problem.solve(SOLVER, qcp=True, low=0, high=0.5)
        self.assertGreater(x.value, 0)
        self.assertLessEqual(x.value, 1)

        problem.solve(SOLVER, qcp=True, low=0, high=None)
        self.assertGreater(x.value, 0)
        self.assertLessEqual(x.value, 1)

        problem.solve(SOLVER, qcp=True, low=None, high=0.5)
        self.assertGreater(x.value, 0)
        self.assertLessEqual(x.value, 1)
Exemple #4
0
    def test_tutorial_dqcp(self):
        # The sign of variables affects curvature analysis.
        x = cp.Variable(nonneg=True)
        concave_frac = x * cp.sqrt(x)
        constraint = [cp.ceil(x) <= 10]
        problem = cp.Problem(cp.Maximize(concave_frac), constraint)
        self.assertTrue(concave_frac.is_quasiconcave())
        self.assertTrue(constraint[0].is_dqcp())
        self.assertTrue(problem.is_dqcp())

        w = cp.Variable()
        fn = w * cp.sqrt(w)
        problem = cp.Problem(cp.Maximize(fn))
        self.assertFalse(fn.is_dqcp())
        self.assertFalse(problem.is_dqcp())
Exemple #5
0
    def test_basic_maximization_with_interval(self):
        x = cp.Variable()
        expr = cp.ceil(x)

        self.assertTrue(expr.is_dqcp())
        self.assertTrue(expr.is_quasiconvex())
        self.assertTrue(expr.is_quasiconcave())
        self.assertFalse(expr.is_convex())
        self.assertFalse(expr.is_concave())
        self.assertFalse(expr.is_dcp())
        self.assertFalse(expr.is_dgp())

        problem = cp.Problem(cp.Maximize(expr), [x >= 12, x <= 17])
        self.assertTrue(problem.is_dqcp())
        self.assertFalse(problem.is_dcp())
        self.assertFalse(problem.is_dgp())

        problem.solve(SOLVER, qcp=True)
        self.assertAlmostEqual(x.value, 17.0, places=3)
Exemple #6
0
    def test_basic_solve(self):
        x = cp.Variable()
        expr = cp.ceil(x)

        self.assertTrue(expr.is_dqcp())
        self.assertTrue(expr.is_quasiconvex())
        self.assertTrue(expr.is_quasiconcave())
        self.assertFalse(expr.is_convex())
        self.assertFalse(expr.is_concave())
        self.assertFalse(expr.is_dcp())
        self.assertFalse(expr.is_dgp())

        problem = cp.Problem(cp.Minimize(expr), [x >= 12, x <= 17])
        self.assertTrue(problem.is_dqcp())
        self.assertFalse(problem.is_dcp())
        self.assertFalse(problem.is_dgp())
        problem.solve(SOLVER, qcp=True, low=12, high=17)
        self.assertAlmostEqual(problem.value, 12.0, places=3)
        self.assertAlmostEqual(x.value, 12.0, places=3)

        problem._clear_solution()
        problem.solve(SOLVER, qcp=True)
        self.assertAlmostEqual(problem.value, 12.0, places=3)
        self.assertAlmostEqual(x.value, 12.0, places=3)

        problem._clear_solution()
        problem.solve(SOLVER, qcp=True, high=17)
        self.assertAlmostEqual(problem.value, 12.0, places=3)
        self.assertAlmostEqual(x.value, 12.0, places=3)

        problem._clear_solution()
        problem.solve(SOLVER, qcp=True, low=12)
        self.assertAlmostEqual(problem.value, 12.0, places=3)
        self.assertAlmostEqual(x.value, 12.0, places=3)

        problem._clear_solution()
        problem.solve(SOLVER, qcp=True, low=0, high=100)
        self.assertAlmostEqual(problem.value, 12.0, places=3)
        self.assertAlmostEqual(x.value, 12.0, places=3)
Exemple #7
0
    def test_basic_composition(self):
        x, y = cp.Variable(2)
        expr = cp.maximum(cp.ceil(cp.ceil(x)), cp.ceil(cp.ceil(y)))

        problem = cp.Problem(cp.Minimize(expr), [x >= 12, x <= 17, y >= 17.4])
        self.assertTrue(problem.is_dqcp())
        problem.solve(SOLVER, qcp=True)
        self.assertEqual(problem.objective.value, 18.0)
        self.assertLess(x.value, 17.1)
        self.assertGreater(x.value, 11.9)
        self.assertGreater(y.value, 17.3)

        # This problem should have the same solution.
        expr = cp.maximum(cp.floor(cp.ceil(x)), cp.floor(cp.ceil(y)))
        problem = cp.Problem(cp.Minimize(expr), [x >= 12, x <= 17, y >= 17.4])
        self.assertTrue(problem.is_dqcp())
        problem.solve(SOLVER, qcp=True)
        self.assertEqual(problem.objective.value, 18.0)
        self.assertLess(x.value, 17.1)
        self.assertGreater(x.value, 11.9)
        self.assertGreater(y.value, 17.3)
Exemple #8
0
 def test_noop_logistic_constr(self):
     x = cp.Variable(nonneg=True)
     constr = [cp.logistic(cp.ceil(x)) >= -5]
     problem = cp.Problem(cp.Minimize(0), constr)
     problem.solve(SOLVER, qcp=True)
     self.assertEqual(problem.status, s.OPTIMAL)
Exemple #9
0
 def test_noop_inv_pos_constr(self):
     x = cp.Variable()
     constr = [cp.inv_pos(cp.ceil(x)) >= -5]
     problem = cp.Problem(cp.Minimize(0), constr)
     problem.solve(SOLVER, qcp=True)
     self.assertEqual(problem.status, s.OPTIMAL)
Exemple #10
0
 def test_infeasible_logistic_constr(self):
     x = cp.Variable(nonneg=True)
     constr = [cp.logistic(cp.ceil(x)) <= -5]
     problem = cp.Problem(cp.Minimize(0), constr)
     problem.solve(SOLVER, qcp=True)
     self.assertEqual(problem.status, s.INFEASIBLE)
Exemple #11
0
 def test_infeasible(self):
     x = cp.Variable(2)
     problem = cp.Problem(cp.Minimize(cp.length(x)),
                          [x == -1, cp.ceil(x) >= 1])
     problem.solve(SOLVER, qcp=True)
     self.assertIn(problem.status, (s.INFEASIBLE, s.INFEASIBLE_INACCURATE))
Exemple #12
0
def solve_problem_func(env, alpha_parameter):
    # ----------parameters----------
    I = env["I"]
    M = env["M"]
    h_ul = env["h_ul"]
    bandwidth_max = env["bandwidth_max"]
    tx_power_m_max = env["tx_power_m_max"]
    comp_bs = env["comp_bs"]
    comp_m = env["comp_m"]
    task = env["task"]
    data = env["data"]
    M_list = env["M_list"]
    time_scale = env["time_scale"]
    rate_m_itr = bandwidth_max * np.log2(1 + tx_power_m_max * h_ul)
    average_rate = bandwidth_max * np.log2(1 + tx_power_m_max * h_ul) / M
    alpha_v = alpha_parameter

    # ----------itration variable parameter----------
    # this part is defined to use the iteration variable in the subproblem

    # ----------variables----------
    # define the variables
    # bandwidth allocation factor w
    omega = cp.Variable([M, I])
    # offloading factor alpha
    alpha = cp.Variable([M, I])
    # bs sever allocation factor beta
    beta = cp.Variable([M, I])
    eta = cp.Variable([M, I])
    mu = cp.Variable([M, I])

    # max delay T
    T = cp.Variable()

    # additional variables
    t1 = cp.Variable()
    t2 = cp.Variable()
    t3 = cp.Variable()

    # ----------problem formulation----------

    # --------objective function--------
    objective_func = T

    objective1 = cp.Minimize(objective_func)

    # --------constraints--------
    # offloading constraints
    c1 = [alpha >= 0, alpha <= 1]
    # bandwidth allocation constraints
    c4_5 = [cp.sum(omega) <= 1, omega >= (1e-7)]
    # bs server allocation constraints
    c2_3 = [cp.sum(beta) <= 1, beta >= (1e-7)]

    c6 = [cp.multiply(1 - alpha, task) / comp_m * time_scale <= t1]
    c7b = [
        -cp.log(t3) - cp.log(beta) + np.log2(alpha_v) +
        cp.multiply(1 / alpha_v / np.log(2),
                    (alpha - alpha_v)) + np.log2(task / comp_bs * time_scale)
        <= 0
    ]
    c8b = [
        -cp.log(t2) - cp.log(omega) + np.log2(alpha_v) + cp.multiply(
            1 / alpha_v / np.log(2),
            (alpha - alpha_v)) + np.log2(data / rate_m_itr * time_scale) <= 0
    ]
    #c8 = [cp.multiply(eta,task/comp_bs)*time_scale<=t3]
    #c7 = [cp.multiply ( mu, data / rate_m_itr)*time_scale<=t2]

    R1a2 = []
    for i in range(0, M):
        for j in range(0, I):

            R1a2 = R1a2 + [
                cp.norm(cp.vstack([2 * (alpha[i, j]), eta[i, j] - beta[i, j]]),
                        2) <= eta[i, j] + beta[i, j]
            ]
            #assert  R1a2[0].is_dqcp()

    R1a3 = [alpha <= 1e7 * beta + (1e-7) * eta - 1, alpha <= eta]

    R2a3 = [alpha <= 1e7 * omega + (1e-7) * mu - 1, alpha <= mu]

    R2a4 = [eta <= 1e7, mu <= 1e7]

    # local computing constraints R1
    #R1 = [cp.ceil(cp.multiply(alpha, task)/comp_m) <= T]
    R1 = [cp.ceil(cp.multiply(1 - alpha, task) / comp_m) - T <= 0]
    #R1 = [cp.ceil(alpha) - 10 <= 0]
    #assert R1[0].is_dqcp ()
    # offloading constraints R2
    R2a = cp.ceil(
        cp.multiply(cp.multiply(alpha, task / comp_bs), cp.inv_pos(beta)))
    R2b = cp.ceil(
        cp.multiply(
            cp.multiply(
                alpha,
                data / bandwidth_max * np.log2(1 + tx_power_m_max * h_ul)),
            cp.inv_pos(omega)))
    R2 = [R2a + R2b <= T]
    R2a1 = cp.ceil((cp.multiply(eta, task / comp_bs)))

    objective2 = cp.Minimize(
        1 / 2 * cp.max(cp.ceil(cp.multiply(alpha, task) / comp_m)))
    objective2 = cp.Minimize(
        cp.max(cp.ceil(cp.multiply(1 - alpha, task / beta / comp_bs))))

    obj3_part1 = 1 / 2 * cp.max(cp.ceil(cp.multiply(1 - alpha, task / comp_m)))
    obj3_part1 = 1 / 2 * cp.ceil(cp.max(cp.multiply(1 - alpha, task / comp_m)))
    obj3_part2 = 1 / 2 * cp.max(
        cp.ceil(cp.multiply(eta, task / comp_bs)) +
        cp.ceil(cp.multiply(mu, data / rate_m_itr)))
    obj3_part2 = 1 / 2 * cp.ceil(cp.max(cp.multiply(
        eta, task / comp_bs))) + 1 / 2 * cp.ceil(
            cp.max(cp.multiply(mu, data / rate_m_itr)))

    obj3_part3 = cp.abs(obj3_part1 - obj3_part2)
    objective3 = cp.Minimize(obj3_part1 + obj3_part2 + obj3_part3)

    #assert obj3_part1.is_dqcp ()
    #assert obj3_part1.is_dcp()
    #assert (cp.max(cp.ceil(cp.multiply(eta,task/comp_bs)))).is_dqcp()
    #assert (cp.max(cp.ceil(cp.multiply(eta,task/comp_bs)))).is_dcp()
    #assert (cp.ceil(cp.multiply(mu,data/rate_m_itr))).is_dqcp
    #assert (cp.ceil(alpha)+cp.ceil(beta)).is_dqcp()
    #assert obj3_part2.is_dqcp ()
    #assert obj3_part3.is_dqcp ()
    #assert objective3.is_dqcp ()

    t = cp.Variable([M, I])
    objective2 = cp.Minimize(cp.max(cp.ceil(cp.multiply(t, task / comp_bs))))
    c4 = [(1 - alpha) / beta <= t]

    objective6 = cp.Minimize(1 / 2 *
                             cp.ceil(t1 + t2 + t3 + cp.abs(t1 - t2 - t3)) +
                             3 / 2)

    rho = 1
    upsilon = 1
    varsigma = 1e-27
    obj7_func1 = 1 / 2 * cp.ceil(t1 + t2 + t3 + cp.abs(t1 - t2 - t3)) + 3 / 2
    obj7_func1 = 1 / 2 * (t1 + t2 + t3 + cp.abs(t1 - t2 - t3))
    obj7_func2 = cp.sum(
        cp.multiply(1 - alpha, varsigma * task * np.square(comp_m))) + cp.sum(
            tx_power_m_max * cp.multiply(mu, data / rate_m_itr))
    objective7 = cp.Minimize(rho * obj7_func1 + upsilon * obj7_func2)

    try:
        # ----------probalem solve and results----------
        # problem6 = cp.Problem(objective6, c1+c2_3+c4_5+c6+c7+c8+R1a3+R2a3+R2a4)
        problem6 = cp.Problem(objective6, c1 + c2_3 + c4_5 + c6 + c7b + c8b)
        problem6.solve(qcp=True, verbose=True, solver=cp.ECOS)
        print()
        print("problem1 solve: ", problem6.value)
        print("alpha.value", alpha.value)
        print("beta.value", beta.value)
        print("omega.value", omega.value)

        np.ceil(data / rate_m_itr * M * time_scale)
        # ----------data collection and depict-----------
        plt.clf()
        plt.subplot(441)
        plt.title("user_rate in M/s")
        # plt.bar(x=M_list, height=(average_rate/1e6).reshape(M), width=1)
        plt.plot(M_list,
                 omega.value * rate_m_itr / 1e6,
                 '-*',
                 color='b',
                 label="optimized rate")
        plt.plot(M_list,
                 average_rate / 1e6,
                 '-o',
                 color='r',
                 label="average bandwidth allocation  rate")

        plt.legend()

        plt.subplot(442)
        plt.title("data transmitting delay")
        plt.plot(
            M_list,
            data / average_rate * time_scale,
            '-*',
            color='b',
            label=
            "full date transmitting delay with average bandwidth allocation")
        plt.plot(M_list, (alpha.value * data) / (omega.value * rate_m_itr) *
                 time_scale,
                 '-o',
                 color='r',
                 label="optimized transmitting delay")
        # plt.bar(x=M_list, height=np.ceil(data / average_rate*time_scale).reshape(M), width=1)
        plt.legend(fontsize='xx-small')

        plt.subplot(443)
        plt.title("local_computing_delay")
        bar_width = 0.3
        plt.bar(x=M_list,
                height=np.ceil(time_scale * task / comp_m).reshape(M),
                width=bar_width,
                label='full local computing delay')
        plt.bar(x=M_list + bar_width,
                height=np.ceil(time_scale * (1 - alpha.value) * task /
                               comp_m).reshape(M),
                width=bar_width,
                label='remain local computing delay')

        plt.plot(M_list,
                 problem6.value * np.ones(M),
                 'o',
                 color='m',
                 label="optimized delay")
        plt.legend(fontsize='xx-small')  # 显示图例,即label
        plt.xticks(x=M_list +
                   bar_width / 2)  # 显示x坐标轴的标签,即tick_label,调整位置,使其落在两个直方图中间位置

        plt.subplot(444)
        plt.title("offloading_computing_delay")

        bar_width = 0.3  # 设置柱状图的宽度
        plt.bar(x=M_list,
                height=(np.ceil(task / (comp_bs / M)) * time_scale).reshape(M),
                width=bar_width,
                label='average full offloading computing delay')
        plt.bar(x=M_list + bar_width,
                height=(np.ceil(task * alpha.value / (beta.value * comp_bs) *
                                time_scale)).reshape(M),
                width=bar_width,
                label='optimized offloading computing delay')

        # 绘制并列柱状图

        plt.legend()  # 显示图例,即label
        plt.xticks(x=M_list +
                   bar_width / 2)  # 显示x坐标轴的标签,即tick_label,调整位置,使其落在两个直方图中间位置

        plt.subplot(445)
        plt.title("optimized local computing_delay gain")
        plt.plot(M_list, (problem6.value -
                          np.ceil(task / comp_m * time_scale)).reshape(M),
                 'x',
                 color='r')
        plt.plot(M_list,
                 problem6.value * np.ones(M),
                 'o',
                 color='m',
                 label="optimized delay")
        plt.plot(M_list,
                 t1.value * np.ones(M),
                 'v',
                 color='g',
                 label="local computing delay t1")
        plt.plot(M_list,
                 t3.value * np.ones(M),
                 '^',
                 color='k',
                 label="offloading computing delay t3")
        plt.plot(M_list,
                 t2.value * np.ones(M),
                 '*',
                 color='b',
                 label="transmitting delay t2")
        plt.legend(fontsize='xx-small')

        plt.subplot(446)
        plt.title("optimized alpha beta")
        plt.plot(M_list, alpha.value, '-v', label='alpha')
        plt.plot(M_list, beta.value, '-x', label='beta')
        plt.plot(M_list, omega.value, '^', label='omega')
        plt.legend()

        plt.subplot(447)
        plt.title("optimized omega")
        plt.plot(M_list, omega.value, '-^')

        plt.subplot(448)
        plt.title("optimized beta")
        plt.plot(M_list, beta.value, '-^')
        '''
        plt.subplot(449)
        plt.title("optimized eta")
        plt.plot(M_list, eta.value,'-^')

        plt.subplot(4,4,10)
        plt.title("optimized mu")
        plt.plot(M_list, mu.value,'-^')

        plt.subplot(4,4,11)
        plt.title("recalculated omega")
        plt.plot(M_list, alpha.value/eta.value,'-^')

        plt.subplot(4,4,12)
        plt.title("recalculated beta")
        plt.plot(M_list, alpha.value/mu.value,'-^')
        '''

        print("func1.value", obj7_func1.value)
        print(
            "local computing energy",
            cp.sum(cp.multiply(1 - alpha,
                               varsigma * task * np.square(comp_m))).value)
        print(
            "offloading energy",
            cp.sum(tx_power_m_max * cp.multiply(mu, data / rate_m_itr)).value)
        print("func2.value", obj7_func2.value)

        problem_result = {
            "problem6.value": problem6.value,
            "alpha.value": alpha.value
        }

        return problem_result

    except SolverError:
        problem6.value = 0

        problem_result = {
            "problem6.value": problem6.value,
            "alpha.value": alpha_v
        }

        return problem_result

        pass
Exemple #13
0
 def test_infeasible_exp_constr(self):
     x = cp.Variable()
     constr = [cp.exp(cp.ceil(x)) <= -5]
     problem = cp.Problem(cp.Minimize(0), constr)
     problem.solve(qcp=True)
     self.assertEqual(problem.status, s.INFEASIBLE)
Exemple #14
0
import cvxpy as cp
import numpy as np

import mosek, sys

env = mosek.Env()
env.set_Stream(mosek.streamtype.log, lambda x: sys.stdout.write(x))
env.echointro(1)

x = cp.Variable ()

y = cp.Variable ()

t = cp.Variable(pos=True)

objective_fn = cp.ceil ( x + y + cp.abs(x-y))

objective = cp.Minimize ( objective_fn+1 )

#constraint = cp.ceil ( x + y) <= t
print((cp.ceil ( x + y + cp.abs(x-y))+1).curvature)

problem = cp.Problem ( objective, [x<=2,y<=3,x>=0.5,y>=0.5] )

problem.solve ( qcp=True )

print ( "Optimal value: ", problem.value )
print ( "x: ", x.value )
print ( "y: ", y.value )

# Generate a random problem