def build_model(self, fixed=False, u_fixed=None): for t in self.period: for g in self.generators.index: if fixed: self.u[t, g] = self.model.add_var(lb=u_fixed[t, g], ub=u_fixed[t, g]) else: self.u[t, g] = self.model.add_var(var_type=BINARY) self.p[t, g] = self.model.add_var() # Max/min power for t in self.period: for g in self.generators.index: self.model.add_constr( self.p[t, g] <= self.generators.loc[g, 'p_max'] * self.u[t, g], name=f'pmax_constr[{t},{g}]') self.model.add_constr( self.p[t, g] >= self.generators.loc[g, 'p_min'] * self.u[t, g], name=f'pmin_constr[{t},{g}]') # Power balance for t in self.period: self.model.add_constr(xsum( self.p[t, g] for g in self.generators.index) == self.demand.loc[t, 'demand'], name=f'power_bal_constr[{t}]') # Min on for t in self.period[1:]: for g in self.generators.index: min_on_time = min(t + self.generators.loc[g, 'min_on'] - 1, len(self.period)) for tau in range(t, min_on_time + 1): self.model.add_constr( self.u[tau, g] >= self.u[t, g] - self.u[t - 1, g], name=f'min_on_constr[{t},{g}]') # Min off for t in self.period[1:]: for g in self.generators.index: min_off_time = min(t + self.generators.loc[g, 'min_off'] - 1, len(self.period)) for tau in range(t, min_off_time + 1): self.model.add_constr( 1 - self.u[tau, g] >= self.u[t - 1, g] - self.u[t, g], name=f'min_off_constr[{t},{g}]') # Objective function self.model.objective = minimize( xsum( xsum(self.p[t, g] * self.generators.loc[g, 'c_var'] + self.u[t, g] * self.generators.loc[g, 'c_fix'] for g in self.generators.index) for t in self.period))
print('solving TSP with {} cities'.format(len(N))) model = Model() # binary variables indicating if arc (i,j) is used on the route or not x = { a: model.add_var('x({},{})'.format(a[0], a[1]), var_type=BINARY) for a in A } # continuous variable to prevent subtours: each # city will have a different "identifier" in the planned route y = {i: model.add_var(name='y({})') for i in N} # objective function: minimize the distance model.objective = minimize(xsum(A[a] * x[a] for a in A)) # constraint : enter each city coming from another city for i in N: model += xsum(x[a] for a in OUT[i]) == 1 # constraint : leave each city coming from another city for i in N: model += xsum(x[a] for a in IN[i]) == 1 # no subtours of size 2 for a in A: if (a[1], a[0]) in A.keys(): model += x[a] + x[a[1], a[0]] <= 1 # computing farthest point for each point
print('solving TSP with {} cities'.format(len(N))) model = Model() # binary variables indicating if arc (i,j) is used on the route or not x = { a: model.add_var('x({},{})'.format(a[0], a[1]), var_type=BINARY) for a in A.keys() } # continuous variable to prevent subtours: each # city will have a different "identifier" in the planned route y = {i: model.add_var(name='y({})') for i in N} # objective function: minimize the distance model.objective = minimize(xsum(A[a] * x[a] for a in A.keys())) # constraint : enter each city coming from another city for i in N: model += xsum(x[a] for a in OUT[i]) == 1 # constraint : leave each city coming from another city for i in N: model += xsum(x[a] for a in IN[i]) == 1 # subtour elimination for (i, j) in [a for a in A.keys() if n0 not in [a[0], a[1]]]: model += \ y[i] - (n+1)*x[(i, j)] >= y[j]-n, 'noSub({},{})'.format(i, j) print('model has {} variables, {} of which are integral and {} rows'.format(
import bmcp_greedy from mip.model import Model, xsum, minimize from mip.constants import MINIMIZE, BINARY data = bmcp_data.read('P1.col') N, r, d = data.N, data.r, data.d S = bmcp_greedy.build(data) C, U = S.C, [i for i in range(S.u_max + 1)] m = Model(sense=MINIMIZE) x = [[m.add_var('x({},{})'.format(i, c), var_type=BINARY) for c in U] for i in N] z = m.add_var('z') m.objective = minimize(z) for i in N: m += xsum(x[i][c] for c in U) == r[i] for i, j, c1, c2 in product(N, N, U, U): if i != j and c1 <= c2 < c1 + d[i][j]: m += x[i][c1] + x[j][c2] <= 1 for i, c1, c2 in product(N, U, U): if c1 < c2 < c1 + d[i][i]: m += x[i][c1] + x[i][c2] <= 1 for i, c in product(N, U): m += z >= (c + 1) * x[i][c]