예제 #1
0
 def chp_unit0(self, unit):
     self.chpunits.append(unit)
     unit.isopen = cvx.Bool()
     unit.mmain = cvx.Variable(self.T)
     unit.p1 = cvx.Variable(self.T)
     unit.p2 = cvx.Variable(self.T)
     unit.p3 = cvx.Variable(self.T)
     unit.h1 = cvx.Variable(self.T)
     unit.pschedule = unit.p1 + unit.p2 + unit.p3
     unit.hschedule = unit.h1
     unit.cost = sum(map(unit.costfun, unit.mmain))
     self.TEG += unit.pschedule
     self.TTG += unit.hschedule
     self.totalcost += unit.cost
     self.constraints += [unit.mmain == unit.p1]
     self.constraints += [unit.p1 == unit.p2]
     self.constraints += [unit.p2 == unit.p3 / 0.8 + unit.h1 / 4.66]
     self.constraints += [unit.p3 / 0.8 >= unit.p2 * 0.15]
     self.constraints += [unit.p1 >= 0]
     self.constraints += [unit.p2 >= 0]
     self.constraints += [unit.p3 >= 0]
     self.constraints += [unit.h1 >= 0]
     self.constraints += [
         unit.pschedule >= unit.pbound[0] * unit.isopen,
         unit.pschedule <= unit.pbound[1] * unit.isopen
     ]
예제 #2
0
 def test_CBC(self):
     A = cvxpy.Bool(1,name='A')
     B = cvxpy.Variable(1,name='B')
     constr = []
     constr.append(-1 <= B)
     constr.append(B <= 5)
     obj = cvxpy.Maximize(A - B)
     problem = cvxpy.Problem(obj, constr)
     ret = problem.solve(solver=cvxpy.CBC)
     self.assertTrue(problem.status in [cvxpy.OPTIMAL, cvxpy.OPTIMAL_INACCURATE])
예제 #3
0
def evaluate(gt_rels, gt_pms, pred_dsms, seq_len, appx=False, prec_pred_pms=None):

	# metrics
	ACC, IHA, CSA, NE, NA, PA, NDCG, KT = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
	rows, cols, n, samples = seq_len, seq_len, pow(seq_len, 2), gt_pms.shape[0]
	if prec_pred_pms is None:
		pred_pms = np.zeros(pred_dsms.shape, np.int)
	else:
		pred_pms = prec_pred_pms

	
	# Find the closest permutation matrix from the predicted	
	var = cvx.Bool(n)
	par = cvx.Parameter(n)
	Rnorm =  np.fromfunction(lambda i, j: j//cols == i, (rows, n), dtype=int).astype(np.float)
	Cnorm =  np.hstack([np.identity(cols) for c in range(cols)]).astype(np.float)       
	A = np.vstack((Rnorm, Cnorm))
	prob = cvx.Problem(cvx.Minimize(cvx.norm(var - par)), [A * var == 1])
	appx_count = 1
	if prec_pred_pms is None:
		for idx, dsm in enumerate(pred_dsms):				
			if appx:			
				if appx_count % 100 == 0:
					print("{} samples has benn approximated".format(appx_count))
				appx_count += 1
				
				try:
					par.value = dsm
					prob.solve()
					dsm = np.array(var.value, dtype=np.float).ravel()				
				except:				
					print("Optimization failed with dsm={}".format(dsm))
			pred_pms[idx] = (dsm.reshape(rows, cols).argmax(axis=1)[:, None] == np.arange(seq_len)).flatten().astype(int)
		
			if len(np.unique(pred_pms[idx].reshape(rows, cols).argmax(axis=0))) < seq_len or len(np.unique(pred_pms[idx].reshape(rows, cols).argmax(axis=1))) < seq_len:
				NA += 1.0
	 
	# compute non-accpeted matrix
	NA = NA/samples	

	# Compute Normalization error
	NE = np.mean(np.linalg.norm(1 - np.dot(pred_dsms, A.T), ord=1, axis=1)/A.shape[0])

	# Averaged Cossine Similarity
	dots = inner1d(gt_pms, pred_dsms)
	norms = np.multiply(np.linalg.norm(gt_pms, ord=2, axis=1), np.linalg.norm(pred_dsms, ord=2, axis=1))
	CSA = np.mean(np.divide(dots, norms))	
	
	# Permutation prediction evaluation
	ACC, IHA = perm_eval(gt_pms, pred_pms, seq_len)

	# Ranking Evaluation
	PA, NDCG, KT = rank_eval(gt_rels, gt_pms, pred_pms)		

	return ACC, IHA, CSA, NE, NA, PA, NDCG, KT
예제 #4
0
 def pc_unit(self, unit):
     self.pcunits.append(unit)
     unit.isopen = cvx.Bool()
     unit.mmain = cvx.Variable(self.T)
     unit.pschedule = cvx.Variable(self.T)
     unit.cost = sum(map(unit.costfun, unit.mmain))
     self.TEG += unit.pschedule
     self.totalcost += unit.cost
     self.constraints += [unit.mmain == unit.pschedule / 2.8]
     self.constraints += [
         unit.pschedule >= unit.pbound[0] * unit.isopen,
         unit.pschedule <= unit.pbound[1] * unit.isopen
     ]
예제 #5
0
 def __init__(self, perm_len):
     # define opt problem for inference
     self._rows, self._cols, self._n, = perm_len, perm_len, pow(perm_len, 2)
     self._var = cvx.Bool(self._n)
     self._par = cvx.Parameter(self._n)
     Rnorm = np.fromfunction(lambda i, j: j // self._cols == i,
                             (self._rows, self._n),
                             dtype=int).astype(np.float)
     Cnorm = np.hstack([np.identity(self._cols)
                        for c in range(self._cols)]).astype(np.float)
     self._A = np.vstack((Rnorm, Cnorm))
     self._prob = cvx.Problem(cvx.Minimize(cvx.norm(self._var - self._par)),
                              [self._A * self._var == 1])
예제 #6
0
def buildProblem(N, MLigne, MDiag):

    #Cost of problem
    ML = cvxpy.Bool(N, N)
    MD = cvxpy.Bool(N, N)

    cost = cvxpy.sum_entries(ML) + cvxpy.sum_entries(MD)

    costObject = cvxpy.Maximize(cost)

    constraints = []
    constraints.append(ML >= MLigne)
    constraints.append(MD >= MDiag)

    inveye = np.fliplr(np.eye(N, N))

    for i in range(N):
        inveye2 = np.fliplr(np.eye(i + 1, i + 1))

        constraints.append(cvxpy.sum_entries(ML[i, :]) <= 1)
        constraints.append(cvxpy.sum_entries(ML[:, i]) <= 1)

        if i >= 1:
            #constraints.append(cvxpy.trace(MD[:i+1, :i+1])  <= 1)
            constraints.append(cvxpy.trace(MD[:i + 1, :i + 1] * inveye2) <= 1)
            #constraints.append(cvxpy.trace(inveye * MD[:i+1, :i+1] * inveye)  <= 1)
            constraints.append(
                cvxpy.trace((inveye * MD)[:i + 1, :i + 1] * inveye2) <= 1)
            constraints.append(
                cvxpy.trace((MD * inveye)[:i + 1, :i + 1] * inveye2) <= 1)
            constraints.append(
                cvxpy.trace((inveye * MD * inveye)[:i + 1, :i + 1] *
                            inveye2) <= 1)

    problem = cvxpy.Problem(costObject, constraints)
    val = problem.solve(solver=cvxpy.GLPK_MI)

    return int(np.round(val)), np.round(ML.value), np.round(MD.value)
예제 #7
0
    def test_CBC_packaging_simple(self):
        num_states = 5
        A = cvxpy.Bool(num_states,name='A')
        B = cvxpy.Variable(num_states,name='B')
        constr = []
        for i in range(1,num_states):
            constr.append(1.0*A[i-1] + 1.0*A[i] <= 1.0)

        for i in range(num_states):
            constr.append(B[i] <= i)

        objs = []
        for i in range(num_states):
            objs.append(cvxpy.Maximize(A[i] - B[i]))
        problem = cvxpy.Problem(sum(objs), constr)
        ret = problem.solve(solver=cvxpy.CBC)
        self.assertTrue(problem.status in [cvxpy.OPTIMAL, cvxpy.OPTIMAL_INACCURATE])
예제 #8
0
def control(s1, gs, ob):

    w = cvxpy.Variable(2, T)
    v = cvxpy.Variable(2, T)
    s = cvxpy.Variable(2, T)
    u = cvxpy.Variable(2, T)
    nob = len(ob)
    o = cvxpy.Bool(4 * nob, T)

    constraints = [cvxpy.abs(u) <= u_max]
    constraints.append(s[:, 0] == s1)

    obj = []
    for t in range(T):
        constraints.append(s[:, t] - gs <= w[:, t])
        constraints.append(-s[:, t] + gs <= w[:, t])
        constraints.append(u[:, t] <= v[:, t])
        constraints.append(-u[:, t] <= v[:, t])

        obj.append(t * q.T * w[:, t] + r.T * v[:, t])

        # obstable avoidanse
        for io in range(nob):
            ind = io * 4
            constraints.append(sum(o[ind:ind + 4, t]) <= 3)
            constraints.append(s[0, t] <= ob[io, 0] + M * o[ind + 0, t])
            constraints.append(-s[0, t] <= -ob[io, 1] + M * o[ind + 1, t])
            constraints.append(s[1, t] <= ob[io, 2] + M * o[ind + 2, t])
            constraints.append(-s[1, t] <= -ob[io, 3] + M * o[ind + 3, t])

    for t in range(T - 1):
        constraints.append(s[:, t + 1] == A * s[:, t] + B * u[:, t])

    objective = cvxpy.Minimize(sum(obj))

    prob = cvxpy.Problem(objective, constraints)

    prob.solve(solver=cvxpy.GUROBI)

    s_p = s.value
    u_p = u.value
    print("status:" + prob.status)

    return s_p, u_p
예제 #9
0
 def chp_unit4(self, unit):
     self.chpunits.append(unit)
     unit.isopen = cvx.Bool()
     unit.mmain = cvx.Variable(self.T)
     unit.p1 = cvx.Variable(self.T)
     unit.p2 = cvx.Variable(self.T)
     unit.p3 = cvx.Variable(self.T)
     unit.h1 = cvx.Variable(self.T)
     unit.hr = cvx.Variable(self.T)
     unit.hs = cvx.Variable(self.T)
     unit.H = cvx.Variable(self.T + 1)
     unit.h0 = unit.hr - unit.hs
     unit.pschedule = unit.p1 + unit.p2 + unit.p3
     unit.hschedule = unit.h1 + unit.hr
     unit.cost = sum(map(unit.costfun, unit.mmain))
     self.TEG += unit.pschedule
     self.TTG += unit.hschedule
     self.totalcost += unit.cost
     self.constraints += [unit.mmain == unit.p1]
     self.constraints += [unit.p1 == unit.p2]
     self.constraints += [
         unit.p2 == unit.p3 / 0.8 + (unit.h1 + unit.hs) / 4.66
     ]
     self.constraints += [unit.p3 / 0.8 >= unit.p2 * 0.15]
     self.constraints += [unit.p1 >= 0]
     self.constraints += [unit.p2 >= 0]
     self.constraints += [unit.p3 >= 0]
     self.constraints += [unit.h1 >= 0]
     self.constraints += [unit.hr >= 0, unit.hr <= unit.hrmax]
     self.constraints += [unit.hs >= 0, unit.hs <= unit.hsmax]
     self.constraints += [
         unit.H[0] == unit.Hmax / 2, unit.H >= 0, unit.H <= unit.Hmax
     ]
     for i in range(self.T):
         self.constraints += [
             unit.H[i + 1] == unit.H[i] * (1 - unit.Hloss) +
             (unit.hs[i] - unit.hr[i])
         ]
     self.constraints += [
         unit.pschedule >= unit.pbound[0] * unit.isopen,
         unit.pschedule <= unit.pbound[1] * unit.isopen
     ]
예제 #10
0
    'C:/Users/J/Desktop/Businesses/Meal_Maker/Scraped_Data/combined_nutrition_small/nutrition_sm_processed_ss.csv',
    encoding='ISO-8859-1')

