Esempio n. 1
0
File: pia.c Progetto: jjgreen/pia
static void range(const point_t *x, size_t c, box_t *B)
{
  while (c--)
    {
      bd(&(B->min.x), x[c].x);
      bu(&(B->max.x), x[c].x);
      bd(&(B->min.y), x[c].y);
      bu(&(B->max.y), x[c].y);
    }
}
int main(int argc, char *argv[])
{
  //  cout  << "argc:" << argc <<endl;

  QString classes = "ALL";

  if (argc!=4)
    usage(argv[0]);
  BottomUp bu(argv[1]); // read infile tnt.uni-hannover.de/

  NodeList *nl = bu.selectClass("ALL");
  qDebug("no elem %d",nl->count());
  if (nl->isEmpty()) return 0;

  Stack stack(nl,argv[2]);
  QString command(argv[3]);

  qDebug("Command=%s",command.latin1());
  stack.registerFunction("neighbour", neighbour);
  if (!stack.run(command)){
    std::cerr << "ga_bu_generic: Error while running stack" << std::endl;
    return EXIT_FAILURE;
  }

  stack.write(argv[2]);
  nl->writeOutImage(QString(argv[2])+".bu.node.plm");
  nl->writeGroupImage(QString(argv[2])+".bu.plm");


}
int main() {
    while(scanf("%d%d",&n,&m) != EOF) {
        solve();
        bu();
    }
    return 0;
}
Esempio n. 4
0
	/*! \brief constructor
	 *
	 * \param dom Box domain
	 * \param n_sub number of sub-domain to create (it is approximated to the biggest 2^n number)
	 * \param lp Local position of the particles
	 *
	 */
	ORB(Box dom, size_t n_sub, loc_pos & lp)
	:v_cl(create_vcluster()),lp(lp)
	{
		typedef ORB<dim,T,loc_wg,loc_pos,Box,Tree> ORB_class;

		dim_div = 0;

		n_sub = openfpm::math::round_big_2(n_sub);
		size_t nsub = log2(n_sub);

		// number of center or mass needed
		cm.resize(2 << nsub);
		cm_cnt.resize(2 << nsub);

		// Every time we divide from 0 to dim-1, calculate how many cycle from 0 to dim-1 we have
		size_t dim_cycle = nsub / dim;

		// Create the root node in the graph
		grp.addVertex();

		// unroll bisection cycle
		bisect_unroll<dim,ORB_class> bu(*this);
		for (size_t i = 0 ; i < dim_cycle ; i++)
		{
			boost::mpl::for_each< boost::mpl::range_c<int,0,dim> >(bu);
			// bu is recreated several time internaly
		}

		// calculate and execute the remaining cycles
		switch(nsub - dim_cycle * dim)
		{
		case 1:
			do_when_dim_gr_i<dim,1,ORB_class>::bisect_loop(bu);
			break;
		case 2:
			do_when_dim_gr_i<dim,2,ORB_class>::bisect_loop(bu);
			break;
		case 3:
			do_when_dim_gr_i<dim,3,ORB_class>::bisect_loop(bu);
			break;
		case 4:
			do_when_dim_gr_i<dim,4,ORB_class>::bisect_loop(bu);
			break;
		case 5:
			do_when_dim_gr_i<dim,5,ORB_class>::bisect_loop(bu);
			break;
		case 6:
			do_when_dim_gr_i<dim,6,ORB_class>::bisect_loop(bu);
			break;
		case 7:
			do_when_dim_gr_i<dim,7,ORB_class>::bisect_loop(bu);
			break;
		default:
			// To extend on higher dimension create other cases or create runtime
			// version of local_cm and bisect
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " ORB is not working for dimension bigger than 8";

		}
	}
