Yes, you can do that. Here is an example for the demand. You can add more fields as you please, I just restricted myself to demand to keep things short:
tuple arcCustomer {
int startTime;
int waitTime;
string origin;
string destination;
int demand;
}
tuple arcWait {
int startTime;
int waitTime;
string origin;
string destination;
int demand;
}
tuple unionKey {
int startTime;
int waitTime;
string origin;
string destination;
}
// Tuple to hold the union elements in case we want to distinguish the demands
// from the two original sets.
tuple arcUnionBoth {
int startTime;
int waitTime;
string origin;
string destination;
int demandCustomer;
int demandWait;
}
// Tuple to hold the union elements in case we want to sum the demands from
// the two original sets.
tuple arcUnionSum {
int startTime;
int waitTime;
string origin;
string destination;
int demandSum;
}
{arcCustomer} customer = { <1, 1, "a", "b", 1>,
<1, 2, "a", "b", 2> };
{arcWait} wait = { <1, 1, "a", "b", 3>,
<1, 3, "a", "b", 4> };
// Create a set that contains the <startTime,waitTime,origin,destination>
// tuples that will appear in the union.
{unionKey} unionKeys = { <t.startTime, t.waitTime, t.origin, t.destination> | t in customer }
union
{ <t.startTime, t.waitTime, t.origin, t.destination> | t in wait };
// Construct the elements in unionBoth. The first 4 elements are just the
// keys from unionKeys. The next two elements are the matching demands from
// the customer and wait set, respectively.
{arcUnionBoth} unionBoth = { <k.startTime,k.waitTime,k.origin,k.destination,
sum (t in customer : t.startTime == k.startTime &&
t.waitTime == k.waitTime &&
t.origin == k.origin &&
t.destination == k.destination) t.demand
,
sum (t in wait : t.startTime == k.startTime &&
t.waitTime == k.waitTime &&
t.origin == k.origin &&
t.destination == k.destination) t.demand>
| k in unionKeys };
// Construct the elements in unionBoth. The first 4 elements are just the
// keys from unionKeys. The next element is the sum of the demands of all
// matching elements.
{arcUnionSum} unionSum = { <k.startTime,k.waitTime,k.origin,k.destination,
sum (t in customer : t.startTime == k.startTime &&
t.waitTime == k.waitTime &&
t.origin == k.origin &&
t.destination == k.destination) t.demand
+ // this differs from unionBoth
sum (t in wait : t.startTime == k.startTime &&
t.waitTime == k.waitTime &&
t.origin == k.origin &&
t.destination == k.destination) t.demand>
| k in unionKeys };
execute {
writeln("unionBoth:");
writeln(unionBoth);
writeln("unionSum:");
writeln(unionSum);
}
Note: I think in each of your sets the combination of <startTime, endTime, origin, destination> is unique, i.e., you don't have two tuples in the same set that have the same value in all of the four fields. In that case you can improve performance a little by marking these four fields as "key" (see here):
tuple arcCustomer {
key int startTime;
key int waitTime;
key string origin;
key string destination;
int demand;
}
tuple arcWait {
key int startTime;
key int waitTime;
key string origin;
key string destination;
int demand;
}
tuple unionKey {
key int startTime;
key int waitTime;
key string origin;
key string destination;
}
#DecisionOptimization#OPLusingCPLEXOptimizer