Example #1
0
bool NuTo::StructureBase::CheckHessian0_Submatrix(const BlockSparseMatrix& rHessian0, BlockSparseMatrix& rHessian0_CDF,
                                                  double rRelativeTolerance, bool rPrintWrongMatrices)
{
    int row, col;
    assert(rHessian0.GetNumRows() == rHessian0_CDF.GetNumRows());
    assert(rHessian0.GetNumColumns() == rHessian0_CDF.GetNumColumns());

    if (rHessian0.GetNumRows() == 0 or rHessian0.GetNumColumns() == 0)
        return true;

    bool isSubmatrixCorrect = true;
    Eigen::IOFormat fmt(Eigen::StreamPrecision, 0, " ", "\n", "|", " |");
    for (auto dofRow : GetDofStatus().GetActiveDofTypes())
    {
        for (auto dofCol : GetDofStatus().GetActiveDofTypes())
        {
            Eigen::MatrixXd hessian0_CDF_Full = rHessian0_CDF(dofRow, dofCol).ConvertToFullMatrix();

            double scaling = 1. / rHessian0_CDF(dofRow, dofCol).AbsMax();

            auto& diff = rHessian0_CDF(dofRow, dofCol);
            diff.AddScal(rHessian0(dofRow, dofCol), -1.);

            diff *= scaling;
            double error = diff.AbsMax(row, col);
            if (error > rRelativeTolerance)
            {
                GetLogger() << "[" << __FUNCTION__ << "] max error in (" << Node::DofToString(dofRow) << ","
                            << Node::DofToString(dofCol) << ") " << error << " at entry (" << row << "," << col
                            << ")\n";
                GetLogger() << "hessian0(" << row << "," << col
                            << ") = " << rHessian0(dofRow, dofCol).ConvertToFullMatrix()(row, col) << "\n";
                GetLogger() << "hessian0_CDF(" << row << "," << col << ") = " << hessian0_CDF_Full(row, col) << "\n";
                isSubmatrixCorrect = false;
                if (rPrintWrongMatrices)
                {
                    Eigen::MatrixXd diffPrint = diff.ConvertToFullMatrix();
                    GetLogger() << "####### relative difference\n" << diffPrint.format(fmt) << "\n";
                    GetLogger() << "####### hessian0\n"
                                << rHessian0(dofRow, dofCol).ConvertToFullMatrix().format(fmt) << "\n";
                    GetLogger() << "####### hessian0_CDF\n" << hessian0_CDF_Full.format(fmt) << "\n";
                }
            }
        }
    }
    return isSubmatrixCorrect;
}
Example #2
0
MaxEntropy::MaxEntropy( const double txTime, 
    const double bandwidthKhz, 
    Map const * const ptrMap, 
    CORRE_MA_OPE* ptrMatComputer, 
    ClusterStructure const * const ptrCS): 
    m_txTimePerSlot(txTime), m_bandwidthKhz(bandwidthKhz),
    m_ptrMap(ptrMap), 
    m_ptrCS(ptrCS), m_ptrMatComputer(ptrMatComputer),
    m_type("BranchBound")
{
  m_numNodes = m_ptrMap->GetNumNodes();
  m_numMaxHeads = m_ptrMap->GetNumInitHeads();
  m_maxPower = m_ptrMap->GetMaxPower();
  Eigen::IOFormat CleanFmt(2, 0, " ", "\n", "", "");
  m_A = Eigen::MatrixXd::Zero(m_numMaxHeads, m_numNodes); 
  m_B = Eigen::MatrixXd::Zero(m_numMaxHeads, m_numNodes); 
  m_C = Eigen::MatrixXd::Zero(m_numMaxHeads, m_numNodes);
  m_X = Eigen::MatrixXd::Zero(m_numMaxHeads, m_numNodes);
  for (int i = 0; i < m_numMaxHeads; ++i) {
    for (int j = 0; j < m_numNodes; ++j) {
     if (i == m_ptrCS->GetChIdxByName(j)) {
       m_A(i,j) = OmegaValue(j);
     }  
    }
  }
  double exponent = pow(2.0,m_ptrMap->GetIdtEntropy()/m_bandwidthKhz/m_txTimePerSlot);
  for (int i = 0; i < m_numMaxHeads; ++i) {
    for (int j = 0; j < m_numNodes; ++j) {
     if (i != m_ptrCS->GetChIdxByName(j)) {
       m_B(i,j) = (exponent-1) * m_maxPower * 
         m_ptrMap->GetGijByPair(m_ptrCS->GetVecHeadName()[i],j);
     }  
    }
  }
  for (int i = 0; i < m_numMaxHeads; ++i) {
    for (int j = 0; j < m_numNodes; ++j) {
      if (i == m_ptrCS->GetChIdxByName(j)) {
        int headName = m_ptrCS->GetVecHeadName()[i];
        m_C(i,j) = m_maxPower * m_ptrMap->GetGijByPair(headName,j) - 
          (exponent -1)*m_bandwidthKhz * m_ptrMap->GetNoise() - OmegaValue(j) ;
      }
    }
  }

  Eigen::MatrixXd tmp = m_A+m_B-m_C;
 
#ifdef DEBUG 
  cout << "================ m_A ================" << endl;
  cout << m_A.format(CleanFmt) << endl;
  cout << "================ m_B ================" << endl;
  cout << m_B.format(CleanFmt) << endl;
  cout << "================ m_C ================" << endl;
  cout << m_C.format(CleanFmt) << endl;
  cout << "================ m_A + m_B-m_C ================" << endl;
  cout << tmp.format(CleanFmt) << endl;
  cout << "Varince: " << m_ptrMatComputer->GetCorrationFactor() << endl;
#endif

  
  double corrFactor = m_ptrMatComputer->GetCorrationFactor();
  double variance = 1.0;
  m_Signma = Eigen::MatrixXd::Zero(m_numNodes, m_numNodes);
  for (int i = 0; i < m_numNodes; ++i) {
    for (int j = 0; j < m_numNodes; ++j) {
      m_Signma(i,j) = variance * exp(-1*(m_ptrMatComputer->GetDijSQByPair(i,j))/corrFactor) ;
    }
  }

  m_vecSched.resize(m_numNodes);
  fill(m_vecSched.begin(), m_vecSched.end(), 0);
}