def test_robust_regression(self):
        #
        # Compare cvxpy solutions to cvxopt ground truth
        #
        from cvxpy import (matrix, variable, program, minimize,
                           huber, sum, leq, geq, square, norm2,
                           ones, zeros, quad_form)
        tol_exp = 5 # Check solution to within 5 decimal places
        m, n = self.m, self.n
        A = matrix(self.A)
        b = matrix(self.b)

        # Method 1: using huber directly
        x = variable(n)
        p = program(minimize(sum(huber(A*x - b, 1.0))))
        p.solve(True)

        np.testing.assert_array_almost_equal(
            x.value, self.xh, tol_exp)

        # Method 2: solving the dual QP
        x = variable(n)
        u = variable(m)
        v = variable(m)
        p = program(minimize(0.5*quad_form(u, 1.0) + sum(v)),
                    [geq(u, 0.0), leq(u, 1.0), geq(v, 0.0),
                     leq(A*x - b, u + v), geq(A*x - b, -u - v)])
        p.solve(True)
        np.testing.assert_array_almost_equal(
            x.value, self.xh, tol_exp)
Example #2
0
def create(m, n):
    np.random.seed(0)

    x0 = np.random.randn(n)
    A = np.random.randn(m,n)
    A = A*sp.diags([1 / np.sqrt(np.sum(A**2, 0))], [0])
    b = A.dot(x0) + np.sqrt(0.01)*np.random.randn(m)
    b = b + 10*np.asarray(sp.rand(m, 1, 0.05).todense()).ravel()

    x = cp.Variable(n)
    return cp.Problem(cp.Minimize(cp.sum_entries(cp.huber(A*x - b))))
Example #3
0
### Part (b) ###
objective2 = cp.Minimize(cp.sum_squares(Y - X * beta - gamma * np.ones(n)))
constrains2 = [beta >= 0]
prob2 = cp.Problem(objective2, constrains2)
print('Part (b):')
print('Optimal value:', prob2.solve())
print('Optimal beta:', beta.value)
print('Optimal gamma:', gamma.value)
print()

### Part (c) ###
objective3 = cp.Minimize(
    cp.sum_squares(Y - X * beta - gamma * np.ones(n)) + lam * cp.norm(beta, 1))
prob3 = cp.Problem(objective3)
print('Part (c):')
print('Optimal value:', prob3.solve())
print('Optimal beta:', beta.value)
print('Optimal gamma:', gamma.value)
print()

### Part (d) ###
objective4 = cp.Minimize(
    cp.sum(cp.huber(Y - X * beta - gamma * np.ones(n), 0.5)))
prob4 = cp.Problem(objective4)
print('Part (d):')
print('Optimal value:', prob4.solve())
print('Optimal beta:', beta.value)
print('Optimal gamma:', gamma.value)
print()
Example #4
0
    def test_huber(self):
        # Valid.
        cp.huber(self.x, 1)

        with self.assertRaises(Exception) as cm:
            cp.huber(self.x, -1)
        self.assertEqual(str(cm.exception),
                         "M must be a non-negative scalar constant.")

        with self.assertRaises(Exception) as cm:
            cp.huber(self.x, [1, 1])
        self.assertEqual(str(cm.exception),
                         "M must be a non-negative scalar constant.")

        # M parameter.
        M = Parameter(nonneg=True)
        # Valid.
        cp.huber(self.x, M)
        M.value = 1
        self.assertAlmostEqual(cp.huber(2, M).value, 3)
        # Invalid.
        M = Parameter(nonpos=True)
        with self.assertRaises(Exception) as cm:
            cp.huber(self.x, M)
        self.assertEqual(str(cm.exception),
                         "M must be a non-negative scalar constant.")

        # Test copy with args=None
        atom = cp.huber(self.x, 2)
        copy = atom.copy()
        self.assertTrue(type(copy) is type(atom))
        # A new object is constructed, so copy.args == atom.args but copy.args
        # is not atom.args.
        self.assertEqual(copy.args, atom.args)
        self.assertFalse(copy.args is atom.args)
        # As get_data() returns a Constant, we have to check the value
        self.assertEqual(copy.get_data()[0].value, atom.get_data()[0].value)
        # Test copy with new args
        copy = atom.copy(args=[self.y])
        self.assertTrue(type(copy) is type(atom))
        self.assertTrue(copy.args[0] is self.y)
        self.assertEqual(copy.get_data()[0].value, atom.get_data()[0].value)
Example #5
0
 def loss(self, A, U): return cp.sum_entries(cp.huber(cp.Constant(A) - U, self.a))
 def __str__(self): return "huber loss"
Example #6
0
 def loss(self, A, U):
     return cp.sum_entries(cp.huber(cp.Constant(A) - U, self.a))
Example #7
0
  Constant([[5, 2], [3, 1]])),
 (lambda x: cp.cumsum(x, axis=1), (2, 2), [[[-5, 2], [-3, 1]]],
  Constant([[-5, 2], [-8, 3]])),
 (lambda x: cp.cumsum(x, axis=0), (2, 2), [[[-5, 2], [-3, 1]]],
  Constant([[-5, -3], [-3, -2]])),
 (lambda x: cp.cummax(x, axis=1), (2, 2), [[[-5, 2], [-3, 1]]],
  Constant([[-5, 2], [-3, 2]])),
 (lambda x: cp.cummax(x, axis=0), (2, 2), [[[-5, 2], [-3, 1]]],
  Constant([[-5, 2], [-3, 1]])),
 (cp.diag, (2,), [[[-5, 2], [-3, 1]]], Constant([-5, 1])),
 (cp.diag, (2, 2), [[-5, 1]], Constant([[-5, 0], [0, 1]])),
 (cp.exp, (2, 2), [[[1, 0], [2, -1]]],
  Constant([[math.e, 1], [math.e**2, 1.0 / math.e]])),
 (cp.huber, (2, 2), [[[0.5, -1.5], [4, 0]]],
  Constant([[0.25, 2], [7, 0]])),
 (lambda x: cp.huber(x, 2.5), (2, 2), [[[0.5, -1.5], [4, 0]]],
  Constant([[0.25, 2.25], [13.75, 0]])),
 (cp.inv_pos, (2, 2), [[[1, 2], [3, 4]]],
  Constant([[1, 1.0 / 2], [1.0 / 3, 1.0 / 4]])),
 (lambda x: (x + Constant(0))**-1, (2, 2), [[[1, 2], [3, 4]]],
  Constant([[1, 1.0 / 2], [1.0 / 3, 1.0 / 4]])),
 (cp.kl_div, tuple(), [math.e, 1], Constant([1])),
 (cp.kl_div, tuple(), [math.e, math.e], Constant([0])),
 (cp.kl_div, (2,), [[math.e, 1], 1], Constant([1, 0])),
 (cp.rel_entr, tuple(), [math.e, 1], Constant([math.e])),
 (cp.rel_entr, tuple(), [math.e, math.e], Constant([0])),
 (cp.rel_entr, (2,), [[math.e, 1], 1], Constant([math.e, 0])),
 (lambda x: cp.kron(np.array([[1, 2], [3, 4]]), x), (4, 4), [np.array([[5, 6], [7, 8]])],
  Constant(np.kron(np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])))),
 (cp.lambda_max, tuple(), [[[2, 0], [0, 1]]], Constant([2])),
 (cp.lambda_max, tuple(), [[[2, 0, 0], [0, 3, 0], [0, 0, 1]]], Constant([3])),
