Hi,
I am wondering how to merge two networks into one? In particular, I would like to clone a network and combine the two copies into one network and then add connections between them. I have tried to copy the network, add new nodes to the new network and clone the node definitions and node dmatrics. But it did not work. Thanks.
Regards,
Peter
Merge two networks
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Re: Merge two networks
What kind of failure did you experience?petcai wrote:But it did not work.
Re: Merge two networks
1) I first got each node from network F and add it into another network T.
2) I then got each child C of each node N in F and added an arc from N to C in network T.
3) And then copy the dmatrix as follows:
DSL_Dmatrix* pDmatProb = pntNodeFrom->Definition()->GetMatrix();
pntNodeTo->Definition()->SetDefinition(*pDmatProb);
which gave me the error when run:
Assertion failed: table.CompatibleWith(withThis), file d:\shooltz\dsl\repo\smile
\defcpt.h, line 107
Before 3) and after 2), I found the numbers return from GetNumberOfOutcomes() are different for corresponding nodes from F and T. Thanks.
2) I then got each child C of each node N in F and added an arc from N to C in network T.
3) And then copy the dmatrix as follows:
DSL_Dmatrix* pDmatProb = pntNodeFrom->Definition()->GetMatrix();
pntNodeTo->Definition()->SetDefinition(*pDmatProb);
which gave me the error when run:
Assertion failed: table.CompatibleWith(withThis), file d:\shooltz\dsl\repo\smile
\defcpt.h, line 107
Before 3) and after 2), I found the numbers return from GetNumberOfOutcomes() are different for corresponding nodes from F and T. Thanks.
Re: Merge two networks
I changed to use FastCopy. Now the program run to the last line and crash when save the new network as follows:
DSL_network netFrom, netTo;
netFrom.ReadFile("StaticNet.xdsl");
netTo.Copy(netFrom);
DSL_node* pNodeFrom;
DSL_node* pNodeTo;
int iNodeFrom;
int iNodeTo;
// Get all nodes from network F
DSL_intArray iArrNodesFrom;
netFrom.GetAllNodes(iArrNodesFrom);
cout << "Num of nodes: " << iArrNodesFrom.NumItems() << endl;
// Add new nodes
int intS = 1;
for(int i = 0; i < iArrNodesFrom.NumItems(); i++){
//Get a node from the network F
iNodeFrom = iArrNodesFrom;
pNodeFrom = netFrom.GetNode(iNodeFrom);
//Add a new node to the network
stringstream ssId;
ssId << pNodeFrom->GetId() << "_" << intS;
int intType = pNodeFrom->Definition()->GetType();
//cout << "Type" << intType << endl;
netTo.AddNode(intType, ssId.str().c_str());
}
for(int i = 0; i < iArrNodesFrom.NumItems(); i++){
cout << "Copy node definition " << i << " ..." << endl;
//Get a node from the static network
iNodeFrom = iArrNodesFrom;
pNodeFrom = netFrom.GetNode(iNodeFrom);
stringstream ssId;
ssId << pNodeFrom->GetId() << "_" << intS;
iNodeTo = netTo.FindNode(ssId.str().c_str());
pNodeTo = netTo.GetNode(iNodeTo);
cout<< pNodeFrom->GetId() << "->" << pNodeTo->GetId() << endl;
cout<<"Type "<<pNodeFrom->Definition()->GetType() << "->" <<pNodeTo->Definition()->GetType() <<endl;
int intNum = pNodeFrom->Definition()->GetNumberOfOutcomes();
cout << intNum << endl;
//Copy node definition
pNodeTo->FastCopy(*pNodeFrom);
intNum = pNodeTo->Definition()->GetNumberOfOutcomes();
cout << intNum << endl;
cout << "Num of children: " << pNodeTo->Children().NumItems() << endl;
cout << "Copying nodes completed." << endl;
}
//Save merged network
netTo.WriteFile("MergedNet.xdsl");
DSL_network netFrom, netTo;
netFrom.ReadFile("StaticNet.xdsl");
netTo.Copy(netFrom);
DSL_node* pNodeFrom;
DSL_node* pNodeTo;
int iNodeFrom;
int iNodeTo;
// Get all nodes from network F
DSL_intArray iArrNodesFrom;
netFrom.GetAllNodes(iArrNodesFrom);
cout << "Num of nodes: " << iArrNodesFrom.NumItems() << endl;
// Add new nodes
int intS = 1;
for(int i = 0; i < iArrNodesFrom.NumItems(); i++){
//Get a node from the network F
iNodeFrom = iArrNodesFrom;
pNodeFrom = netFrom.GetNode(iNodeFrom);
//Add a new node to the network
stringstream ssId;
ssId << pNodeFrom->GetId() << "_" << intS;
int intType = pNodeFrom->Definition()->GetType();
//cout << "Type" << intType << endl;
netTo.AddNode(intType, ssId.str().c_str());
}
for(int i = 0; i < iArrNodesFrom.NumItems(); i++){
cout << "Copy node definition " << i << " ..." << endl;
//Get a node from the static network
iNodeFrom = iArrNodesFrom;
pNodeFrom = netFrom.GetNode(iNodeFrom);
stringstream ssId;
ssId << pNodeFrom->GetId() << "_" << intS;
iNodeTo = netTo.FindNode(ssId.str().c_str());
pNodeTo = netTo.GetNode(iNodeTo);
cout<< pNodeFrom->GetId() << "->" << pNodeTo->GetId() << endl;
cout<<"Type "<<pNodeFrom->Definition()->GetType() << "->" <<pNodeTo->Definition()->GetType() <<endl;
int intNum = pNodeFrom->Definition()->GetNumberOfOutcomes();
cout << intNum << endl;
//Copy node definition
pNodeTo->FastCopy(*pNodeFrom);
intNum = pNodeTo->Definition()->GetNumberOfOutcomes();
cout << intNum << endl;
cout << "Num of children: " << pNodeTo->Children().NumItems() << endl;
cout << "Copying nodes completed." << endl;
}
//Save merged network
netTo.WriteFile("MergedNet.xdsl");
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Re: Merge two networks
The FastCopy method was designed for internal use only; it's called when entire network is copied and assumes the node handles are invariant. This condition is probably not met by your code.petcai wrote:I changed to use FastCopy.
Your first approach (when you added nodes and arcs manually) should work.
You'll have to call DSL_nodeDefinition::SetNumberOfOutcomes to fix this problem. Note that the method should be called before there are any outgoing arcs.Before 3) and after 2), I found the numbers return from GetNumberOfOutcomes() are different for corresponding nodes from F and T.