Esempio n. 5
0
double bright::Reactor1G::batch_average(double BUd, std::string PDk_flag)
{
    // Finds the batch-averaged P(F), D(F), or k(F) when at discharge burnup BUd.
    std::string PDk = pyne::to_upper(PDk_flag);

    // For B batches (indexed from 0 -> B-1) calculate BU of each batch.
    std::vector<double> bu (B, 0.0);
    for (int b = 0; b < B; b++)
        bu[b] = ((double) b + 1) * BUd / ((double) B);

    // Calculate Fluence points for each BU
    std::vector<FluencePoint> fps;
    fps.resize(B);
    for (int b = 0; b < B; b++)
        fps[b] = fluence_at_BU(bu[b]);

    std::vector<double> PDks (B, 0.0);
    for (int b = 0; b < B; b++)
    {
        double p;
        double d;

        if ( (fps[b].f + 1) == F.size() )
        {
            p = pyne::solve_line(fps[b].F, F[fps[b].f], P_F_[fps[b].f], F[fps[b].f-1], P_F_[fps[b].f-1]);
            d = pyne::solve_line(fps[b].F, F[fps[b].f], D_F_[fps[b].f], F[fps[b].f-1], D_F_[fps[b].f-1]);
        }
        else
        {
            p = pyne::solve_line(fps[b].F, F[fps[b].f+1], P_F_[fps[b].f+1], F[fps[b].f], P_F_[fps[b].f]);
            d = pyne::solve_line(fps[b].F, F[fps[b].f+1], D_F_[fps[b].f+1], F[fps[b].f], D_F_[fps[b].f]);
        };

        if (PDk == "K")
            PDks[b] = (p/d);
        else if (PDk == "P")
            PDks[b] = p;
        else if (PDk == "D")
            PDks[b] = d;
        else
        {
            if (1 < bright::verbosity)
                std::cout << "PDk flag is wrong: " << PDk << "\nUsing default of k.\n";
            PDks[b] = (p/d);
        };
    };

    double numerator = 0.0;
    double denominator = 0.0;
    for (int b = 0; b < B; b++)
    {
        numerator   = numerator   + (PDks[b] / fps[b].m);
        denominator = denominator + (1.0 / fps[b].m);
    };

    return numerator/denominator;
};
Esempio n. 6
0
br_status fpa_rewriter::mk_to_sbv_unspecified(unsigned ebits, unsigned sbits, unsigned width, expr_ref & result) {
    bv_util bu(m());
    if (m_hi_fp_unspecified)
        // The "hardware interpretation" is 0.
        result = bu.mk_numeral(0, width);
    else
        result = m_util.mk_internal_to_sbv_unspecified(ebits, sbits, width);

    return BR_DONE;
}
Esempio n. 7
0
void fpa2bv_model_converter::convert(model * bv_mdl, model * float_mdl) {
    float_util fu(m);
    bv_util bu(m);
    mpf fp_val;
    unsynch_mpz_manager & mpzm = fu.fm().mpz_manager();
    unsynch_mpq_manager & mpqm = fu.fm().mpq_manager();

    TRACE("fpa2bv_mc", tout << "BV Model: " << std::endl;
    for (unsigned i = 0; i < bv_mdl->get_num_constants(); i++)
        tout << bv_mdl->get_constant(i)->get_name() << " --> " <<
        mk_ismt2_pp(bv_mdl->get_const_interp(bv_mdl->get_constant(i)), m) << std::endl;
    );
Esempio n. 8
0
br_status fpa_rewriter::mk_to_ieee_bv_unspecified(func_decl * f, expr_ref & result) {
    bv_util bu(m());
    unsigned bv_sz = bu.get_bv_size(f->get_range());

    if (m_hi_fp_unspecified)
        // The "hardware interpretation" is 0.
        result = bu.mk_numeral(0, bv_sz);
    else
        result = m_util.mk_internal_to_ieee_bv_unspecified(bv_sz);

    return BR_DONE;
}
RestorationProblem::RestorationProblem( Problemspec *parentProblem, const Matrix &xiReference )
{
    int i, iVar, iCon;

    parent = parentProblem;
    xiRef.Dimension( parent->nVar );
    for( i=0; i<parent->nVar; i++)
        xiRef( i ) = xiReference( i );

    /* nCon slack variables */
    nVar = parent->nVar + parent->nCon;
    nCon = parent->nCon;

    /* Block structure: One additional block for every slack variable */
    nBlocks = parent->nBlocks+nCon;
    blockIdx = new int[nBlocks+1];
    for( i=0; i<parent->nBlocks+1; i++ )
        blockIdx[i] = parent->blockIdx[i];
    for( i=parent->nBlocks+1; i<nBlocks+1; i++ )
        blockIdx[i] = blockIdx[i-1]+1;

    /* Set bounds */
    objLo = 0.0;
    objUp = 1.0e20;

    bl.Dimension( nVar + nCon ).Initialize( -1.0e20 );
    bu.Dimension( nVar + nCon ).Initialize( 1.0e20 );
    for( iVar=0; iVar<parent->nVar; iVar++ )
    {
        bl( iVar ) = parent->bl( iVar );
        bu( iVar ) = parent->bu( iVar );
    }

    for( iCon=0; iCon<parent->nCon; iCon++ )
    {
        bl( nVar+iCon ) = parent->bl( parent->nVar+iCon );
        bu( nVar+iCon ) = parent->bu( parent->nVar+iCon );
    }
}
Esempio n. 10
0
br_status fpa_rewriter::mk_to_ieee_bv_unspecified(func_decl * f, expr_ref & result) {
    SASSERT(f->get_num_parameters() == 1);
    SASSERT(f->get_parameter(0).is_int());
    unsigned bv_sz = f->get_parameter(0).get_int();

    bv_util bu(m());
    if (m_hi_fp_unspecified)
        // The "hardware interpretation" is 0.
        result = bu.mk_numeral(0, bv_sz);
    else
        result = m_util.mk_internal_to_ieee_bv_unspecified(bv_sz);

    return BR_DONE;
}
Esempio n. 11
0
br_status fpa_rewriter::mk_to_fp(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
    SASSERT(f->get_num_parameters() == 2);
    SASSERT(f->get_parameter(0).is_int());
    SASSERT(f->get_parameter(1).is_int());
    bv_util bu(m());
    scoped_mpf v(m_fm);
    mpf_rounding_mode rmv;
    rational r1, r2, r3;
    unsigned bvs1, bvs2, bvs3;
    unsigned ebits = f->get_parameter(0).get_int();
    unsigned sbits = f->get_parameter(1).get_int();

    if (num_args == 1) {
        if (bu.is_numeral(args[0], r1, bvs1)) {
            // BV -> float
            SASSERT(bvs1 == sbits + ebits);
            unsynch_mpz_manager & mpzm = m_fm.mpz_manager();
            unsynch_mpq_manager & mpqm = m_fm.mpq_manager();
            scoped_mpz sig(mpzm), exp(mpzm);

            const mpz & sm1 = m_fm.m_powers2(sbits - 1);
            const mpz & em1 = m_fm.m_powers2(ebits);

            scoped_mpq q(mpqm);
            mpqm.set(q, r1.to_mpq());
            SASSERT(mpzm.is_one(q.get().denominator()));
            scoped_mpz z(mpzm);
            z = q.get().numerator();

            mpzm.rem(z, sm1, sig);
            mpzm.div(z, sm1, z);

            mpzm.rem(z, em1, exp);
            mpzm.div(z, em1, z);

            SASSERT(mpzm.is_int64(exp));
            mpf_exp_t mpf_exp = mpzm.get_int64(exp);
            mpf_exp = m_fm.unbias_exp(ebits, mpf_exp);

            m_fm.set(v, ebits, sbits, !mpzm.is_zero(z), sig, mpf_exp);
            TRACE("fp_rewriter",
                  tout << "sgn: " << !mpzm.is_zero(z) << std::endl;
                  tout << "sig: " << mpzm.to_string(sig) << std::endl;
                  tout << "exp: " << mpf_exp << std::endl;
                  tout << "v: " << m_fm.to_string(v) << std::endl;);

            result = m_util.mk_value(v);
            return BR_DONE;
        }
inline void load_img( MP img, QImage& qimg)
{
    QString src = img[ "img.src" ];
    if(QFile::exists(src)) {
        qimg.load(src);
    }
    else {
        src.remove(0,22);           // Remove MIME type
        QByteArray b6;
        b6.append(src);
        QByteArray ba = QByteArray::fromBase64(b6);
        QBuffer bu(&ba);
        qimg.load(&bu,"PNG");
    }
}
Esempio n. 13
0
int main(int argc,char** argv){
double prec = 1e-6;
Buca bu(0);
FunzioneBase* t = &(bu);
Bisezione bis(3000);
bis.Precision(prec);
double a, b, zero;
for (int i=0;i<21;++i){
	a=i*M_PI;
	b=i*M_PI+M_PI/2;
	zero = bis.CercaZeri(a,b,&bu);
	std::cout << std::fixed << "Zero trovato: " << std::setprecision(-log10(prec)) << zero << " nell'intervallo ["<<a<<","<<b<<"]"<<std::endl;
}

return 0;
}
  /**
   * \fn void comp_bpoly_matrix( double**& a , double* param_pts , unsigned np ) const
   *
   * \brief Computes the pairwise product of all Bernstein polynomials
   * of  two degrees  at a  given set  of parameters  points.  The two
   * degrees are  the the first and  second index of  the bi-degree of
   * this patch.
   *
   * \param a A (np x (( m + 1) * ( n + 1)) matrix such that ( m , n )
   * is the bi-degree of this patch  and each element of the matrix is
   * a  product of  two  Bernstein polynomials  at  a given  parameter
   * point.
   * \param patch_pts An array of 2D coordinates of parameter points.
   * \param np The number of parameter points.
   */
  void
  tBezier::comp_bpoly_matrix(
			     double**& a ,
			     double* param_pts , 
			     unsigned np 
			    ) 
      const
  {
    /**
     * Compute the value of the Bernstein polynomials at the parameter
     * points.
     */
    double rx = get_aff_1st_point_x_coord() ;
    double ry = get_aff_1st_point_y_coord() ;
    double sx = get_aff_2nd_point_x_coord() ;
    double sy = get_aff_2nd_point_y_coord() ;

    for ( unsigned i = 0 ; i < np ; i++ ) {
      double x = ( param_pts[ ( i << 1 )     ] - rx ) / 
	( sx - rx ) ;

      double y = ( param_pts[ ( i << 1 ) + 1 ] - ry ) / 
	( sy - ry ) ;

      unsigned m = get_bidegree_1st_index() ;
      unsigned n = get_bidegree_2nd_index() ;

      std::vector< double > bu( m + 1 ) ;
      std::vector< double > bv( n + 1 ) ;

      all_bernstein( m , x , bu ) ;
      all_bernstein( n , y , bv ) ;

      for ( unsigned k = 0 ; k <= n ; ++k ) {
        for ( unsigned j = 0 ; j <= m ; ++j ) {
          unsigned l = index( k , j ) ;
          a[ i ][ l ] = bu[ j ] * bv[ k ] ;
        }
      }
    }

    return ;
  }
int main(int argc, char *argv[])
{
  //cout  << "argc:" << argc <<endl;
  QString classes = "ALL";
  if (argc < 4) // || argc > 5 )
    usage(argv[0]);
  if (argc >= 5) classes = argv[4];

	unsigned int maxsize = atoi(argv[3]);
  BottomUp bu(argv[1]); // read infile
  //BottomUp bu("/project/geoaida/share/data/test_files/ga_bu_maxsize/input-27656-0171");
	NodeList *nl = bu.selectClass(classes); //select given classes
	RegionSensor *rs = new RegionSensor();
	for (int n=5; n<argc; n++) {
	  cout <<argv[n]<<",   ";
		*nl += (*(bu.selectClass(argv[n])));
	}
	nl->calcForSelect ("size", rs);
	
	Node *node1;
	QDictIterator<Node> it( *nl ); // iterator for nl
  QFile fp(argv[2]);
  //QFile fp("/project/geoaida/share/data/test_files/ga_bu_maxsize/outfile");
  if (!fp.open(IO_WriteOnly)) qDebug("write: file not accesable to %s\n",argv[2]);
  QTextStream str(&fp);
	str<<"<REGION\n"; 	
  while(it.current()) { //run over list
 		node1=nl->take(it.currentKey()); //it.current();
		unsigned int size = ((*node1)["size"])->toUInt();
		if (size<=maxsize) node1->write(str);
		++it;
		it.toFirst();
	}	
	str<<"/>\n";
	fp.close();

	cout << "FIN .. "<<endl;
#ifdef WIN32
  return(0);
#endif

}
Esempio n. 16
0
main()
{
    char s[100000] ;
    bool fir=1 ;
    while(scanf("%s",s)!=EOF)
    {
        if(fir) fir=0 ;
        else printf("\n") ;
        int l=strlen(s) ;
        int n=1000*(s[l-4]-'0')+100*(s[l-3]-'0')+10*(s[l-2]-'0')+(s[l-1]-'0') ;
        int x=an(n),y=hu(s,l),z=(bu(s,l))&&(an(n)) ;
        if((!x) && (!y) && (!z))
            printf("This is an ordinary year.\n") ;
        else
        {
            if(x) printf("This is leap year.\n") ;
            if(y) printf("This is huluculu festival year.\n") ;
            if(z) printf("This is bulukulu festival year.\n") ;
        }
    }
}
Esempio n. 17
0
void FLCheckBox::drawButton(QPainter *p)
{
  QRect rect, wrect(this->rect());
  rect.setRect((wrect.width() - 13) / 2, (wrect.height() - 13) / 2, 13, 13);

  if (state() == QButton::On) {
    QBrush bu(green, SolidPattern);
    p->fillRect(0, 0, wrect.width() - 1, wrect.height() - 1, bu);
  }

  QRect irect = QStyle::visualRect(rect, this);
  p->fillRect(irect, Qt::white);
  p->drawRect(irect);

  if (state() == QButton::On) {
    QPointArray a(7 * 2);
    int i, xx, yy;
    xx = irect.x() + 3;
    yy = irect.y() + 5;

    for (i = 0; i < 3; i++) {
      a.setPoint(2 * i,   xx, yy);
      a.setPoint(2 * i + 1, xx, yy + 2);
      xx++;
      yy++;
    }

    yy -= 2;
    for (i = 3; i < 7; i++) {
      a.setPoint(2 * i,   xx, yy);
      a.setPoint(2 * i + 1, xx, yy + 2);
      xx++;
      yy--;
    }

    p->drawLineSegments(a);
  }

  drawButtonLabel(p);
}
expr_ref fpa2bv_model_converter_prec::convert_bv2fp(sort * s, expr * sgn, expr * exp, expr * sig) const {
    fpa_util fu(m);
    bv_util bu(m);
    unsynch_mpz_manager & mpzm = fu.fm().mpz_manager();
    unsynch_mpq_manager & mpqm = fu.fm().mpq_manager();

    expr_ref res(m);
    mpf fp_val;

    unsigned ebits = fu.get_ebits(s);
    unsigned sbits = fu.get_sbits(s);

    unsigned sgn_sz = 1;
    unsigned exp_sz = ebits;
    unsigned sig_sz = sbits - 1;

    rational sgn_q(0), sig_q(0), exp_q(0);

    if (sgn) bu.is_numeral(sgn, sgn_q, sgn_sz);
    if (exp) bu.is_numeral(exp, exp_q, exp_sz);
    if (sig) bu.is_numeral(sig, sig_q, sig_sz);

    // un-bias exponent
    rational exp_unbiased_q;
    exp_unbiased_q = exp_q - fu.fm().m_powers2.m1(ebits - 1);

    mpz sig_z; mpf_exp_t exp_z;
    mpzm.set(sig_z, sig_q.to_mpq().numerator());
    exp_z = mpzm.get_int64(exp_unbiased_q.to_mpq().numerator());

    fu.fm().set(fp_val, ebits, sbits, !mpqm.is_zero(sgn_q.to_mpq()), exp_z, sig_z);

    mpzm.del(sig_z);

    res = fu.mk_value(fp_val);

    TRACE("fpa2bv_mc", tout << "[" << mk_ismt2_pp(sgn, m) <<
          " " << mk_ismt2_pp(exp, m) <<
          " " << mk_ismt2_pp(sig, m) << "] == " <<
          mk_ismt2_pp(res, m) << std::endl;);
Esempio n. 19
0
// ** Temporary version
int
ClpPdco::pdco( ClpPdcoBase * stuff, Options &options, Info &info, Outfo &outfo)
{
//    D1, D2 are positive-definite diagonal matrices defined from d1, d2.
//           In particular, d2 indicates the accuracy required for
//           satisfying each row of Ax = b.
//
// D1 and D2 (via d1 and d2) provide primal and dual regularization
// respectively.  They ensure that the primal and dual solutions
// (x,r) and (y,z) are unique and bounded.
//
// A scalar d1 is equivalent to d1 = ones(n,1), D1 = diag(d1).
// A scalar d2 is equivalent to d2 = ones(m,1), D2 = diag(d2).
// Typically, d1 = d2 = 1e-4.
// These values perturb phi(x) only slightly  (by about 1e-8) and request
// that A*x = b be satisfied quite accurately (to about 1e-8).
// Set d1 = 1e-4, d2 = 1 for least-squares problems with bound constraints.
// The problem is then
//
//    minimize    phi(x) + 1/2 norm(d1*x)^2 + 1/2 norm(A*x - b)^2
//    subject to  bl <= x <= bu.
//
// More generally, d1 and d2 may be n and m vectors containing any positive
// values (preferably not too small, and typically no larger than 1).
// Bigger elements of d1 and d2 improve the stability of the solver.
//
// At an optimal solution, if x(j) is on its lower or upper bound,
// the corresponding z(j) is positive or negative respectively.
// If x(j) is between its bounds, z(j) = 0.
// If bl(j) = bu(j), x(j) is fixed at that value and z(j) may have
// either sign.
//
// Also, r and y satisfy r = D2 y, so that Ax + D2^2 y = b.
// Thus if d2(i) = 1e-4, the i-th row of Ax = b will be satisfied to
// approximately 1e-8.  This determines how large d2(i) can safely be.
//
//
// EXTERNAL FUNCTIONS:
// options         = pdcoSet;                  provided with pdco.m
// [obj,grad,hess] = pdObj( x );               provided by user
//               y = pdMat( name,mode,m,n,x ); provided by user if pdMat
//                                             is a string, not a matrix
//
// INPUT ARGUMENTS:
// pdObj      is a string containing the name of a function pdObj.m
//            or a function_handle for such a function
//            such that [obj,grad,hess] = pdObj(x) defines
//            obj  = phi(x)              : a scalar,
//            grad = gradient of phi(x)  : an n-vector,
//            hess = diag(Hessian of phi): an n-vector.
//         Examples:
//            If phi(x) is the linear function c"x, pdObj should return
//               [obj,grad,hess] = [c"*x, c, zeros(n,1)].
//            If phi(x) is the entropy function E(x) = sum x(j) log x(j),
//               [obj,grad,hess] = [E(x), log(x)+1, 1./x].
// pdMat      may be an ifexplicit m x n matrix A (preferably sparse!),
//            or a string containing the name of a function pdMat.m
//            or a function_handle for such a function
//            such that y = pdMat( name,mode,m,n,x )
//            returns   y = A*x (mode=1)  or  y = A"*x (mode=2).
//            The input parameter "name" will be the string pdMat.
// b          is an m-vector.
// bl         is an n-vector of lower bounds.  Non-existent bounds
//            may be represented by bl(j) = -Inf or bl(j) <= -1e+20.
// bu         is an n-vector of upper bounds.  Non-existent bounds
//            may be represented by bu(j) =  Inf or bu(j) >=  1e+20.
// d1, d2     may be positive scalars or positive vectors (see above).
// options    is a structure that may be set and altered by pdcoSet
//            (type help pdcoSet).
// x0, y0, z0 provide an initial solution.
// xsize, zsize are estimates of the biggest x and z at the solution.
//            They are used to scale (x,y,z).  Good estimates
//            should improve the performance of the barrier method.
//
//
// OUTPUT ARGUMENTS:
// x          is the primal solution.
// y          is the dual solution associated with Ax + D2 r = b.
// z          is the dual solution associated with bl <= x <= bu.
// inform = 0 if a solution is found;
//        = 1 if too many iterations were required;
//        = 2 if the linesearch failed too often.
// PDitns     is the number of Primal-Dual Barrier iterations required.
// CGitns     is the number of Conjugate-Gradient  iterations required
//            if an iterative solver is used (LSQR).
// time       is the cpu time used.
//----------------------------------------------------------------------

// PRIVATE FUNCTIONS:
//    pdxxxbounds
//    pdxxxdistrib
//    pdxxxlsqr
//    pdxxxlsqrmat
//    pdxxxmat
//    pdxxxmerit
//    pdxxxresid1
//    pdxxxresid2
//    pdxxxstep
//
// GLOBAL VARIABLES:
//    global pdDDD1 pdDDD2 pdDDD3
//
//
// NOTES:
// The matrix A should be reasonably well scaled: norm(A,inf) =~ 1.
// The vector b and objective phi(x) may be of any size, but ensure that
// xsize and zsize are reasonably close to norm(x,inf) and norm(z,inf)
// at the solution.
//
// The files defining pdObj  and pdMat
// must not be called Fname.m or Aname.m!!
//
//
// AUTHOR:
//    Michael Saunders, Systems Optimization Laboratory (SOL),
//    Stanford University, Stanford, California, USA.
//    [email protected]
//
// CONTRIBUTORS:
//    Byunggyoo Kim, SOL, Stanford University.
//    [email protected]
//
// DEVELOPMENT:
// 20 Jun 1997: Original version of pdsco.m derived from pdlp0.m.
// 29 Sep 2002: Original version of pdco.m  derived from pdsco.m.
//              Introduced D1, D2 in place of gamma*I, delta*I
//              and allowed for general bounds bl <= x <= bu.
// 06 Oct 2002: Allowed for fixed variabes: bl(j) = bu(j) for any j.
// 15 Oct 2002: Eliminated some work vectors (since m, n might be LARGE).
//              Modularized residuals, linesearch
// 16 Oct 2002: pdxxx..., pdDDD... names rationalized.
//              pdAAA eliminated (global copy of A).
//              Aname is now used directly as an ifexplicit A or a function.
//              NOTE: If Aname is a function, it now has an extra parameter.
// 23 Oct 2002: Fname and Aname can now be function handles.
// 01 Nov 2002: Bug fixed in feval in pdxxxmat.
//-----------------------------------------------------------------------

//  global pdDDD1 pdDDD2 pdDDD3
     double inf = 1.0e30;
     double eps = 1.0e-15;
     double atolold = -1.0, r3ratio = -1.0, Pinf, Dinf, Cinf, Cinf0;

     printf("\n   --------------------------------------------------------");
     printf("\n   pdco.m                            Version of 01 Nov 2002");
     printf("\n   Primal-dual barrier method to minimize a convex function");
     printf("\n   subject to linear constraints Ax + r = b,  bl <= x <= bu");
     printf("\n   --------------------------------------------------------\n");

     int m = numberRows_;
     int n = numberColumns_;
     bool ifexplicit = true;

     CoinDenseVector<double> b(m, rhs_);
     CoinDenseVector<double> x(n, x_);
     CoinDenseVector<double> y(m, y_);
     CoinDenseVector<double> z(n, dj_);
     //delete old arrays
     delete [] rhs_;
     delete [] x_;
     delete [] y_;
     delete [] dj_;
     rhs_ = NULL;
     x_ = NULL;
     y_ = NULL;
     dj_ = NULL;

     // Save stuff so available elsewhere
     pdcoStuff_ = stuff;

     double normb  = b.infNorm();
     double normx0 = x.infNorm();
     double normy0 = y.infNorm();
     double normz0 = z.infNorm();

     printf("\nmax |b | = %8g     max |x0| = %8g", normb , normx0);
     printf(                "      xsize   = %8g", xsize_);
     printf("\nmax |y0| = %8g     max |z0| = %8g", normy0, normz0);
     printf(                "      zsize   = %8g", zsize_);

     //---------------------------------------------------------------------
     // Initialize.
     //---------------------------------------------------------------------
     //true   = 1;
     //false  = 0;
     //zn     = zeros(n,1);
     //int nb     = n + m;
     int CGitns = 0;
     int inform = 0;
     //---------------------------------------------------------------------
     //  Only allow scalar d1, d2 for now
     //---------------------------------------------------------------------
     /*
     if (d1_->size()==1)
         d1_->resize(n, d1_->getElements()[0]);  // Allow scalar d1, d2
     if (d2_->size()==1)
         d2->resize(m, d2->getElements()[0]);  // to mean dk * unit vector
      */
     assert (stuff->sizeD1() == 1);
     double d1 = stuff->getD1();
     double d2 = stuff->getD2();

     //---------------------------------------------------------------------
     // Grab input options.
     //---------------------------------------------------------------------
     int  maxitn    = options.MaxIter;
     double featol    = options.FeaTol;
     double opttol    = options.OptTol;
     double steptol   = options.StepTol;
     int  stepSame  = 1;  /* options.StepSame;   // 1 means stepx == stepz */
     double x0min     = options.x0min;
     double z0min     = options.z0min;
     double mu0       = options.mu0;
     int  LSproblem = options.LSproblem;  // See below
     int  LSmethod  = options.LSmethod;   // 1=Cholesky    2=QR    3=LSQR
     int  itnlim    = options.LSQRMaxIter * CoinMin(m, n);
     double atol1     = options.LSQRatol1;  // Initial  atol
     double atol2     = options.LSQRatol2;  // Smallest atol,unless atol1 is smaller
     double conlim    = options.LSQRconlim;
     //int  wait      = options.wait;

     // LSproblem:
     //  1 = dy          2 = dy shifted, DLS
     // 11 = s          12 =  s shifted, DLS    (dx = Ds)
     // 21 = dx
     // 31 = 3x3 system, symmetrized by Z^{1/2}
     // 32 = 2x2 system, symmetrized by X^{1/2}

     //---------------------------------------------------------------------
     // Set other parameters.
     //---------------------------------------------------------------------
     int  kminor    = 0;      // 1 stops after each iteration
     double eta       = 1e-4;   // Linesearch tolerance for "sufficient descent"
     double maxf      = 10;     // Linesearch backtrack limit (function evaluations)
     double maxfail   = 1;      // Linesearch failure limit (consecutive iterations)
     double bigcenter = 1e+3;   // mu is reduced if center < bigcenter.

     // Parameters for LSQR.
     double atolmin   = eps;    // Smallest atol if linesearch back-tracks
     double btol      = 0;      // Should be small (zero is ok)
     double show      = false;  // Controls lsqr iteration log
     /*
     double gamma     = d1->infNorm();
     double delta     = d2->infNorm();
     */
     double gamma = d1;
     double delta = d2;

     printf("\n\nx0min    = %8g     featol   = %8.1e", x0min, featol);
     printf(                  "      d1max   = %8.1e", gamma);
     printf(  "\nz0min    = %8g     opttol   = %8.1e", z0min, opttol);
     printf(                  "      d2max   = %8.1e", delta);
     printf(  "\nmu0      = %8.1e     steptol  = %8g", mu0  , steptol);
     printf(                  "     bigcenter= %8g"  , bigcenter);

     printf("\n\nLSQR:");
     printf("\natol1    = %8.1e     atol2    = %8.1e", atol1 , atol2 );
     printf(                  "      btol    = %8.1e", btol );
     printf("\nconlim   = %8.1e     itnlim   = %8d"  , conlim, itnlim);
     printf(                  "      show    = %8g"  , show );

// LSmethod  = 3;  ////// Hardwire LSQR
// LSproblem = 1;  ////// and LS problem defining "dy".
     /*
       if wait
         printf("\n\nReview parameters... then type "return"\n")
         keyboard
       end
     */
     if (eta < 0)
          printf("\n\nLinesearch disabled by eta < 0");

     //---------------------------------------------------------------------
     // All parameters have now been set.
     //---------------------------------------------------------------------
     double time    = CoinCpuTime();
     //bool useChol = (LSmethod == 1);
     //bool useQR   = (LSmethod == 2);
     bool direct  = (LSmethod <= 2 && ifexplicit);
     char solver[7];
     strncpy(solver, "  LSQR", 7);

     //---------------------------------------------------------------------
     // Categorize bounds and allow for fixed variables by modifying b.
     //---------------------------------------------------------------------

     int nlow, nupp, nfix;
     int *bptrs[3] = {0};
     getBoundTypes(&nlow, &nupp, &nfix, bptrs );
     int *low = bptrs[0];
     int *upp = bptrs[1];
     int *fix = bptrs[2];

     int nU = n;
     if (nupp == 0) nU = 1;  //Make dummy vectors if no Upper bounds

     //---------------------------------------------------------------------
     //  Get pointers to local copy of model bounds
     //---------------------------------------------------------------------

     CoinDenseVector<double> bl(n, columnLower_);
     double *bl_elts = bl.getElements();
     CoinDenseVector<double> bu(nU, columnUpper_);  // this is dummy if no UB
     double *bu_elts = bu.getElements();

     CoinDenseVector<double> r1(m, 0.0);
     double *r1_elts = r1.getElements();
     CoinDenseVector<double> x1(n, 0.0);
     double *x1_elts = x1.getElements();

     if (nfix > 0) {
          for (int k = 0; k < nfix; k++)
               x1_elts[fix[k]] = bl[fix[k]];
          matVecMult(1, r1, x1);
          b = b - r1;
          // At some stage, might want to look at normfix = norm(r1,inf);
     }

     //---------------------------------------------------------------------
     // Scale the input data.
     // The scaled variables are
     //    xbar     = x/beta,
     //    ybar     = y/zeta,
     //    zbar     = z/zeta.
     // Define
     //    theta    = beta*zeta;
     // The scaled function is
     //    phibar   = ( 1   /theta) fbar(beta*xbar),
     //    gradient = (beta /theta) grad,
     //    Hessian  = (beta2/theta) hess.
     //---------------------------------------------------------------------
     double beta = xsize_;
     if (beta == 0) beta = 1; // beta scales b, x.
     double zeta = zsize_;
     if (zeta == 0) zeta = 1; // zeta scales y, z.
     double theta  = beta * zeta;                          // theta scales obj.
     // (theta could be anything, but theta = beta*zeta makes
     // scaled grad = grad/zeta = 1 approximately if zeta is chosen right.)

     for (int k = 0; k < nlow; k++)
          bl_elts[low[k]] = bl_elts[low[k]] / beta;
     for (int k = 0; k < nupp; k++)
          bu_elts[upp[k]] = bu_elts[upp[k]] / beta;
     d1     = d1 * ( beta / sqrt(theta) );
     d2     = d2 * ( sqrt(theta) / beta );

     double beta2  = beta * beta;
     b.scale( (1.0 / beta) );
     y.scale( (1.0 / zeta) );
     x.scale( (1.0 / beta) );
     z.scale( (1.0 / zeta) );

     //---------------------------------------------------------------------
     // Initialize vectors that are not fully used if bounds are missing.
     //---------------------------------------------------------------------
     CoinDenseVector<double> rL(n, 0.0);
     CoinDenseVector<double> cL(n, 0.0);
     CoinDenseVector<double> z1(n, 0.0);
     CoinDenseVector<double> dx1(n, 0.0);
     CoinDenseVector<double> dz1(n, 0.0);
     CoinDenseVector<double> r2(n, 0.0);

     // Assign upper bd regions (dummy if no UBs)

     CoinDenseVector<double> rU(nU, 0.0);
     CoinDenseVector<double> cU(nU, 0.0);
     CoinDenseVector<double> x2(nU, 0.0);
     CoinDenseVector<double> z2(nU, 0.0);
     CoinDenseVector<double> dx2(nU, 0.0);
     CoinDenseVector<double> dz2(nU, 0.0);

     //---------------------------------------------------------------------
     // Initialize x, y, z, objective, etc.
     //---------------------------------------------------------------------
     CoinDenseVector<double> dx(n, 0.0);
     CoinDenseVector<double> dy(m, 0.0);
     CoinDenseVector<double> Pr(m);
     CoinDenseVector<double> D(n);
     double *D_elts = D.getElements();
     CoinDenseVector<double> w(n);
     double *w_elts = w.getElements();
     CoinDenseVector<double> rhs(m + n);


     //---------------------------------------------------------------------
     // Pull out the element array pointers for efficiency
     //---------------------------------------------------------------------
     double *x_elts  = x.getElements();
     double *x2_elts = x2.getElements();
     double *z_elts  = z.getElements();
     double *z1_elts = z1.getElements();
     double *z2_elts = z2.getElements();

     for (int k = 0; k < nlow; k++) {
          x_elts[low[k]]  = CoinMax( x_elts[low[k]], bl[low[k]]);
          x1_elts[low[k]] = CoinMax( x_elts[low[k]] - bl[low[k]], x0min  );
          z1_elts[low[k]] = CoinMax( z_elts[low[k]], z0min  );
     }
     for (int k = 0; k < nupp; k++) {
          x_elts[upp[k]]  = CoinMin( x_elts[upp[k]], bu[upp[k]]);
          x2_elts[upp[k]] = CoinMax(bu[upp[k]] -  x_elts[upp[k]], x0min  );
          z2_elts[upp[k]] = CoinMax(-z_elts[upp[k]], z0min  );
     }
     //////////////////// Assume hessian is diagonal. //////////////////////

//  [obj,grad,hess] = feval( Fname, (x*beta) );
     x.scale(beta);
     double obj = getObj(x);
     CoinDenseVector<double> grad(n);
     getGrad(x, grad);
     CoinDenseVector<double> H(n);
     getHessian(x , H);
     x.scale((1.0 / beta));

     //double * g_elts = grad.getElements();
     double * H_elts = H.getElements();

     obj /= theta;                       // Scaled obj.
     grad = grad * (beta / theta) + (d1 * d1) * x; // grad includes x regularization.
     H  = H * (beta2 / theta) + (d1 * d1)      ; // H    includes x regularization.


     /*---------------------------------------------------------------------
     // Compute primal and dual residuals:
     // r1 =  b - Aprod(x) - d2*d2*y;
     // r2 =  grad - Atprod(y) + z2 - z1;
     //  rL =  bl - x + x1;
     //  rU =  x + x2 - bu; */
     //---------------------------------------------------------------------
     //  [r1,r2,rL,rU,Pinf,Dinf] = ...
     //      pdxxxresid1( Aname,fix,low,upp, ...
     //                   b,bl,bu,d1,d2,grad,rL,rU,x,x1,x2,y,z1,z2 );
     pdxxxresid1( this, nlow, nupp, nfix, low, upp, fix,
                  b, bl_elts, bu_elts, d1, d2, grad, rL, rU, x, x1, x2, y, z1, z2,
                  r1, r2, &Pinf, &Dinf);
     //---------------------------------------------------------------------
     // Initialize mu and complementarity residuals:
     //    cL   = mu*e - X1*z1.
     //    cU   = mu*e - X2*z2.
     //
     // 25 Jan 2001: Now that b and obj are scaled (and hence x,y,z),
     //              we should be able to use mufirst = mu0 (absolute value).
     //              0.1 worked poorly on StarTest1 with x0min = z0min = 0.1.
     // 29 Jan 2001: We might as well use mu0 = x0min * z0min;
     //              so that most variables are centered after a warm start.
     // 29 Sep 2002: Use mufirst = mu0*(x0min * z0min),
     //              regarding mu0 as a scaling of the initial center.
     //---------------------------------------------------------------------
     //  double mufirst = mu0*(x0min * z0min);
     double mufirst = mu0;   // revert to absolute value
     double mulast  = 0.1 * opttol;
     mulast  = CoinMin( mulast, mufirst );
     double mu      = mufirst;
     double center,  fmerit;
     pdxxxresid2( mu, nlow, nupp, low, upp, cL, cU, x1, x2,
                  z1, z2, &center, &Cinf, &Cinf0 );
     fmerit = pdxxxmerit(nlow, nupp, low, upp, r1, r2, rL, rU, cL, cU );

     // Initialize other things.

     bool  precon   = true;
     double PDitns    = 0;
     //bool converged = false;
     double atol      = atol1;
     atol2     = CoinMax( atol2, atolmin );
     atolmin   = atol2;
     //  pdDDD2    = d2;    // Global vector for diagonal matrix D2

     //  Iteration log.

     int nf      = 0;
     int itncg   = 0;
     int nfail   = 0;

     printf("\n\nItn   mu   stepx   stepz  Pinf  Dinf");
     printf("  Cinf   Objective    nf  center");
     if (direct) {
          printf("\n");
     } else {
          printf("  atol   solver   Inexact\n");
     }

     double regx = (d1 * x).twoNorm();
     double regy = (d2 * y).twoNorm();
     //  regterm = twoNorm(d1.*x)^2  +  norm(d2.*y)^2;
     double regterm = regx * regx + regy * regy;
     double objreg  = obj  +  0.5 * regterm;
     double objtrue = objreg * theta;

     printf("\n%3g                     ", PDitns        );
     printf("%6.1f%6.1f" , log10(Pinf ), log10(Dinf));
     printf("%6.1f%15.7e", log10(Cinf0), objtrue    );
     printf("   %8.1f\n"   , center                   );
     /*
     if kminor
       printf("\n\nStart of first minor itn...\n");
       keyboard
     end
     */
     //---------------------------------------------------------------------
     // Main loop.
     //---------------------------------------------------------------------
     // Lsqr
     ClpLsqr  thisLsqr(this);
     //  while (converged) {
     while(PDitns < maxitn) {
          PDitns = PDitns + 1;

          // 31 Jan 2001: Set atol according to progress, a la Inexact Newton.
          // 07 Feb 2001: 0.1 not small enough for Satellite problem.  Try 0.01.
          // 25 Apr 2001: 0.01 seems wasteful for Star problem.
          //              Now that starting conditions are better, go back to 0.1.

          double r3norm = CoinMax(Pinf,   CoinMax(Dinf,  Cinf));
          atol   = CoinMin(atol,  r3norm * 0.1);
          atol   = CoinMax(atol,  atolmin   );
          info.r3norm = r3norm;

          //-------------------------------------------------------------------
          //  Define a damped Newton iteration for solving f = 0,
          //  keeping  x1, x2, z1, z2 > 0.  We eliminate dx1, dx2, dz1, dz2
          //  to obtain the system
          //
          //     [-H2  A"  ] [ dx ] = [ w ],   H2 = H + D1^2 + X1inv Z1 + X2inv Z2,
          //     [ A   D2^2] [ dy ] = [ r1]    w  = r2 - X1inv(cL + Z1 rL)
          //                                           + X2inv(cU + Z2 rU),
          //
          //  which is equivalent to the least-squares problem
          //
          //     min || [ D A"]dy  -  [  D w   ] ||,   D = H2^{-1/2}.         (*)
          //         || [  D2 ]       [D2inv r1] ||
          //-------------------------------------------------------------------
          for (int k = 0; k < nlow; k++)
               H_elts[low[k]]  = H_elts[low[k]] + z1[low[k]] / x1[low[k]];
          for (int k = 0; k < nupp; k++)
               H[upp[k]]  = H[upp[k]] + z2[upp[k]] / x2[upp[k]];
          w = r2;
          for (int k = 0; k < nlow; k++)
               w[low[k]]  = w[low[k]] - (cL[low[k]] + z1[low[k]] * rL[low[k]]) / x1[low[k]];
          for (int k = 0; k < nupp; k++)
               w[upp[k]]  = w[upp[k]] + (cU[upp[k]] + z2[upp[k]] * rU[upp[k]]) / x2[upp[k]];

          if (LSproblem == 1) {
               //-----------------------------------------------------------------
               //  Solve (*) for dy.
               //-----------------------------------------------------------------
               H      = 1.0 / H;  // H is now Hinv (NOTE!)
               for (int k = 0; k < nfix; k++)
                    H[fix[k]] = 0;
               for (int k = 0; k < n; k++)
                    D_elts[k] = sqrt(H_elts[k]);
               thisLsqr.borrowDiag1(D_elts);
               thisLsqr.diag2_ = d2;

               if (direct) {
                    // Omit direct option for now
               } else {// Iterative solve using LSQR.
                    //rhs     = [ D.*w; r1./d2 ];
                    for (int k = 0; k < n; k++)
                         rhs[k] = D_elts[k] * w_elts[k];
                    for (int k = 0; k < m; k++)
                         rhs[n+k] = r1_elts[k] * (1.0 / d2);
                    double damp    = 0;

                    if (precon) {   // Construct diagonal preconditioner for LSQR
                         matPrecon(d2, Pr, D);
                    }
                    /*
                    	rw(7)        = precon;
                            info.atolmin = atolmin;
                            info.r3norm  = fmerit;  // Must be the 2-norm here.

                            [ dy, istop, itncg, outfo ] = ...
                       pdxxxlsqr( nb,m,"pdxxxlsqrmat",Aname,rw,rhs,damp, ...
                                  atol,btol,conlim,itnlim,show,info );


                    	thisLsqr.input->rhs_vec = &rhs;
                    	thisLsqr.input->sol_vec = &dy;
                    	thisLsqr.input->rel_mat_err = atol;
                    	thisLsqr.do_lsqr(this);
                    	*/
                    //  New version of lsqr

                    int istop;
                    dy.clear();
                    show = false;
                    info.atolmin = atolmin;
                    info.r3norm  = fmerit;  // Must be the 2-norm here.

                    thisLsqr.do_lsqr( rhs, damp, atol, btol, conlim, itnlim,
                                      show, info, dy , &istop, &itncg, &outfo, precon, Pr);
                    if (precon)
                         dy = dy * Pr;

                    if (!precon && itncg > 999999)
                         precon = true;

                    if (istop == 3  ||  istop == 7 )  // conlim or itnlim
                         printf("\n    LSQR stopped early:  istop = //%d", istop);


                    atolold   = outfo.atolold;
                    atol      = outfo.atolnew;
                    r3ratio   = outfo.r3ratio;
               }// LSproblem 1

               //      grad      = pdxxxmat( Aname,2,m,n,dy );   // grad = A"dy
               grad.clear();
               matVecMult(2, grad, dy);
               for (int k = 0; k < nfix; k++)
                    grad[fix[k]] = 0;                            // grad is a work vector
               dx = H * (grad - w);

          } else {
               perror( "This LSproblem not yet implemented\n" );
          }
          //-------------------------------------------------------------------

          CGitns += itncg;

          //-------------------------------------------------------------------
          // dx and dy are now known.  Get dx1, dx2, dz1, dz2.
          //-------------------------------------------------------------------
          for (int k = 0; k < nlow; k++) {
               dx1[low[k]] = - rL[low[k]] + dx[low[k]];
               dz1[low[k]] =  (cL[low[k]] - z1[low[k]] * dx1[low[k]]) / x1[low[k]];
          }
          for (int k = 0; k < nupp; k++) {
               dx2[upp[k]] = - rU[upp[k]] - dx[upp[k]];
               dz2[upp[k]] =  (cU[upp[k]] - z2[upp[k]] * dx2[upp[k]]) / x2[upp[k]];
          }
          //-------------------------------------------------------------------
          // Find the maximum step.
          //--------------------------------------------------------------------
          double stepx1 = pdxxxstep(nlow, low, x1, dx1 );
          double stepx2 = inf;
          if (nupp > 0)
               stepx2 = pdxxxstep(nupp, upp, x2, dx2 );
          double stepz1 = pdxxxstep( z1     , dz1      );
          double stepz2 = inf;
          if (nupp > 0)
               stepz2 = pdxxxstep( z2     , dz2      );
          double stepx  = CoinMin( stepx1, stepx2 );
          double stepz  = CoinMin( stepz1, stepz2 );
          stepx  = CoinMin( steptol * stepx, 1.0 );
          stepz  = CoinMin( steptol * stepz, 1.0 );
          if (stepSame) {                  // For NLPs, force same step
               stepx = CoinMin( stepx, stepz );   // (true Newton method)
               stepz = stepx;
          }

          //-------------------------------------------------------------------
          // Backtracking linesearch.
          //-------------------------------------------------------------------
          bool fail     =  true;
          nf       =  0;

          while (nf < maxf) {
               nf      = nf + 1;
               x       = x        +  stepx * dx;
               y       = y        +  stepz * dy;
               for (int k = 0; k < nlow; k++) {
                    x1[low[k]] = x1[low[k]]  +  stepx * dx1[low[k]];
                    z1[low[k]] = z1[low[k]]  +  stepz * dz1[low[k]];
               }
               for (int k = 0; k < nupp; k++) {
                    x2[upp[k]] = x2[upp[k]]  +  stepx * dx2[upp[k]];
                    z2[upp[k]] = z2[upp[k]]  +  stepz * dz2[upp[k]];
               }
               //      [obj,grad,hess] = feval( Fname, (x*beta) );
               x.scale(beta);
               obj = getObj(x);
               getGrad(x, grad);
               getHessian(x, H);
               x.scale((1.0 / beta));

               obj        /= theta;
               grad       = grad * (beta / theta)  +  d1 * d1 * x;
               H          = H * (beta2 / theta)  +  d1 * d1;

               //      [r1,r2,rL,rU,Pinf,Dinf] = ...
               pdxxxresid1( this, nlow, nupp, nfix, low, upp, fix,
                            b, bl_elts, bu_elts, d1, d2, grad, rL, rU, x, x1, x2,
                            y, z1, z2, r1, r2, &Pinf, &Dinf );
               //double center, Cinf, Cinf0;
               //      [cL,cU,center,Cinf,Cinf0] = ...
               pdxxxresid2( mu, nlow, nupp, low, upp, cL, cU, x1, x2, z1, z2,
                            &center, &Cinf, &Cinf0);
               double fmeritnew = pdxxxmerit(nlow, nupp, low, upp, r1, r2, rL, rU, cL, cU );
               double step      = CoinMin( stepx, stepz );

               if (fmeritnew <= (1 - eta * step)*fmerit) {
                    fail = false;
                    break;
               }

               // Merit function didn"t decrease.
               // Restore variables to previous values.
               // (This introduces a little error, but save lots of space.)

               x       = x        -  stepx * dx;
               y       = y        -  stepz * dy;
               for (int k = 0; k < nlow; k++) {
                    x1[low[k]] = x1[low[k]]  -  stepx * dx1[low[k]];
                    z1[low[k]] = z1[low[k]]  -  stepz * dz1[low[k]];
               }
               for (int k = 0; k < nupp; k++) {
                    x2[upp[k]] = x2[upp[k]]  -  stepx * dx2[upp[k]];
                    z2[upp[k]] = z2[upp[k]]  -  stepz * dz2[upp[k]];
               }
               // Back-track.
               // If it"s the first time,
               // make stepx and stepz the same.

               if (nf == 1 && stepx != stepz) {
                    stepx = step;
               } else if (nf < maxf) {
                    stepx = stepx / 2;
               }
               stepz = stepx;
          }

          if (fail) {
               printf("\n     Linesearch failed (nf too big)");
               nfail += 1;
          } else {
               nfail = 0;
          }

          //-------------------------------------------------------------------
          // Set convergence measures.
          //--------------------------------------------------------------------
          regx = (d1 * x).twoNorm();
          regy = (d2 * y).twoNorm();
          regterm = regx * regx + regy * regy;
          objreg  = obj  +  0.5 * regterm;
          objtrue = objreg * theta;

          bool primalfeas    = Pinf  <=  featol;
          bool dualfeas      = Dinf  <=  featol;
          bool complementary = Cinf0 <=  opttol;
          bool enough        = PDitns >=       4; // Prevent premature termination.
          bool converged     = primalfeas  &  dualfeas  &  complementary  &  enough;

          //-------------------------------------------------------------------
          // Iteration log.
          //-------------------------------------------------------------------
          char str1[100], str2[100], str3[100], str4[100], str5[100];
          sprintf(str1, "\n%3g%5.1f" , PDitns      , log10(mu)   );
          sprintf(str2, "%8.5f%8.5f" , stepx       , stepz       );
          if (stepx < 0.0001 || stepz < 0.0001) {
               sprintf(str2, " %6.1e %6.1e" , stepx       , stepz       );
          }

          sprintf(str3, "%6.1f%6.1f" , log10(Pinf) , log10(Dinf));
          sprintf(str4, "%6.1f%15.7e", log10(Cinf0), objtrue     );
          sprintf(str5, "%3d%8.1f"   , nf          , center      );
          if (center > 99999) {
               sprintf(str5, "%3d%8.1e"   , nf          , center      );
          }
          printf("%s%s%s%s%s", str1, str2, str3, str4, str5);
          if (direct) {
               // relax
          } else {
               printf(" %5.1f%7d%7.3f", log10(atolold), itncg, r3ratio);
          }
          //-------------------------------------------------------------------
          // Test for termination.
          //-------------------------------------------------------------------
          if (kminor) {
               printf( "\nStart of next minor itn...\n");
               //      keyboard;
          }

          if (converged) {
               printf("\n   Converged");
               break;
          } else if (PDitns >= maxitn) {
               printf("\n   Too many iterations");
               inform = 1;
               break;
          } else if (nfail  >= maxfail) {
               printf("\n   Too many linesearch failures");
               inform = 2;
               break;
          } else {

               // Reduce mu, and reset certain residuals.

               double stepmu  = CoinMin( stepx , stepz   );
               stepmu  = CoinMin( stepmu, steptol );
               double muold   = mu;
               mu      = mu   -  stepmu * mu;
               if (center >= bigcenter)
                    mu = muold;

               // mutrad = mu0*(sum(Xz)/n); // 24 May 1998: Traditional value, but
               // mu     = CoinMin(mu,mutrad ); // it seemed to decrease mu too much.

               mu      = CoinMax(mu, mulast); // 13 Jun 1998: No need for smaller mu.
               //      [cL,cU,center,Cinf,Cinf0] = ...
               pdxxxresid2( mu, nlow, nupp, low, upp, cL, cU, x1, x2, z1, z2,
                            &center, &Cinf, &Cinf0 );
               fmerit = pdxxxmerit( nlow, nupp, low, upp, r1, r2, rL, rU, cL, cU );

               // Reduce atol for LSQR (and SYMMLQ).
               // NOW DONE AT TOP OF LOOP.

               atolold = atol;
               // if atol > atol2
               //   atolfac = (mu/mufirst)^0.25;
               //   atol    = CoinMax( atol*atolfac, atol2 );
               // end

               // atol = CoinMin( atol, mu );     // 22 Jan 2001: a la Inexact Newton.
               // atol = CoinMin( atol, 0.5*mu ); // 30 Jan 2001: A bit tighter

               // If the linesearch took more than one function (nf > 1),
               // we assume the search direction needed more accuracy
               // (though this may be true only for LPs).
               // 12 Jun 1998: Ask for more accuracy if nf > 2.
               // 24 Nov 2000: Also if the steps are small.
               // 30 Jan 2001: Small steps might be ok with warm start.
               // 06 Feb 2001: Not necessarily.  Reinstated tests in next line.

               if (nf > 2  ||  CoinMin( stepx, stepz ) <= 0.01)
                    atol = atolold * 0.1;
          }
          //---------------------------------------------------------------------
          // End of main loop.
          //---------------------------------------------------------------------
     }


     for (int k = 0; k < nfix; k++)
          x[fix[k]] = bl[fix[k]];
     z      = z1;
     if (nupp > 0)
          z = z - z2;
     printf("\n\nmax |x| =%10.3f", x.infNorm() );
     printf("    max |y| =%10.3f", y.infNorm() );
     printf("    max |z| =%10.3f", z.infNorm() );
     printf("   scaled");

     x.scale(beta);
     y.scale(zeta);
     z.scale(zeta);   // Unscale x, y, z.

     printf(  "\nmax |x| =%10.3f", x.infNorm() );
     printf("    max |y| =%10.3f", y.infNorm() );
     printf("    max |z| =%10.3f", z.infNorm() );
     printf(" unscaled\n");

     time   = CoinCpuTime() - time;
     char str1[100], str2[100];
     sprintf(str1, "\nPDitns  =%10g", PDitns );
     sprintf(str2, "itns =%10d", CGitns );
     //  printf( [str1 " " solver str2] );
     printf("    time    =%10.1f\n", time);
     /*
     pdxxxdistrib( abs(x),abs(z) );   // Private function

     if (wait)
       keyboard;
     */
//-----------------------------------------------------------------------
// End function pdco.m
//-----------------------------------------------------------------------
     /*  printf("Solution x values:\n\n");
       for (int k=0; k<n; k++)
         printf(" %d   %e\n", k, x[k]);
     */
// Print distribution
     double thresh[9] = { 0.00000001, 0.0000001, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1.00001};
     int counts[9] = {0};
     for (int ij = 0; ij < n; ij++) {
          for (int j = 0; j < 9; j++) {
               if(x[ij] < thresh[j]) {
                    counts[j] += 1;
                    break;
               }
          }
     }
     printf ("Distribution of Solution Values\n");
     for (int j = 8; j > 1; j--)
          printf(" %g  to  %g %d\n", thresh[j-1], thresh[j], counts[j]);
     printf("   Less than   %g %d\n", thresh[2], counts[0]);

     return inform;
}
Esempio n. 20
0
void StrandBlockSolver::lhsDissipation()
{
  int jj=nPstr+2,nn=nFaces+nBedges;
  Array4D<double> aa(nq,nq,jj,nn);
  for (int n=0; n<nFaces+nBedges; n++)
    for (int j=0; j<nPstr+2; j++)
      for (int k=0; k<nq; k++)
	for (int l=0; l<nq; l++) aa(l,k,j,n) = 0.;


  // unstructured faces
  int c1,c2,n1,n2,jp,fc,m,mm,m1l,m1u,m2l,m2u,npts=1;
  double a[nq*nq];
  for (int n=0; n<nEdges; n++){
    c1            = edge(0,n);
    c2            = edge(1,n);
    m1l           = ncsc(c1  );
    m1u           = ncsc(c1+1);
    m2l           = ncsc(c2  );
    m2u           = ncsc(c2+1);
    fc            = fClip(c1);
    if (fClip(c2) > fc) fc = fClip(c2);
  for (int j=1; j<fc+1; j++){
    sys->lhsDisFluxJacobian(npts,&facs(0,j,n),&xvs(j,n),
			    &q(0,j,c1),&q(0,j,c2),&a[0]);

    for (int k=0; k<nq*nq; k++) a[k] *= .5;

    // diagonal contributions
    for (int k=0; k<nq; k++){
      m             = k*nq;
    for (int l=0; l<nq; l++){
      dd(l,k,j,c1) += a[m+l];
      dd(l,k,j,c2) += a[m+l];
    }}

    // off-diagonal contributions
    for (int k=0; k<nq; k++){
      m             = k*nq;
    for (int l=0; l<nq; l++){
      aa(l,k,j,c1)  = a[m+l];
      aa(l,k,j,c2)  = a[m+l];
    }}

    for (int mm=m1l; mm<m1u; mm++){
      nn            = csc(mm);
      for (int k=0; k<nq; k++){
	m           = k*nq;
      for (int l=0; l<nq; l++)
	bu(l,k,j,mm) -= aa(l,k,j,nn);
      }}

    for (int mm=m2l; mm<m2u; mm++){
      nn            = csc(mm);
      for (int k=0; k<nq; k++){
	m           = k*nq;
      for (int l=0; l<nq; l++)
	bu(l,k,j,mm) -= aa(l,k,j,nn);
      }}

    // reset working array to zero
    for (int k=0; k<nq; k++){
      m             = k*nq;
    for (int l=0; l<nq; l++){
      aa(l,k,j,c1)  = 0.;
      aa(l,k,j,c2)  = 0.;
    }}
  }}

  aa.deallocate();


  // structured faces
  double Ax,Ay,ds,eps=1.e-14;
  for (int n=0; n<nFaces-nGfaces; n++){
    n1            = face(0,n);
    n2            = face(1,n);
  for (int j=0; j<fClip(n)+1; j++){
    jp            = j+1;
    Ax            = facu(0,j,n);
    Ay            = facu(1,j,n);
    ds            = Ax*Ax+Ay*Ay;
    if (fabs(ds) < eps){
      for (int k=0; k<nq; k++){
	m         = k*nq;
      for (int l=0; l<nq; l++)
	a[m+l]    = 0.;
      }}
    else
      sys->lhsDisFluxJacobian(npts,&facu(0,j,n),&xvu(j,n),
			      &q(0,j,n),&q(0,jp,n),&a[0]);
    for (int k=0; k<nq; k++){
      m             = k*nq;
    for (int l=0; l<nq; l++){
      dd(l,k,j ,n) += (a[m+l]*.5);
      dp(l,k,j ,n) -= (a[m+l]*.5);
      dd(l,k,jp,n) += (a[m+l]*.5);
      dm(l,k,jp,n) -= (a[m+l]*.5);
    }}}}
}
Esempio n. 21
0
void RestorationProblem::printVariables( const Matrix &xi, const Matrix &lambda, int verbose )
{
    int k;

    printf("\n<|----- Original Variables -----|>\n");
    for( k=0; k<parent->nVar; k++ )
        //printf("%7i: %-30s   %7g <= %10.3g <= %7g   |   mul=%10.3g\n", k+1, parent->varNames[k], bl(k), xi(k), bu(k), lambda(k));
        printf("%7i: x%-5i   %7g <= %10.3g <= %7g   |   mul=%10.3g\n", k+1, k, bl(k), xi(k), bu(k), lambda(k));
    printf("\n<|----- Slack Variables -----|>\n");
    for( k=parent->nVar; k<nVar; k++ )
        printf("%7i: slack   %7g <= %10.3g <= %7g   |   mul=%10.3g\n", k+1, bl(k), xi(k), bu(k), lambda(k));
}
Esempio n. 22
0
void RestorationProblem::printConstraints( const Matrix &constr, const Matrix &lambda )
{
    printf("\n<|----- Constraints -----|>\n");
    for( int k=0; k<nCon; k++ )
        //printf("%5i: %-30s   %7g <= %10.4g <= %7g   |   mul=%10.3g\n", k+1, parent->conNames[parent->nVar+k], bl(nVar+k), constr(k), bu(nVar+k), lambda(nVar+k));
        printf("%5i: c%-5i   %7g <= %10.4g <= %7g   |   mul=%10.3g\n", k+1, k, bl(nVar+k), constr(k), bu(nVar+k), lambda(nVar+k));
}
Esempio n. 23
0
int main(int argc, char *argv[])
{
  int n = 15;
  int m = 18;
  std::vector<double> bl(n+m);
  std::vector<double> bu(n+m);

  double inf = 1.0e+20;

  std::fill(bl.begin(), bl.begin()+n, 0);

  std::fill(bu.begin(), bu.begin()+n, inf);


  // http://apmonitor.com/wiki/uploads/Apps/hs118.apm

  // LBX
  bl[0] =   8.0;
  bu[0] =  21.0;

  bl[1] =  43.0;
  bu[1] =  57.0;

  bl[2] =   3.0;
  bu[2] =  16.0;

  bu[3] =  90.0;
  bu[4] = 120.0;
  bu[5] =  60.0;
  bu[6] =  90.0;
  bu[7] = 120.0;
  bu[8] =  60.0;
  bu[9] =  90.0;
  bu[10] = 120.0;
  bu[11] =  60.0;
  bu[12] =  90.0;
  bu[13] = 120.0;
  bu[14] =  60.0;

  //
  std::fill(bl.begin()+n, bl.begin()+n+m, 0);
  std::fill(bu.begin()+n, bu.begin()+n+m, inf);

  //iObj   = 18  means the linear objective is row 18 in valA(*).
  //The objective row is free.

  int iObj   = 17;
  bl[n+iObj] = -inf;

  // LBG
  bl[n+0]  =  -7.0;
  bu[n+0]  =   6.0;

  bl[n+1]  =  -7.0;
  bu[n+1]  =   6.0;

  bl[n+2]  =  -7.0;
  bu[n+2]  =   6.0;

  bl[n+3]  =  -7.0;
  bu[n+3]  =   6.0;

  bl[n+4]  =  -7.0;
  bu[n+4]  =   7.0;

  bl[n+5]  =  -7.0;
  bu[n+5]  =   7.0;

  bl[n+6]  =  -7.0;
  bu[n+6]  =   7.0;

  bl[n+7]  =  -7.0;
  bu[n+7]  =   7.0;

  bl[n+8]  =  -7.0;
  bu[n+8]  =   6.0;

  bl[n+9] =  -7.0;
  bu[n+9] =   6.0;

  bl[n+10] =  -7.0;
  bu[n+10] =   6.0;

  bl[n+11] =  -7.0;
  bu[n+11] =   6.0;

  bl[n+12] =  60.0;
  bl[n+13] =  50.0;
  bl[n+14] =  70.0;
  bl[n+15] =  85.0;
  bl[n+16] = 100.0;


  std::vector<double> x0(n);

  x0[ 0]  =  20.0;
  x0[ 1]  =  55.0;
  x0[ 2]  =  15.0;
  x0[ 3]  =  20.0;
  x0[ 4]  =  60.0;
  x0[ 5]  =  20.0;
  x0[ 6]  =  20.0;
  x0[ 7]  =  60.0;
  x0[ 8]  =  20.0;
  x0[9]  =  20.0;
  x0[10]  =  60.0;
  x0[11]  =  20.0;
  x0[12]  =  20.0;
  x0[13]  =  60.0;
  x0[14]  =  20.0;

  sqic(&m , &n, &bl[0], &bu[0]);

  return 0;
}
  /**
   * \fn void point( double u , double v , double& x , double& y , double& z ) const
   *
   * \brief Compute a point on this rectangular Bezier patch.
   *
   * \param u First Cartesian coordinate of the parameter point.
   * \param v Second Cartesian coordinate of the parameter point.
   * \param x First Cartesian coordinate of the point on the patch.
   * \param y Second Caresian coordinate of the point on the patch.
   * \param z Third Cartesian coordinate of the point on the patch.
   */
  void
  tBezier::point(
		 double u , 
		 double v ,
		 double& x ,
		 double& y ,
		 double& z 
		) 
    const
  {
    /**
     * Map the point to the affine frame [0,1].
     */
    double rx = get_aff_1st_point_x_coord() ;
    double ry = get_aff_1st_point_y_coord() ;
    double sx = get_aff_2nd_point_x_coord() ;
    double sy = get_aff_2nd_point_y_coord() ;
    
    /**
     * Compute all Bernstein polynomials of degree \var{_m}.
     */

    double uu = ( u - rx ) / ( sx - rx ) ;
    double vv = ( v - ry ) / ( sy - ry ) ;

    if ( fabs( uu ) <= 1e-15 ) {
      uu = 0 ;
    }
    else if ( fabs( 1 - uu ) <= 1e-15 ) {
      uu = 1 ;
    }

    if ( fabs( vv ) <= 1e-15 ) {
      vv = 0 ;
    }
    else if ( fabs( 1 - vv ) <= 1e-15 ) {
      vv = 1 ;
    }

    unsigned m = get_bidegree_1st_index() ;
    unsigned n = get_bidegree_2nd_index() ;

    std::vector< double > bu( m + 1 ) ;
    all_bernstein( m , uu , bu );

    /**
     * Compute all Bernstein polynomials of degree \var{_n}.
     */
    std::vector< double > bv( n + 1 ) ;
    all_bernstein( n , vv , bv );

    /**
     * Compute the image point, (x,y,z), of (u,v) on this patch.
     */
    x = 0 ;
    y = 0 ;
    z = 0 ;
    for ( unsigned j = 0 ; j <= n ; j++ ) {
      for ( unsigned i = 0 ; i <= m ; i++ ) {
        double buv = bu[ i ] * bv[ j ] ;
	double xaux ;
	double yaux ;
	double zaux ;
	b( i , j , xaux , yaux , zaux ) ;

	x += buv * xaux ;
	y += buv * yaux ;
	z += buv * zaux ;
      }
    }

    return ;
  }
Esempio n. 25
0
void CFiletransferContainer::UpdateDimensions(const TRect& aRect)
{
	iRect=aRect;
	iWidth=aRect.Width();
	iMargin=iWidth/KMarginRatio;
	iVerticalDistance=aRect.Height()/7;
	if(iVerticalDistance>50)
		iVerticalDistance=50;
	iRound=iVerticalDistance/5; //was 3 before
	iHorizontalDistance=iWidth-iMargin-iMargin;
	iVD4=iVerticalDistance*4;
	iVD5=iVerticalDistance*5;

    iFill.Create(TSize(1,iVerticalDistance),EColor16MU);
    TBitmapUtil bu(&iFill);
    bu.Begin(TPoint(0,0));
    TInt i;
    for(i=0;i<iVerticalDistance;i++)
    {
    	bu.SetPixel(0x00FF00-0x500*i);
    	bu.IncYPos();
    };

    //rectangles
    TInt totHrz=iMargin;
    TReal hrz=0;
    TRect *rect;
    iRects.ResetAndDestroy(); //we will append new elements

    //compute the distance between rectangles
    TInt averageRectangleLength=iHorizontalDistance/iFiletransferView->iTotalFilesNo;

    if(averageRectangleLength<5)
    {
    	//we should have a single rectangle, and not one rectangle for each file
    	//iRects.Count() will be one, but iDistanceBetweenRectangles is negative
    	iDistanceBetweenRectangles=-1;
    	//
    	rect=new(ELeave) TRect(totHrz,iVD4,totHrz+iHorizontalDistance,iVD5);
    	iRects.Append(rect);
    }
    else
    {
    	iDistanceBetweenRectangles=2; //this is what we will use if there are not so many rectangles
    	if(averageRectangleLength<20)iDistanceBetweenRectangles=1;
        if(averageRectangleLength<10)iDistanceBetweenRectangles=0;
        TInt remainingBytes=iFiletransferView->iTotalBytes2Transfer;
        TInt remainingHorizontalDistance=iHorizontalDistance;

        for(i=0;i<iFiletransferView->iTotalFilesNo;i++)
        {
        	totHrz+=(TInt)hrz;//hrz was computed in the last cycle

        	//hrz=iFiletransferView->iFileSizes[i]/(TReal)iFiletransferView->iTotalBytes2Transfer*iHorizontalDistance;
        	hrz=iFiletransferView->iFileSizes[i]/(TReal)remainingBytes*remainingHorizontalDistance;
        	remainingBytes-=iFiletransferView->iFileSizes[i];
        	remainingHorizontalDistance-=(TInt)hrz;

        	rect=new(ELeave) TRect(totHrz,iVD4,totHrz+(TInt)hrz-iDistanceBetweenRectangles,iVD5);
        	iRects.Append(rect);
        };
    }





}
Esempio n. 26
0
int main(void){

	//PRUEBA CLASE EDGE:
	/*
	Edge arista(1,2,0.65);
	cout << arista.Weight() <<" "<< arista.either() <<" "<< arista.other(1) << endl; 
	arista.toString();
	MiBag bolsa;
	bolsa.add(arista);
	bolsa.iterate_Edge();*/
	
	/*
	//PRUEBA CLASE WEIGHTEDGRAPH
	EdgeWeightedGraph mi_grafo(5);
	Edge arista(1,2,0.65);
	Edge arista1(1,3,0.64);
	Edge arista2(4,2,0.70);
	Edge arista3(1,4,0.32);
	Edge arista4(3,0,0.3);
	Edge arista5(0,2,0.1);
	mi_grafo.addEdge(arista);
	mi_grafo.addEdge(arista1);
	mi_grafo.addEdge(arista2);
	mi_grafo.addEdge(arista3);
	mi_grafo.addEdge(arista4);
	mi_grafo.addEdge(arista5);

*/

// INICIO DE PRUEBA LAZY PRIM MST	
	int V,E;
	ifstream fin("tinyEW.txt");
	//ifstream fin("1000EWG.txt");
	//ifstream fin("mediumEWG.txt");	
	char vertex[255]; 
	char edges[255];
	fin.getline(vertex,255);//lee la primera linea
	fin.getline(edges,255);//lee la segunda linea
	string Ve(vertex); 
	string Ed(edges);
	istringstream bv(Ve);
	istringstream be(Ed);
	bv>>V;
	be>>E;
	char line[255];
	//MinIndexedPQ pq(E);//prueba
	EdgeWeightedGraph mi_grafo(V);
	
	int u,v;
	float w;
	for (int i = 0; i<E; i++){
		fin.getline(line,255);
		string s(line);
		vector <string> fields;
		boost::algorithm::split(fields, s, boost::algorithm::is_any_of(" "));
		istringstream bu(fields[0]);
		bu>>u;
		//cout<<u<<" ";
		istringstream bv(fields[1]);
		bv>>v;
		//cout<<v<<" ";
		istringstream bw(fields[2]);
		bw>>w;
		//cout<<w<<" ";
		//cout<<" "<<endl;
		Edge e(u,v,w);
		/*float u = e.weight();
		*/mi_grafo.addEdge(e);
	}
	
	
	//PRUEBA DE PRIORITY QUEUE QUE ALMACENA TODOS LOS EDGES:
	
	/*priority_queue<Edge, vector<Edge>, CompareEdges> pq = mi_grafo.priorityQueue(); 
	while(!pq.empty()){
		Edge e = pq.top();
		e.toString();
		pq.pop();
	}*/
	
	//PRUEBAS DE ALGORITMOS: 
	
	cout<<"El MST Eager Prim:"<<endl;
	EagerPrimMST mst1(mi_grafo);
	mst1.printMST();
	
	cout<<"El MST Lazy Prim:"<<endl;
	LazyPrimMST mst2(mi_grafo);
	mst2.printMST();
	
	cout<<"El MST Kruskal:"<<endl;
	kruskalMST mst3(mi_grafo);
	mst3.printMST();	

	//cout<<"El peso total del MST es: "<<mst.weight()<<endl;
	
	return 0;
}