# Defining Subsets in CPLEX (By C++)

 1 Hi, I'm trying to write a C++ code for a problem and use CPLEX as a solver. There is one thing i'm not familiar with and it is how to define subsets. The problem is consisted of a network including n nodes. but there are some nodes that have an especial property such as the potential to become a warehouse. For example if the set of nodes is N=(1,2,3,4,5) there is B the subset of N which is B=(2,5) i want to write specific constraints for the nodes in B and i don't know what is the general way to define this subset. one thing comes to my mind is to define set B'=(0,1,0,0,1) and use these parameters in all constraints (with caution of course) and cover the nodes in B and neglect others. How do you approach such problems? asked 20 May '12, 07:22 Al1ta 23●2●7 accept rate: 0% fbahr ♦ 4.6k●7●16

 2 You can use the STL set container class to define sets. In the code that builds your model, if you want to create constraints only for indices in some set you created, just loop over that set (adding one constraint per loop iteration). To build an expression (in a constraint or in the objective function) containing a subset of the variables, again define the subset as an instance of the STL set class, then build the expression term by term by looping over the set. Incidentally, since CPLEX handle classes are basically pointers, you can put instances of IloNumVar or IloRange directly into an STL set, rather than putting integer indices for them in the set ... provided you have a convenient way to figure out coefficients etc. based on the identity/name of the constraint/variable rather than on its index. answered 20 May '12, 20:02 Paul Rubin ♦♦ 14.6k●4●12 accept rate: 19% Dear Prof.Rubin, Thanks a lot for your help, it was definitely was what i was looking for , i am reading its guidelines to use it on my further problems. (22 May '12, 02:42) Al1ta
 1 I am not sure if I understood it correctly but why should one need to declare N=(1,2,3,4,5)? why not just indexing the potential locations by jin{1,2} where l_1 is site 2 and l_2 is site 5 and declare corresponding variables? did I miss something? answered 20 May '12, 08:04 ShahinG 281●2●13 accept rate: 0% thanks dear Shahin, I coded the program based on your idea and defined two sets. (22 May '12, 02:32) Al1ta
 1 Similar to the Shahin's idea, you could sort the network nodes based on their properties. For example, if (n) nodes are candidates for opening facilities and (m) nodes are customer nodes, you could dedicate nodes from 1 to (n) to potential facility nodes and nodes from (n + 1) to (n + m) to customer nodes. Then you should control defining the necessary constraints for each type of node based on nodes numbers, which is fairly easy using a for loop. Also, your own approach could be improved via using (B) values in an if condition as follows:  for (int i = 0; i < Array::GetLength(B); i++) if (B[i] == 1){ //Add constraints }  This way you're sure you don't make any mistakes in creating necessary constraints. answered 20 May '12, 13:49 Ehsan ♦ 4.8k●3●11●22 accept rate: 16% Dear ehsan Tashakor, I wanted to code something without the "IF"s and wanted to know about some libraries to do this as Prof.Rubin described. (22 May '12, 02:34) Al1ta
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Tags:

×191
×30
×21
×2