StochGenMatrix* sTreeImpl::createC() { //is this node a dead-end for this process? if(commWrkrs==MPI_COMM_NULL) return new StochGenDummyMatrix(m_id); StochGenMatrix* C = NULL; if (m_id==0) { CoinPackedMatrix Crow; Crow.reverseOrderedCopyOf( in.getFirstStageConstraints() ); // number of nz in the rows corresponding to ineq constraints int nnzD=countNNZ( Crow, in.getFirstStageRowLB(), in.getFirstStageRowUB(), ineq_comp()); C = new StochGenMatrix( m_id, MZ, N, m_mz, 0, 0, // C does not exist for the root m_mz, m_nx, nnzD, // D is 1st stage ineq matrix commWrkrs ); extractRows( Crow, in.getFirstStageRowLB(), in.getFirstStageRowUB(), ineq_comp(), C->Bmat->krowM(), C->Bmat->jcolM(), C->Bmat->M() ); //printf(" -- 1st stage mz=%lu nx=%lu nnzD=%d\n", m_mz, m_nx, nnzD); } else { int scen=m_id-1; CoinPackedMatrix Crow, Drow; Crow.reverseOrderedCopyOf( in.getLinkingConstraints(scen) ); Drow.reverseOrderedCopyOf( in.getSecondStageConstraints(scen) ); int nnzC=countNNZ( Crow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), ineq_comp() ); int nnzD=countNNZ( Drow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), ineq_comp() ); C = new StochGenMatrix( m_id, MZ, N, m_mz, parent->m_nx, nnzC, m_mz, m_nx, nnzD, commWrkrs ); //printf(" -- 2nd stage mz=%lu nx=%lu 1st stage nx=%lu nnzC=%d nnzD=%d\n", m_mz, m_nx, parent->m_nx, nnzC, nnzD); extractRows( Crow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), ineq_comp(), C->Amat->krowM(), C->Amat->jcolM(), C->Amat->M() ); extractRows( Drow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), ineq_comp(), C->Bmat->krowM(), C->Bmat->jcolM(), C->Bmat->M() ); } for(size_t it=0; it<children.size(); it++) { StochGenMatrix* child = ((sTreeImpl*)children[it])->createC(); C->AddChild(child); } return C; }
StochGenMatrix* sTreeImpl::createA() { //is this node a dead-end for this process? if(commWrkrs==MPI_COMM_NULL) return new StochGenDummyMatrix(m_id); StochGenMatrix* A = NULL; if (m_id==0) { CoinPackedMatrix Arow; Arow.reverseOrderedCopyOf( in.getFirstStageConstraints() ); assert(false==Arow.hasGaps()); // number of nz in the rows corresponding to eq constraints int nnzB=countNNZ( Arow, in.getFirstStageRowLB(), in.getFirstStageRowUB(), eq_comp()); //printf("%d -- 1st stage my=%lu nx=%lu nnzB=%d\n", commie, m_my, m_nx, nnzB); A = new StochGenMatrix( m_id, MY, N, m_my, 0, 0, // A does not exist for the root m_my, m_nx, nnzB, // B is 1st stage eq matrix commWrkrs ); extractRows( Arow, in.getFirstStageRowLB(), in.getFirstStageRowUB(), eq_comp(), A->Bmat->krowM(), A->Bmat->jcolM(), A->Bmat->M() ); } else { int scen=m_id-1; CoinPackedMatrix Arow, Brow; Arow.reverseOrderedCopyOf( in.getLinkingConstraints(scen) ); Brow.reverseOrderedCopyOf( in.getSecondStageConstraints(scen) ); int nnzA=countNNZ( Arow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), eq_comp() ); int nnzB=countNNZ( Brow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), eq_comp() ); A = new StochGenMatrix( m_id, MY, N, m_my, parent->m_nx, nnzA, m_my, m_nx, nnzB, commWrkrs ); //cout << commie << " -- 2nd stage my=" << m_my << " nx=" << m_nx // << " 1st stage nx=" << parent->m_nx << " nnzA=" << nnzA << " nnzB=" << nnzB << endl; extractRows( Arow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), eq_comp(), A->Amat->krowM(), A->Amat->jcolM(), A->Amat->M() ); extractRows( Brow, in.getSecondStageRowLB(scen), in.getSecondStageRowUB(scen), eq_comp(), A->Bmat->krowM(), A->Bmat->jcolM(), A->Bmat->M() ); } for(size_t it=0; it<children.size(); it++) { StochGenMatrix* child = ((sTreeImpl*)children[it])->createA(); A->AddChild(child); } return A; }
/** * Get state from rows/columns and save it into a struct. */ void extractRows(const AbstractItemModel& model, const AbstractItemModel::ItemIndex& startIndex, RemoveRowsCommandArgument::Type type, int count, RemoveRowsCommandArgument::ExtractedRowsStorage& o_data) { std::vector<ItemRole::Id> roleIds; { const auto roleNames = model.roles(); roleIds.reserve(roleNames.size()); for (const auto& roleName : roleNames) { roleIds.emplace_back(ItemRole::compute(roleName.c_str())); } } AbstractItemModel::ItemIndex index(startIndex); // Iterate through given range of rows (RemoveType::ROW) // otherwise iterating columns for (int row = 0; row < count; ++row) { // Iterate through all columns for the row (RemoveType::ROW) // otherwise iterating rows int columnCount = 0; if (type == RemoveRowsCommandArgument::Type::ROW) { columnCount = model.columnCount(startIndex.parent_); } else { columnCount = model.rowCount(startIndex.parent_); } for (int column = 0; column < columnCount; ++column) { const auto pItem = model.item(index); TF_ASSERT(pItem != nullptr); for (const auto& roleId : roleIds) { RemoveRowsCommandArgument::ExtractedRows extractedRows; extractedRows.data_ = pItem->getData(index.row_, index.column_, roleId); if (!extractedRows.data_.isVoid()) { ObjectHandle handle; extractedRows.data_.tryCast(handle); auto pInnerModel = handle.getBase<AbstractItemModel>(); if (pInnerModel != nullptr) { // Recursively extract const int innerRow = 0; const int innerColumn = 0; const AbstractItem* pInnerParent = nullptr; const AbstractItemModel::ItemIndex innerStartIndex(innerRow, innerColumn, pInnerParent); const int innerCount = pInnerModel->rowCount(pInnerParent); // Need to create a CollectionHolder, otherwise // extractedRows.data_ = innerData; // is unsafe, because it takes a reference // which will be deleted when innerData goes out of scope auto collectionHolder = std::make_shared<CollectionHolder<RemoveRowsCommandArgument::ExtractedRowsStorage>>(); auto& innerData = collectionHolder->storage(); innerData.reserve(innerCount); extractRows((*pInnerModel), innerStartIndex, type, innerCount, innerData); extractedRows.data_ = Collection(collectionHolder); } extractedRows.index_ = index; extractedRows.roleId_ = roleId; o_data.emplace_back(extractedRows); } } if (type == RemoveRowsCommandArgument::Type::ROW) { ++index.column_; } else { ++index.row_; } } if (type == RemoveRowsCommandArgument::Type::ROW) { ++index.row_; } else { ++index.column_; } } }