num_reqs_df = pd.read_csv("../desktop-meal-maker/num_reqs_df.csv")

df['p_accept'] = .5 + np.random.rand(len(df), 1) * .01

#meals_df=df[df.food_type_grp=='grocery']
meals_df = df[df.food_type_grp == 'grocery']
del df
reqs_df = num_reqs_df
oth_relax = .01
P = 10
mac_relax = 0.03

x = cvxpy.Bool(len(meals_df))

m = meals_df['p_accept'].as_matrix().astype(float)  # p(acceptance)
# Setting constraint variables
c = meals_df['carb_g'].as_matrix().astype(float)  # carbs
f = meals_df['fat_g'].as_matrix().astype(float)  # fat
p = meals_df['protein_g'].as_matrix().astype(float)  # protein
'''
ca = meals_df['calcium_mg'].as_matrix() # calcium
i = meals_df['iron_mg'].as_matrix() # iron
s = meals_df['sodium_mg'].as_matrix() # sodium
v_a = meals_df['vit_a_mcg'].as_matrix() # vitamin A
v_c = meals_df['vit_c_mg'].as_matrix() # vitamin C
ch = meals_df['cholesterol_mg'].as_matrix() # cholesterol
f = meals_df['fiber_g'].as_matrix() # fiber
sa = meals_df['saturated_fat_g'].as_matrix() # saturated fat
예제 #11
0
파일: landing.py 프로젝트: takeoffstd/lcvx
    def __init__(self, agl, mintime, N, micp=False):
        """
        Parameters
        ----------
        agl : float
            Initial altitude above ground level (AGL).
        mintime : bool
            If True, use a minimum time cost, otherwise minimum fuel.
        N : int, optional
            Number of time nodes for discretization.
        micp : bool, optional
            Set to ``True`` to solve the problem via mixed-integer programming.
        """
        super(Lander, self).__init__()

        if micp:
            cvx_opts = dict(solver=cvx.GUROBI, verbose=False, LogFile='')
        else:
            cvx_opts = dict(solver=cvx.ECOS, verbose=False)

        # Physical parameters
        self.omega = 2 * np.pi / (24 * 3600 + 39 * 60 + 35
                                  )  # [rad/s] Mars spin
        self.mass = 1700.  # [kg] Lander mass
        self.g = 3.71  # [m/s^2] Mars surface gravity
        self.R = 3396.2e3  # [m] Mars radius
        #Tmax = 21500. # [N] Maximum thrust
        #amax = Tmax/self.mass; # [m/s^2] Maximum acceleration
        self.rho1 = [
            4, 8
        ]  #[s_*amax for s_ in [0.2,0.6]] # [m/s^2] Smallest  acceleration
        self.rho2 = [
            8, 12
        ]  #[s_*amax for s_ in [0.6,0.9]] # [m/s^2] Largest control acceleration
        self.gs = np.deg2rad(10)  # [rad] Smallest glideslope angle

        # Boundary conditions
        r0 = np.array([1500., agl])
        v0 = np.array([50., -70.])
        rf = np.array([0., 0.])
        vf = np.array([0., 0.])

        # Thruster layout
        self.theta = [120.,
                      10.]  # [rad] Gimbal angles of [low,high] thrust modes
        cone_parameters = [
            dict(alpha=theta, roll=0, twod=True) for theta in self.theta
        ]
        self.C = [tools.make_cone(**param) for param in cone_parameters]
        eps = np.sqrt(np.finfo(np.float64).eps)  # Machine epsilon
        for i in range(len(self.C)):
            # Clean up small coefficients
            self.C[i][np.abs(self.C[i]) < eps] = 0
        self.M = len(self.C)  # Number of thrusters
        self.K = 1  # How many thrusters can be simultaneously active

        # Setup dynamical system
        S = np.array([[0, 1], [-1, 0]])
        Ac = np.block([[np.zeros((2, 2)), np.eye(2)],
                       [self.omega**2 * np.eye(2), 2 * self.omega * S]])
        Bc = np.row_stack([np.zeros((2, 2)), np.eye(2)])
        wc = np.row_stack([np.zeros((3, 1)), self.omega**2 * self.R - self.g])
        self.A, self.B, self.w = Ac, Bc, wc
        nx, nu = Ac.shape[1], Bc.shape[1]
        A = cvx.Parameter(nx, nx)
        B = cvx.Parameter(nx, nu)
        w = cvx.Parameter(nx, 1)

        # Scaling
        Dx = np.concatenate(np.abs([r0, v0]))
        Dx[Dx == 0] = 1
        Dx[0] = r0[1] * np.tan(np.pi / 2 - self.gs)
        self.Dx = np.diag(Dx)
        self.Du = [np.diag([rho2 for _ in range(nu)]) for rho2 in self.rho2]
        self.tfmax = 100.

        # Optimization problem common parts
        self.N = N  # Temporal solution
        x = [cvx.Parameter(nx)
             ] + [self.Dx * cvx.Variable(nx) for _ in range(1, self.N + 1)]
        xi = [cvx.Parameter()] + [cvx.Variable() for _ in range(1, self.N + 1)]
        u = [[self.Du[i] * cvx.Variable(nu) for __ in range(self.N)]
             for i in range(self.M)]
        sigma = [cvx.Variable(self.N) for _ in range(self.M)]
        gamma = [
            cvx.Bool(self.N) if micp else cvx.Variable(self.N)
            for _ in range(self.M)
        ]
        dt = cvx.Parameter()

        # Cost components
        ximax = self.tfmax * np.max(self.rho2)
        Dxi = la.inv(self.Dx)
        time_penalty = dt * self.N * ximax / self.tfmax
        input_penalty = xi[-1]
        wx = 1e-3 * ximax
        state_penalty = sum([
            cvx.abs(dt * Dxi[0, 0] * x[k][0]) +
            1e-3 * cvx.abs(dt * Dxi[1, 1] * x[k][1]) for k in range(self.N + 1)
        ])

        if mintime:
            self.zeta = 0
            cost = time_penalty + wx * state_penalty
        else:
            self.zeta = 1
            cost = input_penalty + wx * state_penalty

        self.constraints = []
        self.dual2idx = dict()

        def add_constraint(new_constraints, dual):
            """
            Add constraint(s).
            
            Parameters
            ----------
            new_constraints : list
                List of constraints to add.
            dual : str
                Dual variable name.
            """
            idx_start = len(self.constraints)
            self.constraints += new_constraints
            idx_end = len(self.constraints)
            if dual not in self.dual2idx:
                self.dual2idx[dual] = range(idx_start, idx_end)
            else:
                self.dual2idx[dual] += range(idx_start, idx_end)

        add_constraint([
            x[k + 1] == A * x[k] + B * sum([u[i][k]
                                            for i in range(self.M)]) + w
            for k in range(self.N)
        ], 'lambda')
        add_constraint([
            xi[k + 1] == xi[k] + sum([dt * sigma[i][k] for i in range(self.M)])
            for k in range(self.N)
        ], 'eta')
        x[0].value = np.concatenate([r0, v0])
        xi[0].value = 0
        add_constraint([x[-1] == np.concatenate([rf, vf])], 'nu_xN')
        for i in range(self.M):
            add_constraint(
                [cvx.norm2(u[i][k]) <= sigma[i][k] for k in range(self.N)],
                'lambda_1_%d' % (i + 1))
            add_constraint([
                gamma[i][k] * self.rho1[i] <= sigma[i][k]
                for k in range(self.N)
            ], 'lambda_2_%d' % (i + 1))
            add_constraint([
                sigma[i][k] <= gamma[i][k] * self.rho2[i]
                for k in range(self.N)
            ], 'lambda_3_%d' % (i + 1))
            if not micp:
                add_constraint([gamma[i][k] >= 0 for k in range(self.N)],
                               'dummy_1_%d' % (i + 1))
                add_constraint([gamma[i][k] <= 1 for k in range(self.N)],
                               'dummy_2_%d' % (i + 1))
            add_constraint([self.C[i] * u[i][k] <= 0 for k in range(self.N)],
                           'lambda_4_%d' % (i + 1))
        add_constraint([
            sum([gamma[i][k] for i in range(self.M)]) <= self.K
            for k in range(self.N)
        ], 'dummy_3_%d' % (i + 1))
        self.Ex = np.column_stack([np.eye(2), np.zeros((2, 2))])
        gs = np.pi / 2 - self.gs
        add_constraint([
            np.array([0, 1]).dot(self.Ex) * x[k] >=
            cvx.norm2(self.Ex * x[k]) * np.cos(gs) for k in range(self.N)
        ], 'dummy_4')

        # Problem oracle
        problem = cvx.Problem(cvx.Minimize(cost), self.constraints)

        def extract_variables():
            primal = dict()
            dual = dict()
            misc = dict()
            # Primal variables
            primal['x'] = np.column_stack(
                [tools.cvx2arr(x[k]) for k in range(self.N + 1)])
            primal['u'] = [
                np.column_stack(
                    [tools.cvx2arr(u[i][k]) for k in range(self.N)])
                for i in range(self.M)
            ]
            primal['sigma'] = [
                np.array([sigma[i][k].value for k in range(self.N)])
                for i in range(self.M)
            ]
            primal['gamma'] = [
                np.array([gamma[i][k].value for k in range(self.N)])
                for i in range(self.M)
            ]
            # Dual variables
            if not micp:
                for name in self.dual2idx.keys():
                    dual[name] = np.column_stack([
                        tools.cvx2arr(self.constraints[k], dual=True)
                        for k in self.dual2idx[name]
                    ])
                # Other (derived) variables
                misc['y'] = np.row_stack([
                    self.Bd.T.dot(dual['lambda'][:, k]) for k in range(self.N)
                ])
            return primal, dual, misc

        def solve(tf):
            dt.value = tf / float(self.N)
            A.value, B.value = tools.discretize(Ac, Bc, dt.value)
            ___, w.value = tools.discretize(Ac, wc, dt.value)
            self.Ad = np.array(A.value)
            self.Bd = np.array(B.value)
            self.wd = np.array(w.value)

            t = np.array([k * dt.value for k in range(self.N + 1)])

            try:
                J = problem.solve(**cvx_opts)
                solver_time = problem.solver_stats.solve_time
                if problem.status == 'infeasible':
                    return problem.status, np.inf, t, None, None, None, solver_time
                else:
                    # All good, return solution
                    primal, dual, misc = extract_variables()
                    return problem.status, J, t, primal, dual, misc, solver_time
            except cvx.SolverError:
                return 'error', np.inf, t, None, None, None, 0.

        self.solve = lambda tf: solve(tf)
예제 #12
0
w_data = []
# Trend
t_data = []

for rider in riders:
    v_data.append(float(rider['value']))
    w_data.append(0.01 * float(rider['popularity']))
    t_data.append(float(rider['growth_tot']))

v = np.array(v_data)
w = np.array(w_data)
t = np.array(t_data)

print t

x = cvx.Bool(len(riders))

popularity = cvx.Variable()
cost = cvx.Variable()
growth = cvx.Variable()

objective = cvx.Maximize(popularity)

constraints = []

constraints.append(popularity == w * x)
constraints.append(growth == t * x)
constraints.append(cost == v * x)
constraints.append(cost <= 62100000)
constraints.append(sum(x) == 9)
예제 #13
0
    used = set(x for l in lineups_unique for x in l)
    for id in used:
        n = sum(list(l).count(id) for l in lineups_unique)
        id_count[id] = n
        id_prop[id] = float(n) / float(len(lineups_unique))

    print 'setting exposure limits'

    for k in id_prop.keys():
        pos = pos_dict[k]['pos']
        if id_prop[k] > MAX_EXPOSURE[pos]:

            player_pool = player_pool.loc[player_pool['pool_id'] != k]

    costs = np.array(player_pool.loc[:, 'cst_fd']).astype(int)
    selection = cvxpy.Bool(len(costs))

    new_lineup = False

    while new_lineup == False:

        # get indices of players to select
        constraints = get_constraints(n_start,
                                      roster_todraft,
                                      player_pool,
                                      budget,
                                      selection,
                                      costs,
                                      team_limit=False)
        sel_index = get_selection_indices(player_pool, selection, constraints,
                                          n_start)
예제 #14
0
Created on Mon Aug 13 10:59:26 2018

@author: "Anirban Das"
"""

import cvxpy
import numpy as np

# The data for the Knapsack problem
# P is total weight capacity of sack
# weights and utilities are also specified
P = 165
weights = np.array([23, 31, 29, 44, 53, 38, 63, 85, 89, 82])
utilities = np.array([92, 57, 49, 68, 60, 43, 67, 84, 87, 72])

# The variable we are solving for
selection = cvxpy.Bool(len(weights))

# The sum of the weights should be less than or equal to P
weight_constraint = weights * selection <= P

# Our total utility is the sum of the item utilities
total_utility = utilities * selection

# We tell cvxpy that we want to maximize total utility 
# subject to weight_constraint. All constraints in 
# cvxpy must be passed as a list
knapsack_problem = cvxpy.Problem(cvxpy.Maximize(total_utility), [weight_constraint])

# Solving the problem
knapsack_problem.solve(solver=cvxpy.GLPK_MI)
예제 #15
0
    def __init__(self):
        # Data
        # ----
        # Definition of the STN (order in the list matters!)
        self.units = ['Heater', 'Reactor 1', 'Reactor 2', 'Column']
        self.tasks = ['Heat', 'Rea. 1', 'Rea. 2', 'Rea. 3', 'Sep.']
        self.states = ['Feed A', 'Feed B', 'Feed C', 'Hot A', 'Intermediate AB',
                       'Intermediate BC', 'Impure E', 'Product 1', 'Product 2']
        self.input_states = [0, 1, 2]  # indices of the input states
        self.horizon = 11  # horizon in number of of steps

        # Aliases
        self.I = self.units.__len__()
        self.J = self.tasks.__len__()
        self.S = self.states.__len__()
        self.T = self.horizon

        # Units capability (what tasks can be processed on which unit)
        # row = unit, column = task
        self.J_i = np.array([[1, 0, 0, 0, 0],
                             [0, 1, 1, 1, 0],
                             [0, 1, 1, 1, 0],
                             [0, 0, 0, 0, 1]])

        # Recipes and timing
        # fractions of input state s (column) required to execute task j (row)
        self.rho_in = np.array([[1,   0,   0,   0,   0,   0,   0,   0,   0],
                                [0,   0.5, 0.5, 0,   0,   0,   0,   0,   0],
                                [0,   0,   0,   0.4, 0,   0.6, 0,   0,   0],
                                [0,   0,   0.2, 0,   0.8, 0,   0,   0,   0],
                                [0,   0,   0,   0,   0,   0,   1,   0,   0]])
        # fractions of output state s (column) produced by task j (row)
        self.rho_out = np.array([[0,   0,   0,   1,   0,   0,   0,   0,   0],
                                 [0,   0,   0,   0,   0,   1,   0,   0,   0],
                                 [0,   0,   0,   0,   0.6, 0,   0,   0.4, 0],
                                 [0,   0,   0,   0,   0,   0,   1,   0,   0],
                                 [0,   0,   0,   0,   0.1, 0,   0,   0,   0.9]])
        # time (in # of steps) required to produce output state s (column) from task j (row)
        self.P = np.array([[0,   0,   0,   1,   0,   0,   0,   0,   0],
                           [0,   0,   0,   0,   0,   2,   0,   0,   0],
                           [0,   0,   0,   0,   2,   0,   0,   2,   0],
                           [0,   0,   0,   0,   0,   0,   1,   0,   0],
                           [0,   0,   0,   0,   2,   0,   0,   0,   1]])
        # total execution time of task j (row-wise max of P)
        self.P_j = np.amax(self.P, axis=1)

        # Capacities
        # max capacity of unit i (row) to process task j (column), in e.g. kg
        self.V_max = np.array([[100, 100, 100, 100, 100],
                               [80,  80,  80,  80,  80],
                               [50,  50,  50,  50,  50],
                               [200, 200, 200, 200, 200]])
        self.V_min = np.array([[0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0]])
        # storage capacity for state s
        self.C_max = np.array([np.infty, np.infty, np.infty, 100, 200, 150, 100, np.infty, np.infty])
        self.C_min = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0])

        # objective to maximize (revenue from the states)
        self.c = np.array([0, 0, 0, -1, -1, -1, -1, 10, 10])

        # Optimization problem structure (cvx.Problem type)
        self.model = 0

        # Optimization Variables
        # ----------------------
        self.x_ijt = {}  # allocation variable (bool)
        self.y_ijt = {}  # batch size [kg]
        self.y_s = {}   # state quantity [kg]
        for i in range(self.I):
            for j in range(self.J):
                for t in range(self.T):
                    self.x_ijt[i,j,t] = cvx.Bool()
        # we separate creation of x_ijt and y_ijt in two loops, so that the optimization variables
        # in the standard model do not get mixed up
        for i in range(self.I):
            for j in range(self.J):
                for t in range(self.T):
                    self.y_ijt[i,j,t] = cvx.Variable()
        # state equations are eliminated to allow for the robust counterpart. We only store
        # the initial state y_s(t=0), which we name y_s
        for s in range(self.S):
            self.y_s[s] = cvx.Variable()
        # auxiliary expressions used in the state equations
        self.y_st_inflow = {}
        self.y_st_outflow = {}

        # Attributes
        # ----------
        # to store results
        self.X_ijt = np.zeros((self.I,self.J,self.T))
        self.Y_ijt = np.zeros((self.I,self.J,self.T))
        self.Y_st = np.zeros((self.S,self.T))
        self.Y_st_inflow = np.zeros((self.S,self.T))
        self.Y_st_outflow = np.zeros((self.S,self.T))
        # to store the standard model
        # min   c_x'*x + c_y'*y
        # s.t.  A_eq*x + B_eq*y = b_eq
        #       A_ineq*x + B_ineq*y <= b_ineq
        #       x \in {0,1}
        self.c_x = 0
        self.c_y = 0
        self.A_eq = 0
        self.A_ineq = 0
        self.B_eq = 0
        self.B_ineq = 0
        self.b_eq = 0
        self.b_ineq = 0
        self.bool_ix = 0
        self.cont_ix = 0
        self.m_eq = 0
        self.m_ineq = 0
    obsVerts.append(np.asarray([[coord_of_obst_list[i][0], coord_of_obst_list[i][0], coord_of_obst_list[i][2], coord_of_obst_list[i][2], coord_of_obst_list[i][0]], [coord_of_obst_list[i][1], coord_of_obst_list[i][3], coord_of_obst_list[i][3], coord_of_obst_list[i][1], coord_of_obst_list[i][1]]]))

# Defining the system matrices
A = np.matrix('1,0,1,0;0,1,0,1;0,0,1,0;,0,0,0,1')
B = np.matrix('0.5,0;0,0.5;1,0;0,1')

#Defining the buffer
buffer = np.matrix('1;1;1;1')
buffer = buffer/(precision*2*np.sqrt(2))

# Defining the decision variables
X = cvx.Variable(4,N+1)
U = cvx.Variable(2,N)

#Defining the Boolean slack variables
b_p = cvx.Bool(4*num_obst,(precision-1)*(N))
b_p1 = cvx.Bool(4*num_obst,N)
b_p2 = cvx.Bool(4*num_obst,N)
b_qq = cvx.Bool(3)

# Big-M
M = 2000

# Define dynamic constraints

## Initial condition
con = [X[:,0] == np.matrix('0;0;0;0')]
## Dynamics
con.extend([X[:,i+1] == A*X[:,i] + B*U[:,i] for i in range(N)])
## Input constraints
con.extend([cvx.norm(U[:,i],np.inf) <= max_inp for i in range(N)])
def optimize(n_cluster,
             error_cons,
             pred_file,
             label_file,
             solution_file,
             ifInt=0,
             obj='balance',
             singleCluster=False):
    # Problem data.
    n_class = 1000

    #mapping = np.genfromtxt('9cluster.csv',delimiter=',')
    #ori_label = np.genfromtxt('label_squeezenet_9cluster.csv', delimiter=',')
    ori_pred = np.genfromtxt(pred_file, delimiter=',')
    ori_top1_pred = ori_pred.argmax(1)
    class_label = np.genfromtxt(label_file, delimiter=',')
    ori_top1_pred, class_label = map_to_cluster(ori_top1_pred, class_label)

    # Construct the problem.
    if ifInt:
        P = cvx.Bool(n_cluster, n_class)
        constraints = []
        use_solver = cvx.GUROBI
    else:
        P = cvx.Variable(n_cluster, n_class)
        constraints = [0 <= P, P <= 1]
        use_solver = cvx.ECOS

    if singleCluster:
        constraints.append(cvx.sum_entries(P, axis=0) <= 1)

    # Objective function
    if obj == 'sum':
        obj_func = cvx.sum_entries(P)
    elif obj == 'sum_sq':
        obj_func = cvx.sum_squares(cvx.sum_entries(P, axis=1))
    elif obj == 'max':
        obj_func = cvx.max_entries(cvx.sum_entries(P, axis=1))
    elif obj == 'max_sq':
        obj_func = cvx.max_entries(cvx.power(cvx.sum_entries(P, axis=1), 2))
    else:
        obj_func = cvx.sum_squares(
            cvx.sum_entries(P, axis=1) - n_class / float(n_cluster))

    objective = cvx.Minimize(obj_func)

    # Error rate constraint
    error_constraint = 0
    for cluster, label in zip(ori_top1_pred, class_label):
        error_constraint += P[cluster, label]
    error_constraint = 1.0 - 1.0 / len(class_label) * error_constraint
    constraints.append(error_constraint <= error_cons)

    # The optimal objective is returned by prob.solve().
    prob = cvx.Problem(objective, constraints)
    result = prob.solve(solver=use_solver)

    save_mat(solution_file, P.value)

    print 'status:', prob.status
    print 'optimal value: ', prob.value
    print 'Optimal P matrix:', P.value
    print 'Error rate constraint dual value: ', constraints[-1].dual_value
    print 'Error rate constraint {0}:{1} <= {2}'.format(
        constraints[-1].value, error_constraint.value, error_cons)
예제 #18
0
        gt_rels = result_dict.get("gt_rels", None) 
        gt_pms = result_dict.get("gt_pms", None)
        pred_dsms = result_dict.get("pred_dsms", None)
	diffs = result_dict.get("sal_map", None)
        seq_len = np.sqrt(gt_pms.shape[1])
        assert round(seq_len) == seq_len
        seq_len = int(seq_len)
        print("\n>>> Evaluating file {} whose sequence lenght is {} <<<".format(result_file, seq_len))
        print("GT relevance shape: {}".format(gt_rels.shape))
        print("GT PMs Mtrices: {}".format(gt_pms.shape))
        print("Pred DSMs Mtrices: {}".format(pred_dsms.shape))

        # Create the permutation matrix approximation problem		
        rows, cols, n, samples = seq_len, seq_len, pow(seq_len, 2), gt_pms.shape[0]
        pred_pms = np.zeros(pred_dsms.shape, np.int)
        var = cvx.Bool(n)
        par = cvx.Parameter(n)
        Rnorm =  np.fromfunction(lambda i, j: j//cols == i, (rows, n), dtype=int).astype(np.float)
        Cnorm =  np.hstack([np.identity(cols) for c in range(cols)]).astype(np.float)       
        A = np.vstack((Rnorm, Cnorm))
        prob = cvx.Problem(cvx.Minimize(cvx.norm(var - par)), [A * var == 1])

        # solve
        for idx, dsm in enumerate(pred_dsms):            
            if idx % 100 == 0:
                print(os.getpid())
                print("{} samples processed".format(idx))

            try:
                par.value = dsm
                prob.solve()
예제 #19
0
    def __init__(self, micp=False):
        """
        Parameters
        ----------
        micp : bool, optional
            Set to ``True`` to solve the problem via mixed-integer programming.
        """
        super(Docker, self).__init__()

        if micp:
            cvx_opts = dict(solver=cvx.GUROBI, verbose=False, LogFile='')
        else:
            cvx_opts = dict(solver=cvx.ECOS, verbose=False)


#        cvx_opts = dict(solver=cvx.GUROBI,verbose=False,Presolve=1,LogFile='',threads=1)

# Physical parameters
        self.omega = np.array(
            [0. * 2 * np.pi / 60., 0. * 2 * np.pi / 60.,
             1. * 2 * np.pi / 60.])  # [rad/s] Space station spin
        self.rho1 = 1e-3  # [m/s^2] Smallest control acceleration
        self.rho2 = 1e-2  # [m/s^2] Largest control acceleration

        # Boundary conditions
        r0 = np.array([5., 5., 1e2])
        v0 = np.array([0., 0., 0.])  #-np.cross(self.omega,r0)
        rf = np.array([0., 0., 0.])
        vf = np.array([0., 0., -1e-2])

        # RCS layout
        cone_angle = 0.  # [deg] Thruster cone angle
        theta = phi = 30  # [deg] Basic pitch, roll for lower thrusters
        theta_up = phi_up = 40  # [deg] Basic pitch, roll for upper thrusters
        cone_parameters = [
            dict(alpha=cone_angle, roll=phi_up, pitch=theta_up, yaw=0),
            dict(alpha=cone_angle, roll=-phi_up, pitch=theta_up, yaw=0),
            dict(alpha=cone_angle, roll=-phi_up, pitch=-theta_up, yaw=0),
            dict(alpha=cone_angle, roll=phi_up, pitch=-theta_up, yaw=0),
            dict(alpha=cone_angle,
                 roll=phi + (180 - 2 * phi),
                 pitch=-theta,
                 yaw=0),
            dict(alpha=cone_angle,
                 roll=-phi - (180 - 2 * phi),
                 pitch=-theta,
                 yaw=0),
            dict(alpha=cone_angle,
                 roll=-phi - (180 - 2 * phi),
                 pitch=theta,
                 yaw=0),
            dict(alpha=cone_angle,
                 roll=phi + (180 - 2 * phi),
                 pitch=theta,
                 yaw=0),
            dict(alpha=cone_angle, roll=90, pitch=0, yaw=0),
            dict(alpha=cone_angle, roll=-90, pitch=0, yaw=0),
            dict(alpha=cone_angle, roll=0, pitch=90, yaw=0),
            dict(alpha=cone_angle, roll=0, pitch=-90, yaw=0)
        ]
        self.C = [tools.make_cone(**param) for param in cone_parameters]
        eps = np.sqrt(np.finfo(np.float64).eps)  # Machine epsilon
        for i in range(len(self.C)):
            # Clean up small coefficients
            self.C[i][np.abs(self.C[i]) < eps] = 0
        self.M = len(self.C)  # Number of thrusters
        self.K = 4  # How many thrusters can be simultaneously active

        # Setup dynamical system
        S = lambda w: np.array([[0, -w[2], w[1]], [w[2], 0, -w[0]],
                                [-w[1], w[0], 0]])
        Ac = np.block([[np.zeros((3, 3)), np.eye(3)],
                       [-mpow(S(self.omega), 2), -2 * S(self.omega)]])
        Bc = np.row_stack([np.zeros((3, 3)), np.eye(3)])
        self.A, self.B = Ac, Bc
        nx, nu = Ac.shape[1], Bc.shape[1]
        A = cvx.Parameter(nx, nx)
        B = cvx.Parameter(nx, nu)

        # Scaling
        Dx = np.concatenate(np.abs([r0, v0]))
        Dx[Dx == 0] = 1
        self.Dx = np.diag(Dx)
        self.Du = np.diag([self.rho2 for _ in range(nu)])

        # Optimization problem common parts
        self.N = 300  # Temporal solution
        x = [cvx.Parameter(nx)
             ] + [self.Dx * cvx.Variable(nx) for _ in range(1, self.N + 1)]
        xi = [cvx.Parameter()] + [cvx.Variable() for _ in range(1, self.N + 1)]
        u = [[self.Du * cvx.Variable(nu) for __ in range(self.N)]
             for _ in range(self.M)]
        unorm = [cvx.Variable(self.N) for _ in range(self.M)]
        sigma = [cvx.Variable(self.N) for _ in range(self.M)]
        gamma = [
            cvx.Bool(self.N) if micp else cvx.Variable(self.N)
            for _ in range(self.M)
        ]
        dt = cvx.Parameter()

        cost = dt * self.N  # minimum time: dt

        self.constraints = []
        self.dual2idx = dict()

        def add_constraint(new_constraints, dual):
            """
            Add constraint(s).
            
            Parameters
            ----------
            new_constraints : list
                List of constraints to add.
            dual : str
                Dual variable name.
            """
            idx_start = len(self.constraints)
            self.constraints += new_constraints
            idx_end = len(self.constraints)
            if dual not in self.dual2idx:
                self.dual2idx[dual] = range(idx_start, idx_end)
            else:
                self.dual2idx[dual] += range(idx_start, idx_end)

        add_constraint([
            x[k + 1] == A * x[k] + B * sum([u[i][k] for i in range(self.M)])
            for k in range(self.N)
        ], 'nu_x')
        add_constraint([
            xi[k + 1] == xi[k] + dt * sum([sigma[i][k] for i in range(self.M)])
            for k in range(self.N)
        ], 'nu_xi')
        x[0].value = np.concatenate([r0, v0])
        xi[0].value = 0
        add_constraint([x[-1] == np.concatenate([rf, vf])], 'nu_xN')
        for i in range(self.M):
            add_constraint([unorm[i][k] <= sigma[i][k] for k in range(self.N)],
                           'lambda_sigma')
            add_constraint([
                u[i][k] == -unorm[i][k] * self.C[i][-1] for k in range(self.N)
            ], 'lambda_unorm')
            add_constraint([
                gamma[i][k] * self.rho1 <= sigma[i][k] for k in range(self.N)
            ], 'lambda_rho1')
            add_constraint([
                sigma[i][k] <= gamma[i][k] * self.rho2 for k in range(self.N)
            ], 'lambda_rho2')
            if not micp:
                add_constraint([gamma[i][k] >= 0 for k in range(self.N)],
                               'lambda_gamma_low')
                add_constraint([gamma[i][k] <= 1 for k in range(self.N)],
                               'lambda_gamma_high')
            add_constraint([self.C[i] * u[i][k] <= 0 for k in range(self.N)],
                           'lambda_u')
        add_constraint([
            sum([gamma[i][k] for i in range(self.M)]) <= self.K
            for k in range(self.N)
        ], 'lambda_sum_gamma')

        # Problem 2 oracle
        problem = cvx.Problem(cvx.Minimize(cost), self.constraints)

        def extract_variables():
            primal = dict()
            dual = dict()
            misc = dict()
            # Primal variables
            primal['x'] = np.column_stack(
                [tools.cvx2arr(x[k]) for k in range(self.N + 1)])
            primal['u'] = [
                np.column_stack(
                    [tools.cvx2arr(u[i][k]) for k in range(self.N)])
                for i in range(self.M)
            ]
            primal['sigma'] = [
                np.array([sigma[i][k].value for k in range(self.N)])
                for i in range(self.M)
            ]
            primal['gamma'] = [
                np.array([gamma[i][k].value for k in range(self.N)])
                for i in range(self.M)
            ]
            # Dual variables
            if not micp:
                for name in self.dual2idx.keys():
                    dual[name] = np.column_stack([
                        tools.cvx2arr(self.constraints[k], dual=True)
                        for k in self.dual2idx[name]
                    ])
                # Other (derived) variables
                misc['y'] = np.row_stack(
                    [self.Bd.T.dot(dual['nu_x'][:, k]) for k in range(self.N)])
            return primal, dual, misc

        def solve(tf):
            dt.value = tf / float(self.N)
            A.value, B.value = tools.discretize(Ac, Bc, dt.value)
            self.Ad = np.array(A.value)
            self.Bd = np.array(B.value)

            t = np.array([k * dt.value for k in range(self.N + 1)])

            try:
                J = problem.solve(**cvx_opts)
                solver_time = problem.solver_stats.solve_time
                if problem.status == 'infeasible':
                    return problem.status, np.inf, t, None, None, None, solver_time
                else:
                    # All good, return solution
                    primal, dual, misc = extract_variables()
                    return problem.status, J, t, primal, dual, misc, solver_time
            except cvx.SolverError:
                return 'error', np.inf, t, None, None, None, 0.

        self.solve = lambda tf: solve(tf)
예제 #20
0
    def test_CBC_hard(self):

        num_states = 5
        x = cvxpy.Bool(num_states,name='x')
        sw_on = cvxpy.Bool(num_states,name='sw_on')
        sw_off = cvxpy.Bool(num_states,name='sw_off')
        sw_stay_on = cvxpy.Bool(num_states,name='sw_stay_on')
        sw_stay_off = cvxpy.Bool(num_states,name='sw_stay_off')
        fl = cvxpy.Variable(num_states,name='float')

        constr = []

        # can only be one transition type
        constr.append(sw_on*1.0 + sw_off*1.0 + sw_stay_on*1.0 + sw_stay_off*1.0 == 1)

        for i in range(num_states):
            # if switching on, must be now on
            constr.append(x[i] >= sw_on[i])

            # if switchin on, must have been off previously
            if i>0:
                constr.append((1-x[i-1]) >= sw_on[i])

            # if switching off, must be now off
            constr.append((1-x[i]) >= sw_off[i])

            # if switchin off, must have been on previously
            if i>0:
                constr.append(x[i-1] >= sw_off[i])

            # if staying on, must be now on
            constr.append(x[i] >= sw_stay_on[i])

            # if staying on, must have been on previously
            if i>0:
                constr.append(x[i-1] >= sw_stay_on[i])

            # if staying, must be now off
            constr.append((1-x[i]) >= sw_stay_off[i])

            # if staying off, must have been off previously
            if i>0:
                constr.append((1-x[i-1]) >= sw_stay_off[i])


        # random stuff
        constr.append(x[1] == 1)
        constr.append(x[3] == 0)
        for i in range(num_states):
            constr.append(fl[i] <= i*sw_on[i])
            constr.append(fl[i] >= -i)


        obj = cvxpy.Maximize(sum(sw_off) + sum(sw_on))
        for i in range(num_states):
            if i%2 == 0:
                obj += cvxpy.Maximize(fl[i])
            else:
                obj += cvxpy.Maximize(-1*fl[i])
        problem = cvxpy.Problem(obj, constr)
        ret = problem.solve(solver=cvxpy.CBC)
        self.assertTrue(problem.status in [cvxpy.OPTIMAL, cvxpy.OPTIMAL_INACCURATE])

        print(' | '.join(['i','x','sw_on','sw_off','stay_on','stay_off','float']))
        for i in range(num_states):
            row = ' | '.join([
                '%1d' % i,
                '%1d' % int(round(x[i].value)),
                '%5d' % int(round(sw_on[i].value)),
                '%6d' % int(round(sw_off[i].value)),
                '%7d' % int(round(sw_stay_on[i].value)),
                '%8d' % int(round(sw_stay_off[i].value)),
                '%f' % fl[i].value
            ])
            print(row)
    def __init__(self,
                 d,
                 dataset_name=None,
                 X=None,
                 use_slab=False,
                 constrain_max_loss=False,
                 use_l2=False,
                 x_pos_tuple=None,
                 x_neg_tuple=None,
                 model_type='svm'):

        self.use_slab = use_slab
        self.constrain_max_loss = constrain_max_loss
        self.use_l2 = use_l2
        self.dataset = dataset_name
        # creating variables with shape of "d"
        # need to deal with some binary feature attibutes
        if dataset_name == "adult":
            # ---- adult -----
            # attributes 0-3 are continuous and represent: "age", "capital-gain", "capital-loss", "hours-per-week"
            # attributes 4-11 are binary representing the work class.
            # attributes 12-26: education background
            # attributes 27-32: martial status
            # attributes 33-46: occupation
            # attributes 47-51: relationship
            # 52-56: race

            # bool_inds = np.array([0]*d)
            # bool_inds[4:] = bool_inds[4:] + 1
            # print("the bolean indices are:",tuple(bool_inds))
            # self.cvx_x_pos = cvx.Variable(d, boolean = tuple([bool_inds]))
            self.cvx_x_pos_real = cvx.Variable(4)
            self.cvx_x_neg_real = cvx.Variable(4)
            self.cvx_x_pos_binary = cvx.Bool(d - 4)
            self.cvx_x_neg_binary = cvx.Bool(d - 4)
            # used for the binary constraints
            arr = np.array([0] * (d - 4))
            arr[0:8] = 1
            self.cvx_work_class = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[8:23] = 1
            self.cvx_education = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[23:29] = 1
            self.cvx_martial = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[29:43] = 1
            self.cvx_occupation = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[43:48] = 1
            self.cvx_relationship = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[48:53] = 1
            self.cvx_race = cvx.Parameter(d - 4, value=arr)
        else:
            self.cvx_x_pos = cvx.Variable(d)
            self.cvx_x_neg = cvx.Variable(d)

        self.cvx_g = cvx.Parameter(d)
        self.cvx_theta = cvx.Parameter(d)
        self.cvx_bias = cvx.Parameter(1)
        self.cvx_epsilon_pos = cvx.Parameter(1)
        self.cvx_epsilon_neg = cvx.Parameter(1)

        self.cvx_centroid_pos = cvx.Parameter(d)
        self.cvx_centroid_neg = cvx.Parameter(d)

        if use_l2:
            self.cvx_sphere_radius_pos = cvx.Parameter(1)
            self.cvx_sphere_radius_neg = cvx.Parameter(1)

        if use_slab:
            self.cvx_centroid_vec = cvx.Parameter(d)
            self.cvx_slab_radius_pos = cvx.Parameter(1)
            self.cvx_slab_radius_neg = cvx.Parameter(1)

        if constrain_max_loss:
            self.cvx_max_loss_pos = cvx.Parameter(1)
            self.cvx_max_loss_neg = cvx.Parameter(1)

        self.cvx_err = cvx.Variable(d)
        self.objective = cvx.Minimize(cvx.pnorm(self.cvx_err, 2))
        # version 0.4 is below
        if dataset_name == 'adult':
            self.constraints = [
                self.cvx_g - self.cvx_epsilon_pos * cvx.vstack(self.cvx_x_pos_real,self.cvx_x_pos_binary) +\
                     self.cvx_epsilon_neg * cvx.vstack(self.cvx_x_neg_real,self.cvx_x_neg_binary) == self.cvx_err,
                cvx_dot(self.cvx_theta, cvx.vstack(self.cvx_x_pos_real,self.cvx_x_pos_binary)) + self.cvx_bias < 1, # margin constraint, ideally should be 1
                -(cvx_dot(self.cvx_theta, cvx.vstack(self.cvx_x_neg_real,self.cvx_x_neg_binary)) + self.cvx_bias) < 1 , # ideally should be 1
            ]
        else:
            if model_type == 'svm':
                self.constraints = [
                    self.cvx_g - self.cvx_epsilon_pos * self.cvx_x_pos +
                    self.cvx_epsilon_neg * self.cvx_x_neg == self.cvx_err,
                    cvx_dot(self.cvx_theta, self.cvx_x_pos) + self.cvx_bias <
                    1,  # margin constraint, ideally should be 1
                    -(cvx_dot(self.cvx_theta, self.cvx_x_neg) + self.cvx_bias)
                    < 1,  # ideally should be 1
                ]
            # cvx 1.0 version
            # if model_type == 'svm':
            #     self.constraints = [
            #         self.cvx_g - self.cvx_epsilon_pos * self.cvx_x_pos + self.cvx_epsilon_neg * self.cvx_x_neg == self.cvx_err,
            #         cvx_dot(self.cvx_theta, self.cvx_x_pos) + self.cvx_bias <= 1, # margin constraint, ideally should be 1
            #         -(cvx_dot(self.cvx_theta, self.cvx_x_neg) + self.cvx_bias) <= 1 , # ideally should be 1
            #     ]
            elif model_type == 'lr':  # lr is not convex, cannot support it now
                pos_margin = cvx_dot(self.cvx_x_pos,
                                     self.cvx_theta) + self.cvx_bias
                neg_margin = -cvx_dot(self.cvx_x_neg,
                                      self.cvx_theta) + self.cvx_bias
                pos_grad = -sigmoid(-pos_margin) * self.cvx_x_pos
                neg_grad = sigmoid(-neg_margin) * self.cvx_x_neg
                self.constraints = [
                    self.cvx_g - self.cvx_epsilon_pos * pos_grad +
                    self.cvx_epsilon_neg * neg_grad == self.cvx_err
                ]
            else:
                print("Please use common linear classifier!")
                raise NotImplementedError

        if use_slab:
            self.constraints.append(
                cvx_dot(self.cvx_centroid_vec, self.cvx_x_pos -
                        self.cvx_centroid_pos) < self.cvx_slab_radius_pos)
            self.constraints.append(
                -cvx_dot(self.cvx_centroid_vec, self.cvx_x_pos -
                         self.cvx_centroid_pos) < self.cvx_slab_radius_pos)

            self.constraints.append(
                cvx_dot(self.cvx_centroid_vec, self.cvx_x_neg -
                        self.cvx_centroid_neg) < self.cvx_slab_radius_neg)
            self.constraints.append(
                -cvx_dot(self.cvx_centroid_vec, self.cvx_x_neg -
                         self.cvx_centroid_neg) < self.cvx_slab_radius_neg)

        if dataset_name in ['mnist_17', 'enron', 'imdb']:
            self.constraints.append(self.cvx_x_pos >= 0)
            self.constraints.append(self.cvx_x_neg >= 0)

        # additional constraints on synthetic dataset
        if x_pos_tuple:
            assert x_neg_tuple != None
            self.x_pos_min, self.x_pos_max = x_pos_tuple
            self.x_neg_min, self.x_neg_max = x_neg_tuple

            if dataset_name == 'adult':
                self.constraints.append(self.cvx_x_pos_real >= self.x_pos_min)
                self.constraints.append(self.cvx_x_pos_real <= self.x_pos_max)
                self.constraints.append(self.cvx_x_neg_real >= self.x_neg_min)
                self.constraints.append(self.cvx_x_neg_real <= self.x_neg_max)
            else:
                self.constraints.append(self.cvx_x_pos >= self.x_pos_min)
                self.constraints.append(self.cvx_x_pos <= self.x_pos_max)
                self.constraints.append(self.cvx_x_neg >= self.x_neg_min)
                self.constraints.append(self.cvx_x_neg <= self.x_neg_max)

        if constrain_max_loss:
            self.constraints.append(1 -
                                    (cvx_dot(self.cvx_theta, self.cvx_x_pos) +
                                     self.cvx_bias) < self.cvx_max_loss_pos)
            self.constraints.append(1 +
                                    (cvx_dot(self.cvx_theta, self.cvx_x_neg) +
                                     self.cvx_bias) < self.cvx_max_loss_neg)

        if dataset_name in ['mnist_17']:
            self.constraints.append(self.cvx_x_pos <= 1)
            self.constraints.append(self.cvx_x_neg <= 1)
        if dataset_name == 'adult':
            # binary featutre constraints: beacuse of one-hot encoding
            self.constraints.append(
                cvx_dot(self.cvx_work_class, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_work_class, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_education, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_education, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_martial, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_martial, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_occupation, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_occupation, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_relationship, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_relationship, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_race, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_race, self.cvx_x_neg_binary) == 1)

        # If we pass in X, do the LP/integer constraint
        if (X is not None) and (dataset_name in ['enron', 'imdb']):
            if sparse.issparse(X):
                X_max = np.max(X, axis=0).toarray().reshape(-1)
            else:
                X_max = np.max(X, axis=0).reshape(-1)
            X_max[X_max < 1] = 1
            X_max[X_max > 50] = 50

            self.constraints.append(self.cvx_x_pos <= X_max)
            self.constraints.append(self.cvx_x_neg <= X_max)
            kmax = int(np.ceil(np.max(X_max)))

            self.cvx_env_pos = cvx.Variable(d)
            self.cvx_env_neg = cvx.Variable(d)
            for k in range(1, kmax + 1):
                active = k <= (X_max)
                self.constraints.append(
                    self.cvx_env_pos[active] >= self.cvx_x_pos[active] *
                    (2 * k - 1) - k * (k - 1))
                self.constraints.append(
                    self.cvx_env_neg[active] >= self.cvx_x_neg[active] *
                    (2 * k - 1) - k * (k - 1))

            if use_l2:
                self.constraints.append(
                    (cvx.sum_entries(self.cvx_env_pos) -
                     2 * cvx_dot(self.cvx_centroid_pos, self.cvx_x_pos) +
                     cvx.sum_squares(self.cvx_centroid_pos)) < (
                         self.cvx_sphere_radius_pos**2))
                self.constraints.append(
                    (cvx.sum_entries(self.cvx_env_neg) -
                     2 * cvx_dot(self.cvx_centroid_neg, self.cvx_x_neg) +
                     cvx.sum_squares(self.cvx_centroid_neg)) < (
                         self.cvx_sphere_radius_neg**2))
        else:
            if use_l2:
                self.constraints.append(
                    cvx.pnorm(self.cvx_x_pos - self.cvx_centroid_pos, 2)**2 <
                    self.cvx_sphere_radius_pos**2)
                self.constraints.append(
                    cvx.pnorm(self.cvx_x_neg - self.cvx_centroid_neg, 2)**2 <
                    self.cvx_sphere_radius_neg**2)
예제 #22
0
def solve(n_p,
          n_d,
          n_s,
          G,
          T,
          M,
          indisp,
          forced,
          slot_choice,
          demand,
          prop=0.5,
          hist=None):
    """ Solves the Integer Programming problem that generates a schedule.
    The current constraints are, maximum of one man per slot...

    Args:
        n_p (int): the number of people in the list
        n_d (int): the number of days being considered
        n_s (int): the number of slots of work per day
        G (ndarray): A np array (colum vector) of 1s and 0s representing
        the gender of each person in the list, 1 for man and 0 for woman
        T (ndarray): A np array (colum vector) representing the teacher
        status of each person in the list, 1 for teacher and 0 for auxiliar
        M (ndarray): A np array (colum vector) representing the maturity
        status of each person in the list, 1 for mature and 0 otherwise
        indisp (list): List of tuples corresponding to the the date
        indisponibilities in the form (person, day)
        forced (list): Similarly to indisp, this list contains the tuples
        of people that we want to force to be present in a given slot in the
        solution. In this case the tuples are (person, day*n_s+slot).
        slot_choice (ndarray): A somewhat dense matrix of shape (n_p, n_s)
        representing the disponibility of each person to work on each slot.
        1 means available, 0 otherwise
        demand (ndarray): A (n_d*n_s) column vector of the demand of people
        for each slot.
        prop (float): Maximum proportion of men in each slot

    Returns:
        solution (ndarray): A matrix of shape (n_p, n_d*n_s) where xij = 1
        represents a person p working on day j//n_s on slot j%n_s 
    """
    # TODO Change the disp matrix to slots? Maybe add another constraint
    # source, specifically targeting the day and slot.
    # To fix someone on a specific role you can set the other slots to 0
    # every day.

    X = cvx.Bool(n_p, n_d * n_s)

    if demand is not None:
        # print('demand is not None, but is: ', demand)
        demand_constr = []
        for s in range(n_d * n_s):
            demand_constr.append(cvx.sum_entries(X[:, s]) == demand[s])

    # This is just an example, I need to change the rest of the code to use it
    else:
        demand_constr = [1 > 0]

    # Date Indisponibility constraints
    ind_constr = []
    for p, d in indisp:
        # Check if the list is being correctly appended: The elements, not the list
        ind_constr = ind_constr + [X[p, d * n_s + k] == 0 for k in range(n_s)]

    # Gender constraints
    gend_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            gend_constr.append(G.reshape(1, n_p) * X[:, s] <= prop * demand[s])

    # Teacher constraints
    teach_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            teach_constr.append(T.reshape(1, n_p) * X[:, s] == 1)

    # Maturity constraints
    matur_constr = []
    for s in range(n_d * n_s):
        if demand[s] > 1:
            matur_constr.append(M.reshape(1, n_p) * X[:, s] >= 1)

    # No Repeat constraints
    no_rep_constr = []
    for d in range(n_d):
        for p in range(n_p):
            no_rep_constr.append(
                cvx.sum_entries(X[p, d * n_s:n_s * (d + 1)]) <= 1)

    # Slot (Activity) choice constraint
    slot_constr = []
    for p in range(n_p):
        for s in range(n_s):
            slot_constr = slot_constr + [
                X[p, s + n_s * d] <= slot_choice[p, s] for d in range(n_d)
            ]

    # Forced constraint
    force_constr = []
    for p, d in forced:
        force_constr.append(X[p, d] == 1)

    constraints = (demand_constr + ind_constr + gend_constr + teach_constr +
                   matur_constr + no_rep_constr + slot_constr + force_constr)

    # Objective Function
    obj = cvx.Minimize(cvx.max_entries(cvx.sum_entries(X, axis=1)))

    prob = cvx.Problem(obj, constraints)
    prob.solve(solver=cvx.GLPK_MI)
    sol = X.value
    value = prob.value
    if sol is not None:
        # Gets rids of the residues, rounds everything to zero or one
        sol = np.int8(sol.round(2))
        value = int(round(value))

    return prob.status, sol, value
예제 #23
0
#
# A sample code to solve a knapsack_problem with cvxpy
#
# Author: Atsushi Sakai (@Atsushi_twi)
#

import cvxpy
import numpy as np
import random
import time

N_item = 30000

size = np.array([random.randint(1, 50) for i in range(N_item)])
weight = np.array([random.randint(1, 50) for i in range(N_item)])
capacity = 1000

x = cvxpy.Bool(size.shape[0])
objective = cvxpy.Maximize(weight * x)
constraints = [capacity >= size * x]

start = time.time()
prob = cvxpy.Problem(objective, constraints)
prob.solve(solver=cvxpy.ECOS_BB)
elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
result = [round(ix[0, 0]) for ix in x.value]

print("status:", prob.status)
print("optimal value", prob.value)
예제 #24
0
 def addBinVar(self):
     self._boolid += 1
     return cvxpy.Bool()
예제 #25
0
for i in range(N - 1):
    obsRHS = np.column_stack((obsRHS, obsRHS_1))

# Vertices of the obstacle
obsVerts = np.asarray([[3.5, 3.5, 6.5, 6.5, 3.5], [3.5, 6.5, 6.5, 3.5, 3.5]])

#### Define the system matrices (as matrix types so we can multiply) ####
A = np.matrix(
    np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]]))
B = np.matrix(np.array([[0.5, 0], [0, 0.5], [1, 0], [0, 1]]))

# Define the decision variables
X = cvx.Variable(4, N + 1)  # States
U = cvx.Variable(2, N)  # Inputs
b = cvx.Bool(4, N)  # Binary Variables

#### Define the Big-M constraint here ####
M = 100

#### Define dynamic constraints here ####

## Initial condition
con = [X[:, 0] == np.matrix('0;0;0;0')]

## Dynamics
con.extend([A * X[:, 0:15] + B * U == X[:, 1:16]])

## Input constraints
con.extend([cvx.norm(U, "inf") <= 0.5])
    def get_infected_dataset(self, new_train_data, new_train_labels):
        n, m = np.shape(new_train_data)
        svc = SVC(C=self.a, kernel='linear')
        svc.fit(new_train_data, new_train_labels)
        w_old = svc.coef_[0]
        b_old = svc.intercept_[0]

        b_t = 1000

        h_hat = {}  #cvx.Variable((n, n, m))
        for k in range(m):
            h_hat[k] = cvx.Variable(n, n)
        g = cvx.Variable(n, n)
        b = cvx.Variable()
        a_slack = cvx.Variable(n)
        c_slack = cvx.Variable(n)
        c_dual = cvx.Variable(n)
        z = cvx.Bool(n)

        labels = cvx.Constant(np.array(new_train_labels))
        data = cvx.Constant(np.array(new_train_data))

        cons = [
            a_slack >= -1,
            c_dual >= 0,
            #c_dual <= self.a*(1-z),    # todo: double check
            labels * c_dual == 0,
            c_slack >= 0,
            #c_slack <= b_t*z
        ]

        w = {}
        for j in range(m):
            num1 = c_dual.T * cvx.vstack(
                [labels[i] * data[i, j] for i in range(n)])
            num2 = labels * cvx.diag(h_hat[j])
            w[j] = self.a * (num1 + num2)

        for i in range(n):
            g_add = 0
            for k in range(n):
                h_kij = cvx.vstack([h_hat[j][k, i] for j in range(m)])
                g_add += (data[k, :] * h_kij) * labels[k] + labels[k] * g[k, i]

            cons.append(c_dual[i] <= self.a * (1 - z[i]))
            cons.append(c_slack[i] <= b_t * z[i])

            cons.append(a_slack[i] >= labels[i] *
                        (data[i, :] * np.array(list(w)) + b))
            cons.append(
                labels[i] *
                (data[i, :] * np.array(list(w)) + g_add + b) >= 1 - c_slack[i])
            cons.append(labels[i] *
                        (data[i, :] * np.array(list(w)) + g_add + b) - 1 +
                        c_slack[i] <= b_t * z[i])

            for j in range(n):
                cons.append(g[i, j] >= -c_dual[i] * (self.eps**2))
                cons.append(g[i, j] <= c_dual[i] * (self.eps**2))

                for k in range(m):
                    cons.append(h_hat[k][j, i] >= -self.eps * c_dual[j])
                    cons.append(h_hat[k][j, i] <= self.eps * c_dual[j])

        obj = cvx.Minimize(cvx.sum_entries(a_slack))
        prob = cvx.Problem(obj, cons)
        prob.solve()
        print(prob.status)
        # print(prob.value)

        # recovering infected dataset
        h = []
        infected_dataset = []

        n_sup = 0
        for i in range(n):
            hi = [(h_hat[j][i, i].value /
                   c_dual[i].value if c_dual[i].value != 0 else 0.0)
                  for j in range(m)]
            h.append(hi)
            print(c_dual[i].value)
            if c_dual[i].value != 0: n_sup += 1
            infected_dataset.append(
                [new_train_data[i, j] + hi[j] for j in range(m)])

        print('w0= ', w[0].value, 'w1= ', w[1].value, 'b= ', b.value)
        print('support vectors: ', n_sup)

        return np.array(infected_dataset), np.linalg.norm(h) / n