Exemplo n.º 1
0
void ElementXMLTest::testBinaryData()
{
    soarxml::ElementXML* pXML1 = createXML1();
    soarxml::ElementXML* pXML2 = createXML2();
    soarxml::ElementXML* pXML4 = createXML4();
    soarxml::ElementXML* pXML5 = createXML5();

    pXML4->AddChild( pXML1 ) ;
    pXML4->AddChild( pXML2 ) ;
    pXML4->AddChild( pXML5 ) ;

    char* pStr = pXML4->GenerateXMLString( true ) ;
    soarxml::ElementXML* pParsedXML = soarxml::ElementXML::ParseXMLFromString( pStr ) ;

    CPPUNIT_ASSERT( pParsedXML != NULL ) ;
    CPPUNIT_ASSERT( pParsedXML->GetNumberChildren() == 3 ) ;

    soarxml::ElementXML child0(NULL) ;
    soarxml::ElementXML const* pChild0 = &child0 ;
    CPPUNIT_ASSERT( pParsedXML->GetChild(&child0, 0) );
    CPPUNIT_ASSERT( pChild0->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild0->GetTagName() ) == tag1 ) ;
    CPPUNIT_ASSERT( pChild0->GetCharacterData() != NULL );
    CPPUNIT_ASSERT( std::string( pChild0->GetCharacterData() ) == data1 ) ;
    CPPUNIT_ASSERT( pChild0->GetNumberAttributes() == 2 ) ;
    CPPUNIT_ASSERT( pChild0->GetNumberChildren() == 0 ) ;

    soarxml::ElementXML child1(NULL) ;
    soarxml::ElementXML const* pChild1 = &child1 ;
    CPPUNIT_ASSERT( pParsedXML->GetChild(&child1, 1) ) ;
    CPPUNIT_ASSERT( pChild1->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetTagName() ) == tag2 ) ;
    CPPUNIT_ASSERT( pChild1->GetCharacterData() != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetCharacterData() ) == data2 ) ;
    CPPUNIT_ASSERT( pChild0->GetNumberChildren() == 0 ) ;
    CPPUNIT_ASSERT( pChild1->GetAttribute( att21.c_str() ) != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetAttribute( att21.c_str() ) ) == val21 ) ;

    soarxml::ElementXML child2(NULL) ;
    soarxml::ElementXML const* pChild2 = &child2 ;
    CPPUNIT_ASSERT( pParsedXML->GetChild(&child2, 2) );
    CPPUNIT_ASSERT( pChild2->IsCharacterDataBinary() ) ;

    const char* pBuffer = pChild2->GetCharacterData() ;
    int bufferLen = pChild2->GetCharacterDataLength() ;

    CPPUNIT_ASSERT( bufferLen == BUFFER_LENGTH ) ;
    CPPUNIT_ASSERT( verifyBuffer( pBuffer ) ) ;

    soarxml::ElementXML::DeleteString(pStr) ;

    delete pXML4 ;
    delete pParsedXML ;
}
Exemplo n.º 2
0
void ElementXMLTest::testChildren()
{
    soarxml::ElementXML* pXML1 = createXML1();
    soarxml::ElementXML* pXML2 = createXML2();
    soarxml::ElementXML* pXML3 = createXML3();
    soarxml::ElementXML* pXML4 = createXML4();

    pXML4->AddChild( pXML1 );
    pXML4->AddChild( pXML2 );
    pXML4->AddChild( pXML3 );

    CPPUNIT_ASSERT( pXML4->GetNumberChildren() == 3 );

    soarxml::ElementXML child0( NULL ) ;
    soarxml::ElementXML const* pChild0 = &child0 ;

    CPPUNIT_ASSERT( pXML4->GetChild( &child0, 0 ) );
    CPPUNIT_ASSERT( pChild0->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild0->GetTagName() ) == tag1 );
    CPPUNIT_ASSERT( pChild0->GetCharacterData() != NULL );
    CPPUNIT_ASSERT( std::string( pChild0->GetCharacterData() ) == data1 );

    CPPUNIT_ASSERT( pChild0->GetNumberAttributes() == 2 );
    CPPUNIT_ASSERT( pChild0->GetNumberChildren() == 0 );

    // Let's put this one on the heap so we can control when we delete it.
    soarxml::ElementXML* pChild1Object = new soarxml::ElementXML( NULL );
    soarxml::ElementXML const* pChild1 = pChild1Object;

    CPPUNIT_ASSERT( pXML4->GetChild( pChild1Object, 1 ) );
    CPPUNIT_ASSERT( pChild1->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetTagName() ) == tag2 );
    CPPUNIT_ASSERT( pChild1->GetCharacterData() != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetCharacterData() ) == data2 );
    CPPUNIT_ASSERT( pChild0->GetNumberChildren() == 0 );
    CPPUNIT_ASSERT( pChild1->GetAttribute( att21.c_str() ) != NULL );
    CPPUNIT_ASSERT( std::string( pChild1->GetAttribute( att21.c_str() ) ) == val21 );

    //// This test is because I read online about looking up an element in an empty
    //// map causing an exception.  Need to make sure that doesn't happen in our
    //// attribute map implementation.
    soarxml::ElementXML child2(NULL);
    soarxml::ElementXML const* pChild2 = &child2;
    CPPUNIT_ASSERT ( pXML4->GetChild( &child2, 2 ) );
    CPPUNIT_ASSERT( pChild2->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild2->GetTagName() ) == tag3 );
    CPPUNIT_ASSERT( pChild2->GetAttribute( "missing" ) == NULL );

    soarxml::ElementXML test;
    CPPUNIT_ASSERT( pXML4->GetChild( &test, 3 ) == 0 );
    CPPUNIT_ASSERT( pXML4->GetChild( &test, -3 ) == 0 );

    // Create an XML string and print it out
    char* pStr = pXML4->GenerateXMLString( true );
    //printf(pStr) ;
    //printf("\n") ;
    pXML4->DeleteString( pStr );

    // Let's play a game.
    // Create another object pointing at the same internal handle
    soarxml::ElementXML* pChild1Alt = new soarxml::ElementXML( pChild1->GetXMLHandle() );
    pChild1Alt->AddRefOnHandle();

    // Delete the entire tree, releasing refs on the children
    delete pXML4;

    // We have to delete this other reference into the tree or its
    // not a proper test of pChild1Alt.  (If pChild1Object was on the stack
    // it wouldn't be deleted yet and of course we could talk to the child).
    delete pChild1Object;

    // Since we added a ref to pChild1 it should still exist
    CPPUNIT_ASSERT( pChild1Alt->GetTagName() != NULL );
    CPPUNIT_ASSERT( std::string( pChild1Alt->GetTagName() ) == tag2 );
    CPPUNIT_ASSERT( pChild1Alt->ReleaseRefOnHandle() == 0 ) ;

    delete pChild1Alt;
}
Exemplo n.º 3
0
BCP_branching_decision MCF1_lp::
select_branching_candidates(const BCP_lp_result& lpres,
			    const BCP_vec<BCP_var*>& vars,
			    const BCP_vec<BCP_cut*>& cuts,
			    const BCP_lp_var_pool& local_var_pool,
			    const BCP_lp_cut_pool& local_cut_pool,
			    BCP_vec<BCP_lp_branching_object*>& cands,
			    bool force_branch)
{
  if (generated_vars > 0) {
    return BCP_DoNotBranch;
  }

  if (lpres.objval() > upper_bound() - 1e-6) {
    return BCP_DoNotBranch_Fathomed;
  }

  int i, j;

  const int dummyStart = par.entry(MCF1_par::AddDummySourceSinkArcs) ?
    data.numarcs - data.numcommodities : data.numarcs;
		
  // find a few fractional original variables and do strong branching on them
  for (i = data.numcommodities-1; i >= 0; --i) {
    std::map<int,double>& f = flows[i];
    int most_frac_ind = -1;
    double most_frac_val = 0.5-1e-6;
    double frac_val = 0.0;
    for (std::map<int,double>::iterator fi=f.begin(); fi != f.end(); ++fi){
      if (fi->first >= dummyStart)
	continue;
      const double frac = fabs(fi->second - floor(fi->second) - 0.5);
      if (frac < most_frac_val) {
	most_frac_ind = fi->first;
	most_frac_val = frac;
	frac_val = fi->second;
      }
    }
    if (most_frac_ind >= 0) {
      BCP_vec<BCP_var*> new_vars;
      BCP_vec<int> fvp;
      BCP_vec<double> fvb;
      int lb = data.arcs[most_frac_ind].lb;
      int ub = data.arcs[most_frac_ind].ub;
      for (j = branch_history[i].size() - 1; j >= 0; --j) {
	// To correctly set lb/ub we need to check whether we have
	// already branched on this arc
	const MCF1_branch_decision& h = branch_history[i][j];
	if (h.arc_index == most_frac_ind) {
	  lb = CoinMax(lb, h.lb);
	  ub = CoinMin(ub, h.ub);
	}
      }
      const int mid = static_cast<int>(floor(frac_val));
      new_vars.push_back(new MCF1_branching_var(i, most_frac_ind,
						lb, mid, mid+1, ub));
      // Look at the consequences of of this branch
      BCP_vec<int> child0_pos, child1_pos;
      BCP_vec<double> child0_bd, child1_bd;

      MCF1_branch_decision child0(most_frac_ind, lb, mid);
      MCF1_adjust_bounds(vars, i, child0, child0_pos, child0_bd);

      MCF1_branch_decision child1(most_frac_ind, mid+1, ub);
      MCF1_adjust_bounds(vars, i, child1, child1_pos, child1_bd);

      // Now put together the changes
      fvp.push_back(-1);
      fvp.append(child0_pos);
      fvp.append(child1_pos);
      fvb.push_back(0.0);
      fvb.push_back(0.0);
      fvb.append(child0_bd);
      for (j = child1_pos.size() - 1; j >= 0; --j) {
	fvb.push_back(0.0);
	fvb.push_back(1.0);
      }
      fvb.push_back(1.0);
      fvb.push_back(1.0);
      for (j = child0_pos.size() - 1; j >= 0; --j) {
	fvb.push_back(0.0);
	fvb.push_back(1.0);
      }
      fvb.append(child1_bd);
      cands.push_back(new BCP_lp_branching_object(2, // num of children
						  &new_vars,
						  NULL, // no new cuts
						  &fvp,NULL,&fvb,NULL,
						  NULL,NULL,NULL,NULL));
    }
  }
  return BCP_DoBranch;
}