コード例 #1
0
ファイル: ccopf.py プロジェクト: sharnett/ccopf
 def add_cut(self, it):
     _,branch,_,Binv, _,_,_,_,winds,sigma,_ = self.data + self.aux
     p, a, D, t, m, s = self.var 
     index = self.worst['line']
     ID = branch[index]['id']
     i,j = branch[index]['ij']
     y = branch[index]['y']
     if self.solver == 'cplex':
         Di, Dj = self.sol.get_values([D[i], D[j]])
         sij = 's{}'.format(ID)
         Bij = {k: Binv[k][i]-Binv[k][j] for k in winds}
         rhs = y**2*sum([sigma[k]**2*Bij[k]*(Bij[k]-Di+Dj) for k in winds])
         coeff = y**2*sum([sigma[k]**2*(Bij[k]-Di+Dj) for k in winds])
         x = arr([y*(Bij[k]-Di+Dj)*sigma[k] for k in winds])
         #self.M.linear_constraints.add(lin_expr = [sp(ind = [sij], val = [1.0])], 
                 #rhs = [0], names=['cut_help%d'%it], senses='G')
         self.M.linear_constraints.add(lin_expr = 
                 [sp(ind = [sij, D[i], D[j]], val = [norm(x), coeff, -coeff])], 
                 rhs = [rhs], names=['cut_%d'%it], senses='G')
     else:
         x = arr([y*(Binv[k][i]-Binv[k][j]-D[i].x+D[j].x)*sigma[k] for k in winds])
         self.M.addConstr(s[ID] >= 0, 'cut_%d_help'%(it+1))
         self.M.addConstr(qs(x[l]*y*(Binv[k][i]-Binv[k][j]-D[i]+D[j])*sigma[k]
                 for l,k in enumerate(winds)) <= s[ID]*norm(x), 'cut_%d'%it)
コード例 #2
0
ファイル: ccopf.py プロジェクト: sharnett/ccopf
 def addCons(self):
     bus,branch,Bbus,_, line_nu,gen_nu, n,_,d,mu,winds,sigma,gens = self.data + \
             [self.line_nu, self.gen_nu] + self.aux
     if self.verbose: print 'defining constraints'
     p, a, D, t, m, s = self.var 
     sumvar = sum(sigma[b]**2 for b in winds)
     if self.solver == 'cplex':
         ng = len(gens)
         self.M.linear_constraints.add(lin_expr = [sp(ind = a, val = [1.0]*ng)], 
                 rhs = [1.0], names=['sum_alpha'])
         self.M.linear_constraints.add(lin_expr = [sp(ind = p, val = [1.0]*ng)], 
                 rhs = [sum(d)-sum(mu)], names=['power_balance'])
         for i in xrange(n-1):
             Bi = Bbus[i,:-1].tocoo()
             J,V = Bi.col, Bi.data
             if i in gens:
                 pi, ai, Di, ti = 'p_%d'%i, 'a_%d'%i, 'delta_%d'%i, 'theta_%d'%i
                 self.M.linear_constraints.add(lin_expr = 
                         [sp(ind = list(D[J])+[ai], val = list(V)+[-1]),
                         sp(ind = list(t[J])+[pi], val = list(V)+[-1])], 
                         rhs=[0, mu[i]-d[i]], names=[Di, ti], senses='EE')
                 self.M.linear_constraints.add(lin_expr = 
                         [sp(ind = [pi,ai], val = [1, sumvar**.5*gen_nu]), 
                         sp(ind = [pi,ai], val = [1, -sumvar**.5*gen_nu])], 
                         rhs=[bus[i]['pmax'], bus[i]['pmin']], 
                         names=['gen+_%d'%i, 'gen-_%d'%i], senses='LG')
             else:
                 self.M.linear_constraints.add(lin_expr = 
                         [sp(ind = D[J], val = V), sp(ind = t[J], val = V)], 
                         rhs=[0, mu[i]-d[i]], names=['delta_%d'%i, 'theta_%d'%i], 
                         senses='EE')
         self.M.linear_constraints.add(lin_expr = 
                 [sp(ind = [D[n-1]], val = [1.0]), sp(ind = [t[n-1]], val = [1.0])], 
                 rhs = [0,0], names=['delta_%d'%(n-1), 'theta_%d'%(n-1)], senses='EE')
         for line in branch:
             ID = line['id']
             ij = line['ij']
             i,j = ij
             mij, sij = 'm{}'.format(ID), 's{}'.format(ID)
             self.M.linear_constraints.add(lin_expr = 
                     [sp(ind = [mij, t[i], t[j]], val = [1.0, -line['y'], line['y']])], 
                     rhs = [0], names=['line_avg_{}'.format(ID)], senses='E')
             self.M.linear_constraints.add(lin_expr = 
                     [sp(ind = [mij, sij], val = [-1.0, line_nu]),
                     sp(ind = [mij, sij], val = [1.0, line_nu])], 
                     names=['line+_{}'.format(ID), 'line-_{}'.format(ID)],
                     rhs = [line['fmax'], line['fmax']], senses='LL')
     else: # gurobi is much nicer
         self.M.addConstr(qs(a[i] for i in gens) == 1, 'sum_alpha_1')
         self.M.addConstr(qs(p[i] for i in gens) + sum(mu) == sum(d), 'power_balance')
         for i in xrange(n-1):
             Bi = Bbus[i,:-1].tocoo()
             Bi = zip(Bi.col, Bi.data)
             if i in gens:
                 self.M.addConstr(qs(v*D[j] for j,v in Bi) == a[i], 'delta_%d'%i)
                 self.M.addConstr(qs(v*t[j] for j,v in Bi) == p[i]+mu[i]-d[i], 
                         'theta_%d'%i)
                 self.M.addConstr(sumvar**.5*a[i]*gen_nu <= bus[i]['pmax']-p[i], 
                         'gen+_%d'%i)
                 self.M.addConstr(sumvar**.5*a[i]*gen_nu <= -bus[i]['pmin']+p[i], 
                         'gen-_%d'%i)
             else:
                 self.M.addConstr(qs(v*D[j] for j,v in Bi) == 0, 'delta_%d'%i)
                 self.M.addConstr(qs(v*t[j] for j,v in Bi) == mu[i]-d[i], 'theta_%d'%i)
         self.M.addConstr(D[n-1] == 0, 'delta')
         self.M.addConstr(t[n-1] == 0, 'theta')
         for line in branch:
             ID = line['id']
             ij = line['ij']
             i,j = ij
             self.M.addConstr(m[ID] == line['y']*(t[i]-t[j]), 
                     'line_avg_{}'.format(ID))
             self.M.addConstr(s[ID]*line_nu <= line['fmax'] - m[ID], 
                     'line+_{}'.format(ID))
             self.M.addConstr(s[ID]*line_nu <= line['fmax'] + m[ID], 
                     'line-_{}'.format(ID))