Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72517 > unrolled thread

Having trouble in expressing constraints in Python

Started byvarun7rs@gmail.com
First post2014-06-03 06:44 -0700
Last post2014-06-06 03:38 -0700
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Having trouble in expressing constraints in Python varun7rs@gmail.com - 2014-06-03 06:44 -0700
    Re: Having trouble in expressing constraints in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-06-03 16:15 +0100
      Re: Having trouble in expressing constraints in Python varun7rs@gmail.com - 2014-06-03 14:31 -0700
    Re: Having trouble in expressing constraints in Python Chris Angelico <rosuav@gmail.com> - 2014-06-04 01:19 +1000
    Re: Having trouble in expressing constraints in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-04 01:18 -0600
    Re: Having trouble in expressing constraints in Python varun7rs@gmail.com - 2014-06-06 03:38 -0700

#72517 — Having trouble in expressing constraints in Python

Fromvarun7rs@gmail.com
Date2014-06-03 06:44 -0700
SubjectHaving trouble in expressing constraints in Python
Message-ID<1919ef6f-6aed-4b03-a9b1-ac9e2a2b31a7@googlegroups.com>
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 fine except for one constraint(Link Mapping Constraint). I've attached pieces 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 not 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] + IMS_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])) <= capacity_bdw[ns1, ns2];

        for edge in phy_network.edges:
            varNames = []
            varCoeffs = []
            for demand in demands:
                varNames.append("f_eN_SGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
                varCoeffs.append(demand.MME_bdw + demand.IMS_bdw + demand.PoP_bdw )
                varNames.append("f_SGW_PGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
                varCoeffs.append(demand.PoP_bdw)
                varNames.append("f_SGW_IMS_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
                varCoeffs.append(demand.IMS_bdw)
                varNames.append("f_SGW_MME_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
                varCoeffs.append(demand.MME_bdw)
                varNames.append("f_PGW_PoP_{}_{}_{}".format(demand.demandID, 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.DestinationID, demand.demandID))

#Link Mapping Constraint
subject to Link_Mapping_Constraints_1{dns in DEMAND, ns1 in PHY_NODES}: sum {(ns1,w) in PHY_LINKS} (f_eN_SGW[dns, w, ns1] - f_eN_SGW[dns, ns1, w]) = x_eN[dns, ns1] - x_SGW[dns, ns1];





[toc] | [next] | [standalone]


#72524

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-06-03 16:15 +0100
Message-ID<mailman.10628.1401808540.18130.python-list@python.org>
In reply to#72517
On 03/06/2014 14:44, varun7rs@gmail.com 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 fine except for one constraint(Link Mapping Constraint). I've attached pieces 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 not 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] + IMS_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])) <= capacity_bdw[ns1, ns2];
>
>          for edge in phy_network.edges:
>              varNames = []
>              varCoeffs = []
>              for demand in demands:
>                  varNames.append("f_eN_SGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                  varCoeffs.append(demand.MME_bdw + demand.IMS_bdw + demand.PoP_bdw )
>                  varNames.append("f_SGW_PGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                  varCoeffs.append(demand.PoP_bdw)
>                  varNames.append("f_SGW_IMS_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                  varCoeffs.append(demand.IMS_bdw)
>                  varNames.append("f_SGW_MME_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                  varCoeffs.append(demand.MME_bdw)
>                  varNames.append("f_PGW_PoP_{}_{}_{}".format(demand.demandID, 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.DestinationID, demand.demandID))
>
> #Link Mapping Constraint
> subject to Link_Mapping_Constraints_1{dns in DEMAND, ns1 in PHY_NODES}: sum {(ns1,w) in PHY_LINKS} (f_eN_SGW[dns, w, ns1] - f_eN_SGW[dns, ns1, w]) = x_eN[dns, ns1] - x_SGW[dns, ns1];
>

Are you trying to implement your own code rather than use an existing 
library from pypi?

I also observe the gmail address which I'm assuming means google groups. 
  If that is the case, would you please use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.  If not 
please ignore this paragraph :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#72552

Fromvarun7rs@gmail.com
Date2014-06-03 14:31 -0700
Message-ID<752aeb33-d83c-4137-b964-86e6abbd43bf@googlegroups.com>
In reply to#72524
> 
> Are you trying to implement your own code rather than use an existing 
> library from pypi?

I borrowed the idea from a previous file which I was working on. I input variables and coefficients as lists and then inturn as matrices to the CPLEX. So, I have a problem with expressing the constraint in Python.

[toc] | [prev] | [next] | [standalone]


#72525

FromChris Angelico <rosuav@gmail.com>
Date2014-06-04 01:19 +1000
Message-ID<mailman.10629.1401808778.18130.python-list@python.org>
In reply to#72517
On Wed, Jun 4, 2014 at 1:15 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> I also observe the gmail address which I'm assuming means google groups.

No need to assume - the OP's headers show Google Groups injection info.

ChrisA

[toc] | [prev] | [next] | [standalone]


#72607

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-06-04 01:18 -0600
Message-ID<mailman.10685.1401866386.18130.python-list@python.org>
In reply to#72517
On Tue, Jun 3, 2014 at 7:44 AM,  <varun7rs@gmail.com> 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 fine except for one constraint(Link Mapping Constraint). I've attached pieces 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 not 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] + IMS_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])) <= capacity_bdw[ns1, ns2];
>
>         for edge in phy_network.edges:
>             varNames = []
>             varCoeffs = []
>             for demand in demands:
>                 varNames.append("f_eN_SGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                 varCoeffs.append(demand.MME_bdw + demand.IMS_bdw + demand.PoP_bdw )
>                 varNames.append("f_SGW_PGW_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                 varCoeffs.append(demand.PoP_bdw)
>                 varNames.append("f_SGW_IMS_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                 varCoeffs.append(demand.IMS_bdw)
>                 varNames.append("f_SGW_MME_{}_{}_{}".format(demand.demandID, edge.SourceID, edge.DestinationID))
>                 varCoeffs.append(demand.MME_bdw)
>                 varNames.append("f_PGW_PoP_{}_{}_{}".format(demand.demandID, 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.DestinationID, demand.demandID))
>
> #Link Mapping Constraint
> subject to Link_Mapping_Constraints_1{dns in DEMAND, ns1 in PHY_NODES}: sum {(ns1,w) in PHY_LINKS} (f_eN_SGW[dns, w, ns1] - f_eN_SGW[dns, ns1, w]) = 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.

[toc] | [prev] | [next] | [standalone]


#72830

Fromvarun7rs@gmail.com
Date2014-06-06 03:38 -0700
Message-ID<71ca41c5-65be-4523-9d59-02c2ddce7fb0@googlegroups.com>
In reply to#72517
Thanks a lot Ian. Your post helped me understand the problem in a much better way and I've solved the first objective thanks to you but incase of my second objective which is minimize the number of nodes, I have got one of the weirdest looking constraints which I don't know how to express in Python because python basically takes matrices and inputs them into cplex. My constraint is as below. Thsi was what I wrote in AMPL

minimize phy_nodes: sum {w in PHY_NODES} x_ns[w] ;

s.t. Phy_nodes_Eq{w in PHY_NODES, dns in DEMAND}: 
x_ns[w] = 1 ==> x_SGW[dns, w] + x_PGW[dns, w] + x_MME[dns, w] + x_IMS[dns, w] + x_PoP[dns, w] >= 1 
else x_SGW[dns, w] + x_PGW[dns, w] + x_MME[dns, w] + x_IMS[dns, w] + x_PoP[dns, w] = 0;
Could you help me fix this problem?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web