Example #8
0
 def loss(self, A, U): return cp.sum(cp.huber(cp.Constant(A) - U, self.a))
 def __str__(self): return "huber loss"
Example #9
0
def Classo_R2(pb, lam, compute=True):

    pb_type = pb.type  # can be 'Path-Alg', 'P-PDS' , 'PF-PDS' or 'DR'

    (m, d, k), (A, C, y) = pb.dim, pb.matrix
    lamb, rho = lam * pb.lambdamax, pb.rho

    #ODE
    # here we compute the path algo until our lambda, and just take the last beta

    if (pb_type == 'Path-Alg'):
        beta = solve_path((A, C, y), lam, False, rho, 'huber')[0]
        return (beta[-1])

    # 2 prox :
    regpath = pb.regpath
    r = lamb / (2 * rho)
    if (pb_type == 'DR'):
        Ahuber = np.concatenate((A, r * np.eye(len(A))), axis=1)
        Chuber = np.concatenate((C, np.zeros((len(C), len(y)))), axis=1)
        matrices_huber = (Ahuber, Chuber, y)
        prob = problem_R1(matrices_huber, 'DR')
        prob.regpath = regpath
        if (len(pb.init) == 3): prob.init = pb.init
        if not (regpath): return (Classo_R1(prob, lamb / prob.lambdamax)[:d])
        x, warm_start = Classo_R1(prob, lamb / prob.lambdamax)
        return (x[:d], warm_start)

    tol = pb.tol * LA.norm(y) / LA.norm(A, 'fro')  # tolerance rescaled

    #cvx
    # call to the cvx function of minimization
    if (pb_type == 'cvx'):
        import cvxpy as cp
        x = cp.Variable(d)
        objective, constraints = cp.Minimize(
            cp.sum(cp.huber(A * x - y, rho)) +
            lamb * cp.norm(x, 1)), [C * x == 0]
        prob = cp.Problem(objective, constraints)
        result = prob.solve(warm_start=regpath, eps_abs=tol)
        if (regpath): return (x.value, True)
        return (x.value)

    if (compute): pb.compute_param()
    tau, Proj, AtA, Aty = pb.tauN, proj_c(C, d), pb.AtA, pb.Aty
    gamma = pb.gam / (2 * (pb.AtAnorm + r**2))
    t = lamb * gamma
    w, tm, zerom, zerod = t * pb.weights, t * np.ones(m), np.zeros(
        m), np.zeros(d)
    o, xbar, x, v = pb.init
    # vectors usefull to compute the prox of f(b)= sum(wi |bi|)

    #FORWARD BACKWARD
    if (pb_type == 'P-PDS'):

        for i in range(pb.N):
            grad = (AtA.dot(x) - Aty)
            v = v + tau * C.dot(xbar)
            S = x - 2 * gamma * grad - 2 * gamma * r * (A.T).dot(o) - (
                C.T).dot(v)
            o = prox(
                o * (1 - 2 * gamma * r**2) + 2 * gamma * r * (y - A.dot(x)),
                tm, zerom)
            p = prox(S, w, zerod)
            nw_x = Proj.dot(p)
            eps = nw_x - x
            xbar = p + eps

            if (i % 10 == 2 and LA.norm(eps) < tol):  # 0.6
                if (regpath): return (x, (o, xbar, x, v))
                else: return (x)
            x = nw_x
            if (LA.norm(x) > 1e10):
                print('DIVERGENCE')
                return (x)
        print('NO CONVERGENCE')
        return (x)

    # NO PROJ

    if (pb_type == 'PF-PDS'):  # y1 --> S ; p1 --> p . ; p2 --> y2
        for i in range(pb.N):
            grad = (AtA.dot(x) - Aty)

            S1 = x - 2 * gamma * grad - 2 * gamma * r * (A.T).dot(o) - (
                C.T).dot(v)
            S2 = o * (1 - 2 * gamma * r**2) + 2 * gamma * r * (y - A.dot(x))

            p1 = prox(S1, w, zerod)
            p2 = prox(S2, tm, zerom)

            v = v + tau * C.dot(p1)
            v2 = v + tau * C.dot(x)

            eps1 = p1 + 2 * gamma * (Aty - AtA.dot(p1) -
                                     r * A.T.dot(o)) - C.T.dot(v2) - S1
            eps2 = p2 + 2 * r * gamma * (y - r * p2 - A.dot(x)) - S2

            x = x + eps1
            o = o + eps2

            if (i > 0 and LA.norm(eps1) + LA.norm(eps2) < tol):
                if (regpath): return (x, (o, xbar, x, v))
                else: return (x)

            if (LA.norm(x) + LA.norm(o) + LA.norm(v) > 1e6):
                print('DIVERGENCE')
                return (x)
        print('NO CONVERGENCE')
        return (x)
             linf(np_x[2 * k + 2, :], np_u[k + 1, :], x[2 * k + 2, :],
                  u[k + 1, :]))
        ]
        const += [
            x[2 * k + 2, :] - x[2 * k, :] ==
            (linf(np_x[2 * k, :], np_u[k, :], x[2 * k, :], u[k, :]) +
             4 * linf(np_x[2 * k + 1, :],
                      (np_u[k, :] + np_u[k + 1, :]) / 2, x[2 * k + 1],
                      (u[k, :] + u[k + 1, :]) / 2) +
             linf(np_x[2 * k + 2, :], np_u[k + 1, :], x[2 * k + 2], u[k, :])) *
            dt / 6
        ]

        #const += [x[k+1,:] - x[k,:] ==   0.5*dt*(linf(np_x[k+1,:],np_u[k+1,:],x[k+1,:],u[k+1,:])+linf(np_x[k,:],np_u[k,:],x[k,:],u[k,:]))]

        cost = cost + 0.5 * cvx.huber(
            x[k, 0] - np.pi, M=0.5) + 0.01 * cvx.huber(
                u[k, :]) + 0.5 * cvx.huber(x[k, 1] - 0, M=0.25)

    cost = cost + 100 * cvx.square(x[K, 0] -
                                   np.pi) + 100 * cvx.square(x[K, 1] - 0)
    objective = cvx.Minimize(cost)
    print("Iteration", j + 1)
    prob = cvx.Problem(objective, const)
    sol = prob.solve(verbose=True)
    np_x = x.value
    np_u = u.value
    #print((np_x))
#    print(np.shape(np_u))

plt.plot(np_x[:, 0])
plt.plot(np_x[:, 1])
Example #11
0
new_pos = new_pos.astype(int)
# true permutation matrix
P=np.identity(m)

P[perm_idxs]=P[new_pos,:]
true_perm=[]
for i in range(k):
    if perm_idxs[i] != new_pos[i]:
        true_perm = np.append(true_perm, perm_idxs[i])
y=P.dot(y_true)
new_pos = None
# naive estimator (P=I)
x_naive = np.linalg.lstsq(A,y)[0]
# robust estimator
x_hub = cp.Variable(n)
obj = cp.sum(cp.huber(A@x_hub-y))  #TODO dimention bugs
cp.Problem(cp.Minimize(obj)).solve()
plt.figure(1)
plt.plot(np.arange(m), np.abs(A.dot(x_hub.value)-y), '.')
plt.ylabel('residual')
plt.xlabel('idx')
plt.savefig('prob_152.png')

