Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'retrieved': 0.05; 'subject:Python': 0.06; 'source.': 0.07; 'fashion.': 0.09; 'iterate': 0.09; 'python': 0.11; 'wrote': 0.14; 'dict': 0.16; 'iterating': 0.16; 'skip:v 60': 0.16; 'varnames': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'pieces': 0.19; 'help.': 0.21; 'aspect': 0.24; 'comparing': 0.24; 'of.': 0.24; 'skip:l 30': 0.24; 'fine': 0.24; 'looks': 0.24; "i've": 0.25; 'source': 0.25; 'skip:v 30': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'node': 0.31; 'quite': 0.32; 'proceed': 0.33; 'problem': 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'edge': 0.36; 'skip:" 50': 0.36; "didn't": 0.36; "i'll": 0.36; 'similar': 0.36; 'list': 0.37; 'mapping': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'tell': 0.60; 'first': 0.61; 'such': 0.63; 'sum': 0.64; 'more': 0.64; 'subject': 0.69; 'wish': 0.70; 'skip:v 50': 0.84; 'demand': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=VLGYDX3Fo9j/kMPz/SgVKIUjhtbAyW92BTgOsrhaIJg=; b=N18c5R9QQycRbVXsPjLHkOQrdjs9H2G5jdQ1MKUE82Kmi9gCqwShT6ClJTfdYwn+Wk tCwVlWfWkeJwePZ/rJAb9g5BugzfOh934ZgCCW9dTao2fXmoX3V9OJX1YRg4A45ZKPFL 5+jvXfRaAfEjhK1jWgK2oJmAoo9o96XWFNxkbXohhnIT2t7YfWeudnJC+Y6XR7SS9s2X 0G2GZh3sDhbxGWxBpWQ3f0eacfN5YWmTjlsHLi2objvU8lZ5u+jrr1/+A39SdThZmwoL fk7TaBgQVvVTGGcrD18SdvOraZpU7g3GYrhRnJ9JhhmYTklVXBVU8LlCZzTDB9xZBzGQ nQ9A== X-Received: by 10.236.179.69 with SMTP id g45mr69980484yhm.81.1401866378618; Wed, 04 Jun 2014 00:19:38 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1919ef6f-6aed-4b03-a9b1-ac9e2a2b31a7@googlegroups.com> References: <1919ef6f-6aed-4b03-a9b1-ac9e2a2b31a7@googlegroups.com> From: Ian Kelly Date: Wed, 4 Jun 2014 01:18:58 -0600 Subject: Re: Having trouble in expressing constraints in Python To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401866386 news.xs4all.nl 2902 [2001:888:2000:d::a6]:39871 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72607 On Tue, Jun 3, 2014 at 7:44 AM, wrote: > I have a problem in writing a constraint in Python. Firstly, I wrote the = code in AMPL and it was working and I'm using Python for the reason that it= is more suitable to handle large data. I managed to write the code quite f= ine except for one constraint(Link Mapping Constraint). I've attached piece= s of code from both AMPL and Python. First part of it is the Link Capacity = Constraint in AMPl followed by Python. Second part of the code is the Link = Mapping Constraint and I wish to write it in a similar fashion. But, I'm no= t able to proceed with it. I really appreciate your help. > > subject to Link_Capacity_Constraints { (ns1, ns2) in PHY_LINKS}: > sum {dns in DEMAND} ((f_eN_SGW[dns, ns1, ns2] * (MME_bdw[dns] + I= MS_bdw[dns] + PoP_bdw[dns])) + (f_SGW_PGW[dns, ns1, ns2] * PoP_bdw[dns]) + = (f_SGW_IMS[dns, ns1, ns2] * IMS_bdw[dns]) + (f_SGW_MME[dns, ns1, ns2] * MME= _bdw[dns]) + (f_PGW_PoP[dns, ns1, ns2] * PoP_bdw[dns])) <=3D capacity_bdw[n= s1, ns2]; > > for edge in phy_network.edges: > varNames =3D [] > varCoeffs =3D [] > for demand in demands: > varNames.append("f_eN_SGW_{}_{}_{}".format(demand.demandI= D, edge.SourceID, edge.DestinationID)) > varCoeffs.append(demand.MME_bdw + demand.IMS_bdw + demand= .PoP_bdw ) > varNames.append("f_SGW_PGW_{}_{}_{}".format(demand.demand= ID, edge.SourceID, edge.DestinationID)) > varCoeffs.append(demand.PoP_bdw) > varNames.append("f_SGW_IMS_{}_{}_{}".format(demand.demand= ID, edge.SourceID, edge.DestinationID)) > varCoeffs.append(demand.IMS_bdw) > varNames.append("f_SGW_MME_{}_{}_{}".format(demand.demand= ID, edge.SourceID, edge.DestinationID)) > varCoeffs.append(demand.MME_bdw) > varNames.append("f_PGW_PoP_{}_{}_{}".format(demand.demand= ID, edge.SourceID, edge.DestinationID)) > varCoeffs.append(demand.PoP_bdw) > solver.add_constraint(varNames, varCoeffs, "L", edge.capacity= _bdw, "Link_Capacity_Constraints{}_{}_{}".format(edge.SourceID, edge.Destin= ationID, demand.demandID)) > > #Link Mapping Constraint > subject to Link_Mapping_Constraints_1{dns in DEMAND, ns1 in PHY_NODES}: s= um {(ns1,w) in PHY_LINKS} (f_eN_SGW[dns, w, ns1] - f_eN_SGW[dns, ns1, w]) = =3D x_eN[dns, ns1] - x_SGW[dns, ns1]; You didn't tell us what aspect of this is confounding you, so I'll take a wild stab at it. Looks like you want to iterate over the Cartesian product of all the demands and all the nodes, and for each such pair generate a constraint by iterating over all the edges that have that node as the source. You can do that by iterating over *all* the edges and comparing the source node of each one. Or you can precompute the edges for each source node and store them in a dict mapping each node to the list of edges it is the source of. Then all you need to do in the loop is look up the source node in the dict and iterate over the retrieved list of edges.