# remove k largest residuals
cand_idxs = np.zeros(m)
cand_idxs[:] = np.flip(np.argsort(np.abs(A.dot(x_hub.value)-y)))
cand_idxs = np.sort(cand_idxs[:k])
cand_idxs = cand_idxs.astype(int)
keep_idxs = np.zeros(m)
keep_idxs[:] = np.argsort(np.abs(A.dot(x_hub.value)-y).T)
keep_idxs = np.sort(keep_idxs[:(m-k)])
Example #12
0
    def main_optimize(elec_storage, battery_depth, thermal_storage, CHP_Runtime, CHP_Downtime, capacities_conv,
                      capacities_stor, tech_details, storage_details, list_techs, list_storage, time_now):


        # Original boundary condition
        hub_el = 'elec_' + str(hub)
        hub_ht = 'heat_' + str(hub)

        P_Demand = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), hub_el].values.tolist()
        Q_Demand = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), hub_ht].values.tolist()
        Ta = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), 'temp'].values.tolist()
        I_Solar = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), 'solar_roof'].values.tolist()

        Rini_CHP = CHP_Runtime
        Dini_CHP = CHP_Downtime

        power_list = []
        heat_list = []

        cost = 0
        constr = []

        Qmax_GSHP = 0

        comp_list = []
        it = 0

        for item in list_techs:

            if item == 'solar_PV':
                Pmax_PV = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) & (
                        capacities_conv.tech == item)), 'value'].squeeze())
                Eff_PV = math.ceil(tech_details.loc[(tech_details.tech == item), 'eff'].squeeze()) * 0.01
                Pmin_PV = 0

                comp_list.append(PV(num_opt_var))

                power_list.append(comp_list[it].P_PV)

                for t in range(num_opt_var):
                    comp_list[it].I_PV[t] = I_Solar[t] * (Pmax_PV / d_PV)
                    comp_list[it].TempEff_PV[t] = 1 + ((-beta) * ((Ta[t] - Tstc) +
                                                                  (Tnoct - Ta[t]) * (I_Solar[t] / 0.8)))

                for t in range(num_opt_var):
                    constr += [comp_list[it].P_PV[t] >= 0,
                               comp_list[it].P_PV[t] <= (comp_list[it].b_PV[t] * Eff_PV *
                                                         comp_list[it].TempEff_PV[t] * comp_list[it].I_PV[t])]

                it = it + 1

            elif item == 'solar_PVT':

                Pmax_PVT = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) & (
                        capacities_conv.tech == item)), 'value'].squeeze())
                Eff_PVT = tech_details.loc[(tech_details.tech == item), 'outshare'].squeeze()
                list1 = Eff_PVT.split(",")
                li1 = []
                for t in list1:
                    li1.append(float(t))

                PEff_PVT = li1[0]
                QEff_PVT = li1[1]

                Eff_PVT = tech_details.loc[(tech_details.tech == item), 'eff'].squeeze() * 0.01

                comp_list.append(PVT(num_opt_var))

                power_list.append(comp_list[it].P_PVT)
                heat_list.append(comp_list[it].Q_PVT)

                for t in range(num_opt_var):
                    comp_list[it].I_PVT[t] = I_Solar[t] * (Pmax_PVT / d_PVT)
                    comp_list[it].TempEff_PVT[t] = 1 + ((-beta) * ((Ta[t] - Tstc) +
                                                                   (Tnoct - Ta[t]) * (I_Solar[t] / 0.8)))

                for t in range(num_opt_var):
                    constr += [comp_list[it].P_PVT[t] >= 0, comp_list[it].Q_PVT[t] >= 0,
                               comp_list[it].Out_PVT[t] <= (comp_list[it].b_PVT[t] * Eff_PVT *
                                                            comp_list[it].TempEff_PVT[t] * comp_list[it].I_PVT[t]),
                               comp_list[it].P_PVT[t] == PEff_PVT * comp_list[it].Out_PVT[t],
                               comp_list[it].Q_PVT[t] == QEff_PVT * comp_list[it].Out_PVT[t]]

                it = it + 1

            elif item == 'Gas_CHP_unit_1':

                Pmax_mCHP = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) & (
                        capacities_conv.tech == item)), 'value'].squeeze())
                Pmin_mCHP = 0
                Eff_mCHP = tech_details.loc[(tech_details.tech == item), 'outshare'].squeeze()

                list2 = Eff_mCHP.split(",")
                li = []
                for k in list2:
                    li.append(float(k))
                PEff_mCHP = li[0]
                QEff_mCHP = li[1]
                Eff_mCHP = tech_details.loc[(tech_details.tech == item), 'eff'].squeeze() * 0.01

                comp_list.append(mCHP(num_opt_var))

                power_list.append(comp_list[it].P_mCHP)
                heat_list.append(comp_list[it].Q_mCHP)

                for t in range(num_opt_var):
                    cost += comp_list[it].C_mCHP[t]
                    constr += [comp_list[it].Out_mCHP[t] >= comp_list[it].b_mCHP[t] * Pmin_mCHP,
                               comp_list[it].Out_mCHP[t] <= comp_list[it].b_mCHP[t] * Pmax_mCHP,
                               comp_list[it].P_mCHP[t] == PEff_mCHP * comp_list[it].Out_mCHP[t],
                               comp_list[it].Q_mCHP[t] == QEff_mCHP * comp_list[it].Out_mCHP[t],
                               comp_list[it].C_mCHP[t] == C_Fuel * comp_list[it].F_mCHP[t],
                               comp_list[it].F_mCHP[t] == comp_list[it].P_mCHP[t] / Eff_mCHP]

                it = it + 1

            elif item == 'Gas_CHP_unit_2':

                Pmax_CHP = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) & (
                        capacities_conv.tech == item)), 'value'].squeeze())
                Pmin_CHP = 0
                Eff_CHP = tech_details.loc[(tech_details.tech == item), 'outshare'].squeeze()
                list2 = Eff_CHP.split(",")
                li = []
                for m in list2:
                    li.append(float(m))
                PEff_CHP = li[0]
                QEff_CHP = li[1]
                Eff_CHP = tech_details.loc[(tech_details.tech == item), 'eff'].squeeze() * 0.01
                CHP_points = CHP_operation(Pmax_CHP, Eff_CHP, PEff_CHP, QEff_CHP, n_CHP, min_cap_CHP)
                CHP_fuelcap = Pmax_CHP / Eff_CHP

                P11_CHP = CHP_points[1][0]
                P12_CHP = CHP_points[1][0]
                P13_CHP = CHP_points[1][3]
                P14_CHP = CHP_points[1][4]
                P21_CHP = CHP_points[1][0]
                P22_CHP = CHP_points[1][1]
                P23_CHP = CHP_points[1][2]
                P24_CHP = CHP_points[1][3]

                Q11_CHP = CHP_points[0][0]
                Q12_CHP = CHP_points[0][0]
                Q13_CHP = CHP_points[0][3]
                Q14_CHP = CHP_points[0][4]
                Q21_CHP = CHP_points[0][0]
                Q22_CHP = CHP_points[0][1]
                Q23_CHP = CHP_points[0][2]
                Q24_CHP = CHP_points[0][3]

                comp_list.append(CHP(num_opt_var))

                power_list.append(comp_list[it].P_CHP)
                heat_list.append(comp_list[it].Q_CHP)

                constr += [comp_list[it].ysum_CHP[0] == comp_list[it].yon_CHP[0],
                           comp_list[it].zsum_CHP[0] == comp_list[it].zoff_CHP[0],
                           comp_list[it].R_CHP[0] == (Rini_CHP + 1) * comp_list[it].b_CHP[0],
                           comp_list[it].D_CHP[0] == (Dini_CHP + 1) * (1 - comp_list[it].b_CHP[0])]

                if Rini_CHP == 0:
                    constr += [comp_list[it].zoff_CHP[0] == 0, comp_list[it].yon_CHP[0] == comp_list[it].b_CHP[0]]
                elif Dini_CHP == 0:
                    constr += [comp_list[it].zoff_CHP[0] == 1 - comp_list[it].b_CHP[0], comp_list[it].yon_CHP[0] == 0]

                for t in range(num_opt_var):

                    cost += comp_list[it].C_CHP[t]
                    constr += [comp_list[it].P_CHP[t] <= Pmax_CHP * 10 * comp_list[it].b_CHP[t],
                               comp_list[it].Q_CHP[t] <= Pmax_CHP * 10 * comp_list[it].b_CHP[t],
                               comp_list[it].P_CHP[t] >= Pmin_CHP * comp_list[it].b_CHP[t],
                               comp_list[it].Q_CHP[t] >= Pmin_CHP * comp_list[it].b_CHP[t],
                               comp_list[it].P_CHP[t] == (
                                       comp_list[it].w11_CHP[t] * P11_CHP + comp_list[it].w12_CHP[t] * P12_CHP +
                                       comp_list[it].w13_CHP[t] * P13_CHP + comp_list[it].w14_CHP[t] * P14_CHP +
                                       comp_list[it].w21_CHP[t] * P21_CHP + comp_list[it].w22_CHP[t] * P22_CHP +
                                       comp_list[it].w23_CHP[t] * P23_CHP + comp_list[it].w24_CHP[t] * P24_CHP),
                               comp_list[it].Q_CHP[t] == (
                                       comp_list[it].w11_CHP[t] * Q11_CHP + comp_list[it].w12_CHP[t] * Q12_CHP +
                                       comp_list[it].w13_CHP[t] * Q13_CHP + comp_list[it].w14_CHP[t] * Q14_CHP +
                                       comp_list[it].w21_CHP[t] * Q21_CHP + comp_list[it].w22_CHP[t] * Q22_CHP +
                                       comp_list[it].w23_CHP[t] * Q23_CHP + comp_list[it].w24_CHP[t] * Q24_CHP),
                               comp_list[it].b1_CHP[t] + comp_list[it].b2_CHP[t] == comp_list[it].b_CHP[t],
                               comp_list[it].w11_CHP[t] + comp_list[it].w12_CHP[t] + comp_list[it].w13_CHP[t] +
                               comp_list[it].w14_CHP[t] == comp_list[it].b1_CHP[t],
                               comp_list[it].w21_CHP[t] + comp_list[it].w22_CHP[t] + comp_list[it].w23_CHP[t] +
                               comp_list[it].w24_CHP[t] == comp_list[it].b2_CHP[t],
                               comp_list[it].w11_CHP[t] >= 0, comp_list[it].w12_CHP[t] >= 0,
                               comp_list[it].w13_CHP[t] >= 0, comp_list[it].w14_CHP[t] >= 0,
                               comp_list[it].w21_CHP[t] >= 0, comp_list[it].w22_CHP[t] >= 0,
                               comp_list[it].w23_CHP[t] >= 0, comp_list[it].w24_CHP[t] >= 0,
                               comp_list[it].w11_CHP[t] <= 1, comp_list[it].w12_CHP[t] <= 1,
                               comp_list[it].w13_CHP[t] <= 1, comp_list[it].w14_CHP[t] <= 1,
                               comp_list[it].w21_CHP[t] <= 1, comp_list[it].w22_CHP[t] <= 1,
                               comp_list[it].w23_CHP[t] <= 1, comp_list[it].w24_CHP[t] <= 1,
                               comp_list[it].yon_CHP[t] + comp_list[it].zoff_CHP[t] <= 1,
                               comp_list[it].C_CHP[t] == C_Fuel * comp_list[it].F_CHP[t],
                               comp_list[it].F_CHP[t] == comp_list[it].P_CHP[t] / Eff_CHP]
                    if t >= 1:
                        constr += [comp_list[it].P_CHP[t] <= (comp_list[it].P_CHP[t - 1] + 0.5 * Pmax_CHP *
                                                              (comp_list[it].b_CHP[t - 1] + comp_list[it].yon_CHP[t])),
                                   comp_list[it].P_CHP[t] >= (comp_list[it].P_CHP[t - 1] - 0.5 * Pmax_CHP *
                                                              (comp_list[it].b_CHP[t] + comp_list[it].zoff_CHP[t])),
                                   comp_list[it].Q_CHP[t] <= (comp_list[it].Q_CHP[t - 1] + 0.5 * Pmax_CHP *
                                                              (comp_list[it].b_CHP[t - 1] + comp_list[it].yon_CHP[t])),
                                   comp_list[it].Q_CHP[t] >= (comp_list[it].Q_CHP[t - 1] - 0.5 * Pmax_CHP *
                                                              (comp_list[it].b_CHP[t] + comp_list[it].zoff_CHP[t])),
                                   comp_list[it].yon_CHP[t] - comp_list[it].zoff_CHP[t] == (comp_list[it].b_CHP[t] -
                                                                                            comp_list[it].b_CHP[t - 1])]

                    # min up time constraints
                    if UPmin_CHP > 0:
                        if t >= UPmin_CHP:
                            constr += [comp_list[it].b_CHP[t] >= comp_list[it].ysum_CHP[t],
                                       comp_list[it].ysum_CHP[t] == (comp_list[it].ysum_CHP[t - 1] -
                                                                     comp_list[it].yon_CHP[t - UPmin_CHP] +
                                                                     comp_list[it].yon_CHP[t])]

                        elif 1 <= t < UPmin_CHP:
                            constr += [comp_list[it].b_CHP[t] >= comp_list[it].ysum_CHP[t],
                                       comp_list[it].ysum_CHP[t] == (comp_list[it].ysum_CHP[t - 1] +
                                                                     comp_list[it].yon_CHP[t])]

                        if 0 < Rini_CHP < UPmin_CHP:
                            if t < (UPmin_CHP - Rini_CHP):
                                constr += [comp_list[it].b_CHP[t] == 1,
                                           comp_list[it].yon_CHP[t] == 0, comp_list[it].zoff_CHP[t] == 0]

                        elif Rini_CHP >= UPmin_CHP:
                            if t == 0:
                                constr += [comp_list[it].zoff_CHP[t] == 1 - comp_list[it].b_CHP[t],
                                           comp_list[it].yon_CHP[t] == 0]

                    if DNmin_CHP > 0:

                        if t >= DNmin_CHP:
                            constr += [(1 - comp_list[it].b_CHP[t]) >= comp_list[it].zsum_CHP[t],
                                       comp_list[it].zsum_CHP[t] == (comp_list[it].zsum_CHP[t - 1] -
                                                                     comp_list[it].zoff_CHP[t - DNmin_CHP] +
                                                                     comp_list[it].zoff_CHP[t])]

                        if 1 <= t < DNmin_CHP:
                            constr += [(1 - comp_list[it].b_CHP[t]) >= comp_list[it].zsum_CHP[t],
                                       comp_list[it].zsum_CHP[t] == (comp_list[it].zsum_CHP[t - 1] +
                                                                     comp_list[it].zoff_CHP[t])]

                        if 0 < Dini_CHP < DNmin_CHP:
                            if t < (DNmin_CHP - Dini_CHP):
                                constr += [comp_list[it].b_CHP[t] == 0, comp_list[it].yon_CHP[t] == 0,
                                           comp_list[it].zoff_CHP[t] == 0]

                        elif Dini_CHP >= DNmin_CHP:
                            if t == 0:
                                constr += [comp_list[it].zoff_CHP[t] == 0,
                                           comp_list[it].yon_CHP[t] == comp_list[it].b_CHP[t]]

                it = it + 1

            elif item == 'GSHP_1' or item == 'GSHP_2':

                Qmax_GSHP = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) &
                                                           (capacities_conv.tech == item)), 'value'].squeeze())
                Qmin_GSHP = 0

                comp_list.append(GSHP(num_opt_var))

                power_list.append(-comp_list[it].P_GSHP)
                heat_list.append(comp_list[it].Q_GSHP)

                for t in range(num_opt_var):
                    constr += [comp_list[it].Q_GSHP[t] <= comp_list[it].b_GSHP[t] * Qmax_GSHP,
                               comp_list[it].Q_GSHP[t] >= comp_list[it].b_GSHP[t] * Qmin_GSHP,
                               comp_list[it].Q_GSHP[t] == comp_list[it].P_GSHP[t] * COP]

                it = it + 1

            elif item == 'gas_boiler_1' or item == 'gas_boiler_2':

                Qmax_GB = math.ceil(capacities_conv.loc[((capacities_conv.hub == hub) &
                                                         (capacities_conv.tech == item)), 'value'].squeeze())
                Qmin_GB = 0

                x0_GB = 0
                x1_GB = 0.25 * Qmax_GB
                x2_GB = 0.5 * Qmax_GB
                x3_GB = 0.75 * Qmax_GB
                x4_GB = Qmax_GB

                Eff1_GB = 0.01 * (21.49 + (182.18 * (1 / 4)) + ((-120.67) * (1 / 4) ** 2))
                Eff2_GB = 0.01 * (21.49 + (182.18 * (2 / 4)) + ((-120.67) * (2 / 4) ** 2))
                Eff3_GB = 0.01 * (21.49 + (182.18 * (3 / 4)) + ((-120.67) * (3 / 4) ** 2))
                Eff4_GB = 0.01 * (21.49 + (182.18 * (4 / 4)) + ((-120.67) * (4 / 4) ** 2))

                comp_list.append(GB(num_opt_var))

                heat_list.append(comp_list[it].Q_GB)

                for t in range(num_opt_var):
                    cost += comp_list[it].C_GB[t]
                    constr += [comp_list[it].Q_GB[t] <= comp_list[it].b_GB[t] * Qmax_GB,
                               comp_list[it].Q_GB[t] >= comp_list[it].b_GB[t] * Qmin_GB,
                               comp_list[it].Q_GB[t] == (comp_list[it].w1_GB[t] * x1_GB +
                                                         comp_list[it].w2_GB[t] * x2_GB +
                                                         comp_list[it].w3_GB[t] * x3_GB +
                                                         comp_list[it].w4_GB[t] * x4_GB),
                               (comp_list[it].b1_GB[t] + comp_list[it].b2_GB[t] +
                                comp_list[it].b3_GB[t] + comp_list[it].b4_GB[t]) == comp_list[it].b_GB[t],
                               (comp_list[it].w0_GB[t] + comp_list[it].w1_GB[t] + comp_list[it].w2_GB[t] +
                                comp_list[it].w3_GB[t] + comp_list[it].w4_GB[t]) == comp_list[it].b_GB[t],
                               comp_list[it].w0_GB[t] <= comp_list[it].b1_GB[t],
                               comp_list[it].w1_GB[t] <= comp_list[it].b1_GB[t] + comp_list[it].b2_GB[t],
                               comp_list[it].w2_GB[t] <= comp_list[it].b3_GB[t] + comp_list[it].b2_GB[t],
                               comp_list[it].w3_GB[t] <= comp_list[it].b3_GB[t] + comp_list[it].b4_GB[t],
                               comp_list[it].w4_GB[t] <= comp_list[it].b4_GB[t],
                               comp_list[it].w0_GB[t] >= 0, comp_list[it].w1_GB[t] >= 0, comp_list[it].w2_GB[t] >= 0,
                               comp_list[it].w3_GB[t] >= 0, comp_list[it].w4_GB[t] >= 0,
                               comp_list[it].w0_GB[t] <= 1, comp_list[it].w1_GB[t] <= 1, comp_list[it].w2_GB[t] <= 1,
                               comp_list[it].w3_GB[t] <= 1, comp_list[it].w4_GB[t] <= 1,
                               comp_list[it].C_GB[t] == C_Fuel * comp_list[it].F_GB[t],
                               comp_list[it].F_GB[t] == (comp_list[it].w1_GB[t] * (x1_GB / Eff1_GB) +
                                                         comp_list[it].w2_GB[t] * (x2_GB / Eff2_GB) +
                                                         comp_list[it].w3_GB[t] * (x3_GB / Eff3_GB) +
                                                         comp_list[it].w4_GB[t] * (x4_GB / Eff4_GB))]

                it = it + 1

        for item in list_storage:

            if item == 'heat_storage':

                Qmax_Storage = capacities_stor.loc[((capacities_stor.hub == hub) &
                                                    (capacities_stor.tech == item)), 'value'].squeeze()
                Eff_Storage = storage_details.loc[(storage_details.tech == item), 'stateff'].squeeze()
                Eff_StorageCh = storage_details.loc[(storage_details.tech == item), 'cyceff'].squeeze()
                Eff_StorageDc = Eff_StorageCh

                comp_list.append(Heat_Storage(num_opt_var))

                constr += [comp_list[it].Q_StorageTot[0] == (Eff_Storage * thermal_storage +
                                                             Eff_StorageCh * comp_list[it].Q_StorageCh[0] -
                                                             Eff_StorageDc * comp_list[it].Q_StorageDc[0])]

                heat_list.append(comp_list[it].Q_StorageDc)
                heat_list.append(-comp_list[it].Q_StorageCh)

                for t in range(num_opt_var):

                    constr += [comp_list[it].Q_StorageCh[t] >= 0,
                               comp_list[it].Q_StorageCh[t] <= BigM * comp_list[it].b_StorageCh[t],
                               comp_list[it].Q_StorageDc[t] >= 0,
                               comp_list[it].Q_StorageDc[t] <= BigM * comp_list[it].b_StorageDc[t],
                               comp_list[it].Q_StorageTot[t] >= 0.2 * Qmax_Storage,
                               comp_list[it].Q_StorageTot[t] <= Qmax_Storage,
                               comp_list[it].b_StorageCh[t] + comp_list[it].b_StorageDc[t] == 1,
                               comp_list[it].b_StorageCh[t] >= 0, comp_list[it].b_StorageDc[t] >= 0,
                               comp_list[it].b_StorageCh[t] <= 1, comp_list[it].b_StorageDc[t] <= 1]
                    if t >= 1:
                        constr += [comp_list[it].Q_StorageTot[t] == (Eff_Storage * comp_list[it].Q_StorageTot[t - 1] +
                                                                     Eff_StorageCh * comp_list[it].Q_StorageCh[t] -
                                                                     Eff_StorageDc * comp_list[it].Q_StorageDc[t])]

                it = it + 1

            elif item == 'Battery':

                Pmax_Battery = capacities_stor.loc[((capacities_stor.hub == hub) &
                                                    (capacities_stor.tech == item)), 'value'].squeeze()
                Pmin_Battery = 0
                Eff_Battery = storage_details.loc[(storage_details.tech == item), 'stateff'].squeeze()
                Eff_BatteryCh = storage_details.loc[(storage_details.tech == item), 'cyceff'].squeeze()
                Eff_BatteryDc = Eff_BatteryCh

                c1_Battery = 0.11
                c2_Battery = 0.32
                c3_Battery = 0.55
                c4_Battery = 0.76

                comp_list.append(Elec_Storage(num_opt_var))

                power_list.append(comp_list[it].P_BatteryDc)
                power_list.append(-comp_list[it].P_BatteryCh)

                constr += [comp_list[it].P_BatteryTot[0] == (Eff_Battery * elec_storage +
                                                             Eff_BatteryCh * comp_list[it].P_BatteryCh[0] -
                                                             (1 / Eff_BatteryDc) * comp_list[it].P_BatteryDc[0]),
                           comp_list[it].w1_Battery[0] == (battery_depth[0] + comp_list[it].p1_BatteryCh[0] -
                                                           comp_list[it].p1_BatteryDc[0]),
                           comp_list[it].w2_Battery[0] == (battery_depth[1] + comp_list[it].p2_BatteryCh[0] -
                                                           comp_list[it].p2_BatteryDc[0]),
                           comp_list[it].w3_Battery[0] == (battery_depth[2] + comp_list[it].p3_BatteryCh[0] -
                                                           comp_list[it].p3_BatteryDc[0]),
                           comp_list[it].w4_Battery[0] == (battery_depth[3] + comp_list[it].p4_BatteryCh[0] -
                                                           comp_list[it].p4_BatteryDc[0])]

                for t in range(num_opt_var):

                    cost += comp_list[it].C_Battery[t]
                    constr += [comp_list[it].P_BatteryTot[t] >= 0.2 * Pmax_Battery,
                               comp_list[it].P_BatteryTot[t] <= 0.8 * Pmax_Battery,
                               comp_list[it].P_BatteryCh[t] >= 0,
                               comp_list[it].P_BatteryCh[t] <= BigM * comp_list[it].b_BatteryCh[t],
                               comp_list[it].P_BatteryDc[t] >= 0,
                               comp_list[it].P_BatteryDc[t] <= BigM * comp_list[it].b_BatteryDc[t],
                               comp_list[it].b_BatteryCh[t] + comp_list[it].b_BatteryDc[t] == 1,
                               comp_list[it].w1_Battery[t] <= 0.2, comp_list[it].w2_Battery[t] <= 0.2,
                               comp_list[it].w3_Battery[t] <= 0.2, comp_list[it].w4_Battery[t] <= 0.2,
                               comp_list[it].w1_Battery[t] >= 0, comp_list[it].w2_Battery[t] >= 0,
                               comp_list[it].w3_Battery[t] >= 0, comp_list[it].w4_Battery[t] >= 0,
                               comp_list[it].p1_BatteryCh[t] >= 0, comp_list[it].p2_BatteryCh[t] >= 0,
                               comp_list[it].p3_BatteryCh[t] >= 0, comp_list[it].p4_BatteryCh[t] >= 0,
                               comp_list[it].p1_BatteryDc[t] >= 0, comp_list[it].p2_BatteryDc[t] >= 0,
                               comp_list[it].p3_BatteryDc[t] >= 0, comp_list[it].p4_BatteryDc[t] >= 0,
                               comp_list[it].p1_BatteryCh[t] <= 0.2 - comp_list[it].w1_Battery[t],
                               comp_list[it].p2_BatteryCh[t] <= 0.2 - comp_list[it].w2_Battery[t],
                               comp_list[it].p3_BatteryCh[t] <= 0.2 - comp_list[it].w3_Battery[t],
                               comp_list[it].p4_BatteryCh[t] <= 0.2 - comp_list[it].w4_Battery[t],
                               comp_list[it].p1_BatteryDc[t] <= comp_list[it].w1_Battery[t],
                               comp_list[it].p2_BatteryDc[t] <= comp_list[it].w2_Battery[t],
                               comp_list[it].p3_BatteryDc[t] <= comp_list[it].w3_Battery[t],
                               comp_list[it].p4_BatteryDc[t] <= comp_list[it].w4_Battery[t],
                               (1 / Eff_BatteryDc) * comp_list[it].P_BatteryDc[t] == ((comp_list[it].p1_BatteryDc[t] +
                                                                                       comp_list[it].p2_BatteryDc[t] +
                                                                                       comp_list[it].p3_BatteryDc[t] +
                                                                                       comp_list[it].p4_BatteryDc[t])
                                                                                      * Pmax_Battery),
                               Eff_BatteryCh * comp_list[it].P_BatteryCh[t] == ((comp_list[it].p1_BatteryCh[t] +
                                                                                 comp_list[it].p2_BatteryCh[t] +
                                                                                 comp_list[it].p3_BatteryCh[t] +
                                                                                 comp_list[it].p4_BatteryCh[t])
                                                                                * Pmax_Battery),
                               comp_list[it].C_Battery[t] == ((c1_Battery * (comp_list[it].p1_BatteryCh[t] +
                                                                            comp_list[it].p1_BatteryDc[t]) +
                                                              c2_Battery * (comp_list[it].p2_BatteryCh[t] +
                                                                            comp_list[it].p2_BatteryDc[t]) +
                                                              c3_Battery * (comp_list[it].p3_BatteryCh[t] +
                                                                            comp_list[it].p3_BatteryDc[t]) +
                                                              c4_Battery * (comp_list[it].p4_BatteryCh[t] +
                                                                            comp_list[it].p4_BatteryDc[t]))
                               * Pmax_Battery)]
                    if t >= 1:
                        constr += [
                            comp_list[it].P_BatteryTot[t] == (Eff_Battery * comp_list[it].P_BatteryTot[t - 1] +
                                                              Eff_BatteryCh * comp_list[it].P_BatteryCh[t] -
                                                              (1 / Eff_BatteryDc) * comp_list[it].P_BatteryDc[t]),
                            comp_list[it].w1_Battery[t] == (comp_list[it].w1_Battery[t - 1] +
                                                            comp_list[it].p1_BatteryCh[t] -
                                                            comp_list[it].p1_BatteryDc[t]),
                            comp_list[it].w2_Battery[t] == (comp_list[it].w2_Battery[t - 1] +
                                                            comp_list[it].p2_BatteryCh[t] -
                                                            comp_list[it].p2_BatteryDc[t]),
                            comp_list[it].w3_Battery[t] == (comp_list[it].w3_Battery[t - 1] +
                                                            comp_list[it].p3_BatteryCh[t] -
                                                            comp_list[it].p3_BatteryDc[t]),
                            comp_list[it].w4_Battery[t] == (comp_list[it].w4_Battery[t - 1] +
                                                            comp_list[it].p4_BatteryCh[t] -
                                                            comp_list[it].p4_BatteryDc[t])]

                it = it + 1

        R_GridOut = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), 'el_tariff'].values.tolist()
        R_GridIn = demand_data.loc[time_now: time_now + dt.timedelta(hours=23), 'feed_in_tariff'].values.tolist()

        comp_list.append(Elec_Grid(num_opt_var))

        if len(power_list) < 24:
            t = len(power_list)
            while t <= 23:
                power_list.append([0] * num_opt_var)
                t = t + 1

        if len(heat_list) < 24:
            t = len(heat_list)
            while t <= 23:
                heat_list.append([0] * num_opt_var)
                t = t + 1

        for t in range(num_opt_var):
            # Demand
            cost += comp_list[it].C_Grid[t] + (cp.huber(cp.norm(comp_list[it].P_Slack[t]), P_delta) +
                                               cp.huber(cp.norm(comp_list[it].Q_Slack[t]), Q_delta))

            constr += [P_Demand[t] == (power_list[0][t] + power_list[1][t] + power_list[2][t] + power_list[3][t] +
                                       power_list[4][t] + power_list[5][t] + power_list[6][t] + power_list[7][t] +
                                       power_list[8][t] + power_list[9][t] + power_list[10][t] + power_list[11][t] +
                                       power_list[12][t] + power_list[13][t] + power_list[14][t] + power_list[15][t] +
                                       power_list[16][t] + power_list[17][t] + power_list[18][t] + power_list[19][t] +
                                       power_list[20][t] + power_list[21][t] + power_list[22][t] + power_list[23][t] +
                                       comp_list[it].P_GridOut[t] - comp_list[it].P_GridIn[t]),
                       Q_Demand[t] == (heat_list[0][t] + heat_list[1][t] + heat_list[2][t] + heat_list[3][t] +
                                       heat_list[4][t] + heat_list[5][t] + heat_list[6][t] + heat_list[7][t] +
                                       heat_list[8][t] + heat_list[9][t] + heat_list[10][t] + heat_list[11][t] +
                                       heat_list[12][t] + heat_list[13][t] + heat_list[14][t] + heat_list[15][t] +
                                       heat_list[16][t] + heat_list[17][t] + heat_list[18][t] + heat_list[19][t] +
                                       heat_list[20][t] + heat_list[21][t] + heat_list[22][t] + heat_list[23][t]),
                       comp_list[it].b_GridIn[t] + comp_list[it].b_GridOut[t] <= 1,
                       comp_list[it].P_GridIn[t] >= 0, comp_list[it].P_GridIn[t] <= BigM * comp_list[it].b_GridIn[t],
                       comp_list[it].P_GridOut[t] >= 0, comp_list[it].P_GridOut[t] <= BigM * comp_list[it].b_GridOut[t],
                       comp_list[it].C_Grid[t] == (R_GridOut[t] * comp_list[it].P_GridOut[t] -
                                                   R_GridIn[t] * comp_list[it].P_GridIn[t]),
                       comp_list[it].P_Slack[t] >= 0, comp_list[it].Q_Slack[t] >= 0]

            # Solve with mosek or Gurobi
        problem = cp.Problem(cp.Minimize(cost), constr)
        problem.solve(solver=cp.MOSEK, verbose=True, save_file='opt_diagnosis.opf',
                      mosek_params={mosek.iparam.intpnt_solve_form: mosek.solveform.dual,
                                    mosek.dparam.optimizer_max_time: 100.0})
        opt_stat = problem.status
        opt_val = problem.value
        opt_time = problem.solver_stats
        print(f"Status:{opt_stat}, with Value:{opt_val:.2f}")

        power = []
        heat = []
        grid_in = []
        grid_out = []
        storage_heatin = []
        storage_heatout = []
        storage_elecin = []
        storage_elecout = []
        tech_cost = 0
        storage_cost = 0
        grid_cost = 0
        final_cost = 0
        storage_elec = 0
        storage_heat = 0
        batt_depth = [0, 0, 0, 0]
        batt_in = []
        batt_out = []
        CHP_ontime = 0
        CHP_offtime = 0

        it = 0
        for item in list_techs:

            if item == 'solar_PV':
                power.append(comp_list[it].P_PV.value[0])
                heat.append(0)
                it = it + 1

            if item == 'solar_PVT':
                power.append(comp_list[it].P_PVT.value[0])
                heat.append(comp_list[it].Q_PVT.value[0])
                it = it + 1

            if item == 'Gas_CHP_unit_1':
                power.append(comp_list[it].P_mCHP.value[0])
                heat.append(comp_list[it].Q_mCHP.value[0])
                tech_cost += comp_list[it].C_mCHP.value[0]
                final_cost += comp_list[it].C_mCHP.value[0]
                it = it + 1

            if item == 'Gas_CHP_unit_2':
                power.append(comp_list[it].P_CHP.value[0])
                heat.append(comp_list[it].Q_CHP.value[0])
                tech_cost += comp_list[it].C_CHP.value[0]

                CHP_ontime = comp_list[it].R_CHP.value[0]
                CHP_offtime = comp_list[it].D_CHP.value[0]
                final_cost += comp_list[it].C_CHP.value[0]
                it = it + 1

            if item == 'GSHP_1' or item == 'GSHP_2':
                power.append(-comp_list[it].P_GSHP.value[0])
                heat.append(comp_list[it].Q_GSHP.value[0])
                it = it + 1

            if item == 'gas_boiler_1' or item == 'gas_boiler_2':
                power.append(0)
                heat.append(comp_list[it].Q_GB.value[0])
                tech_cost += comp_list[it].C_GB.value[0]
                final_cost += comp_list[it].C_GB.value[0]
                it = it + 1

        for item in list_storage:

            if item == 'heat_storage':
                storage_heatin.append(-comp_list[it].Q_StorageCh.value[0])
                storage_heatout.append(comp_list[it].Q_StorageDc.value[0])
                storage_heat = comp_list[it].Q_StorageTot.value[0]
                it = it + 1

            if item == 'Battery':
                storage_elecin.append(-comp_list[it].P_BatteryCh.value[0])
                storage_elecout.append(comp_list[it].P_BatteryDc.value[0])
                storage_elec = comp_list[it].P_BatteryTot.value[0]
                storage_cost += comp_list[it].C_Battery.value[0]
                batt_depth = [comp_list[it].w1_Battery.value[0], comp_list[it].w2_Battery.value[0],
                              comp_list[it].w3_Battery.value[0], comp_list[it].w4_Battery.value[0]]
                batt_in = [comp_list[it].p1_BatteryCh.value[0], comp_list[it].p2_BatteryCh.value[0],
                           comp_list[it].p3_BatteryCh.value[0], comp_list[it].p4_BatteryCh.value[0]]
                batt_out = [comp_list[it].p1_BatteryDc.value[0], comp_list[it].p2_BatteryDc.value[0],
                            comp_list[it].p3_BatteryDc.value[0], comp_list[it].p4_BatteryDc.value[0]]
                final_cost += comp_list[it].C_Battery.value[0]
                it = it + 1

        grid_cost = [comp_list[it].C_Grid.value[0]]
        final_cost += comp_list[it].C_Grid.value[0]

        grid_in.append(-comp_list[it].P_GridIn.value[0])
        grid_out.append(comp_list[it].P_GridOut.value[0])

        demand_power = [P_Demand[0]]
        demand_heat = [Q_Demand[0]]

        final_cost = [final_cost]
        tech_cost = [tech_cost]
        storage_cost = [storage_cost]

        print('run complete')
        return demand_power, demand_heat, power, heat, grid_in, grid_out, storage_heatin, storage_heatout, storage_elecin, storage_elecout, tech_cost, storage_cost, grid_cost, final_cost, storage_heat, storage_elec, batt_depth, CHP_ontime, CHP_offtime
Example #13
0
def huber_loss_fn(residuals, params):
    M = params
    return np.sum([i for i in cp.huber(residuals, M)])
Example #14
0
def huber_loss_fn(Kmat, Ymat, alpha_coef):
    return cp.sum(cp.huber(Kmat.T * alpha_coef - Ymat, 1))
Example #15
0
                           (u + next_u) / 2) +
                  linf(np_x[2 * i + 2, :], np_u[i + 1, :], next_x, next_u)) *
            dt / 6)
        #constraints.append(deldthetan == deldtheta + lin(at, deltheta) * dt)

        #conditions on allowable control
        constraints.append(next_u <= 8.0)
        constraints.append(next_u >= -8.0)
        #trust regions
        #constraints.append(delthetan <= 0.5)
        #constraints.append(delthetan >= -0.5)
        #constraints.append(deldthetan <= 0.5)
        #constraints.append(deldthetan >= -0.5)

        #Cost calculation
        cost = cost + cvx.huber(x[0] - np.pi, M=0.5) + 0.01 * cvx.huber(
            u
        )  #+ (np.cos(np_x[2*i,:]) + 1) * (x[0] - np_x[2*i,:])  #+ cvx.square( x[0] - np.pi ) #+ cvx.square(u) #+ 0.1 * cvx.square(ut)
        # + cvx.square(np.cos(np_x[2*i,:])*(x - np_x[2*i,:]))
        x = next_x
        u = next_u

    cost = cost + 100 * cvx.square(
        x[0] - np.pi)  # cvx.huber( x[0] - np.pi, M=0.4)
    objective = cvx.Minimize(cost)
    #print(objective)
    #print(constraints)
    prob = cvx.Problem(objective, constraints)
    sol = prob.solve(verbose=True)
    #print(sol)
    #update by the del
Example #16
0
    PTl[np.asmatrix(pid).T, pid] = PTs
    #[pid,pid], [[pid],pid] don't work!!!!!!
    #http://stackoverflow.com/questions/4257394/slicing-of-a-numpy-2d-array-or-how-do-i-extract-an-mxm-submatrix-from-an-nxn-ar
    PTp.value = PTl * PTp.value

    det = np.abs(np.linalg.det(PTp.value))
    #print('det', det)
    if det != 1:
        input('...det not 1')


#
PTp = cv.Parameter(ct.m, ct.m)
PTp.value = np.asmatrix(np.eye(ct.m))
prbr = cv.Problem(cv.Minimize(\
        cv.sum_entries(cv.huber(ct.A * xvr - PTp * ct.y))))
prb = cv.Problem(cv.Minimize(cv.norm(ct.A * xvr - PTp * ct.y)))
bestres = np.float("inf")
bestxer = np.float("inf")
bestidx = np.float("inf")
bestP = np.float("inf")
#
pl.figure(1)
for idx in np.arange(8):
    #rough fit
    prbr.solve()
    #rearrange
    construct_PT(np, ct, xvr, PTp)
    #tight fit
    prb.solve()
    if prb.status != 'optimal':
Example #17
0
def method1():
    '''
	First, try and fine the TF for vent at a few good points, then used that to find the scaling 
	at other points. Works alright, not the best though.
	'''
    st = 500
    en = 1000
    T = en - st
    asm = a[st:en]
    valsm = val[st:en]
    Vtf = cvx.Variable(Vtf_end - Vtf_st)
    Val = cvx.Variable(T)
    g = 0
    for i in range(-Vtf_st, T - Vtf_end):
        if i + Vtf_st == 0:
            g += valsm[i] * cvx.vstack(Vtf, np.zeros(T - Vtf_end + Vtf_st))
        else:
            g += valsm[i] * cvx.vstack(np.zeros(
                (i + Vtf_st, 1)), Vtf, np.zeros((T - Vtf_end - i, 1)))
    const = [Val == g]
    obj = cvx.Minimize(cvx.sum_entries(cvx.huber(asm + Val, 0.2)))
    prob = cvx.Problem(obj, const)
    prob.solve(solver='ECOS',
               verbose=True,
               reltol=1e-10,
               abstol=1e-10,
               max_iters=200)
    #plt.plot(Vtf.value)
    #plt.show()
    Vtf = Vtf.value

    T = a.size
    print(T, bal.size)
    Bal = cvx.Variable(T)
    Val = cvx.Variable(T)
    Btf = cvx.Variable(Btf_end - Btf_st)
    Vs = cvx.Variable(T)
    const = []
    f = 0
    for i in range(-Btf_st, T - Btf_end):
        if i + Btf_st == 0:
            f += bal[i] * cvx.vstack(Btf, np.zeros(T - Btf_end + Btf_st))
        else:
            f += bal[i] * cvx.vstack(np.zeros(
                (i + Btf_st, 1)), Btf, np.zeros((T - Btf_end - i, 1)))
    const.append(Bal == f)
    const.append(Btf >= 0)
    const.append(cvx.sum_entries(Btf) == 400)

    g = 0
    for i in range(-Vtf_st, T - Vtf_end):
        if i + Vtf_st == 0:
            g += Vs[i] * val[i] * cvx.vstack(Vtf,
                                             np.zeros(T - Vtf_end + Vtf_st))
        else:
            g += Vs[i] * val[i] * cvx.vstack(np.zeros(
                (i + Vtf_st, 1)), Vtf, np.zeros((T - Vtf_end - i, 1)))
    const.append(Val == g)
    const.append(Vs >= 0)
    #const.append(cvx.sum_entries(Vtf) == 400)

    obj = cvx.Minimize(cvx.sum_entries(cvx.huber(a - Bal + Val, 0.2)))
    prob = cvx.Problem(obj, const)
    print("OHP")
    r = prob.solve(solver='ECOS',
                   verbose=True,
                   reltol=1e-10,
                   abstol=1e-10,
                   max_iters=200)
    print(r)

    plt.subplot(411)
    plt.plot(bal)
    plt.plot(val)
    plt.subplot(412)
    plt.plot(a)
    plt.plot(a + np.array((-Bal.value + Val.value))[:, 0])
    plt.subplot(413)
    plt.plot(Bal.value, 'r--')
    plt.subplot(414)
    plt.plot(Btf.value)
    #plt.plot(V.value)
    plt.show()
    1
Example #18
0
import scipy.sparse as sps
np.random.seed(0)
m = 5000
n = 200

x0 = np.random.randn(n)
A = np.random.randn(m, n)
A = A * sps.diags([1 / np.sqrt(np.sum(A**2, 0))], [0])
b = A.dot(x0) + np.sqrt(0.01) * np.random.randn(m)
b = b + 10 * np.asarray(sps.rand(m, 1, 0.05).todense()).ravel()

# Problem construction

x = cp.Variable(n)
prob = cp.Problem(cp.Minimize(cp.sum_entries(cp.huber(A * x - b))))

# Problem collection

# Single problem collection
problemDict = {"problemID": problemID, "problem": prob, "opt_val": opt_val}
problems = [problemDict]

# For debugging individual problems:
if __name__ == "__main__":

    def printResults(problemID="", problem=None, opt_val=None):
        print(problemID)
        problem.solve()
        print("\tstatus: {}".format(problem.status))
        print("\toptimal value: {}".format(problem.value))