void SimpleVector::axdzpy( double alpha, OoqpVector& xvec, OoqpVector& zvec, OoqpVector& select ) { assert( n == xvec.length() && n == zvec.length() ); SimpleVector & sxvec = dynamic_cast<SimpleVector &>(xvec); double * x = sxvec.v; SimpleVector & szvec = dynamic_cast<SimpleVector &>(zvec); double * z = szvec.v; SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * s = sselect.v; int i; if( alpha == 1.0 ) { for( i = 0; i < n; i++ ) { if( 0.0 != s[i] ) v[i] += x[i] / z[i]; } } else if ( alpha == -1.0 ) { for( i = 0; i < n; i++ ) { if( 0.0 != s[i] ) v[i] -= x[i] / z[i]; } } else { for( i = 0; i < n; i++ ) { if( 0.0 != s[i] ) v[i] += alpha * x[i] / z[i]; } } }
void SimpleVector::axzpy( double alpha, OoqpVector& xvec, OoqpVector& zvec ) { assert( n == xvec.length() && n == zvec.length() ); SimpleVector & sxvec = dynamic_cast<SimpleVector &>(xvec); SimpleVector & szvec = dynamic_cast<SimpleVector &>(zvec); double * x = sxvec.v; double * z = szvec.v; double * lx = x + n; double * w = v; if( alpha == 1.0 ) { while( x < lx ) { *w += *x * *z; w++; x++; z++; } } else if ( alpha == -1 ) { while( x < lx ) { *w -= *x * *z; w++; x++; z++; } } else { while( x < lx ) { *w += alpha * *x * *z; w++; x++; z++; } } }
void PetscVector::copyFrom( OoqpVector& v_in ) { assert( n == v_in.length() ); if( v_in.isKindOf( kPetscVector ) ) { PetscVector & pet = (PetscVector &) v_in; int ierr = VecCopy( pet.pv, pv ); assert( ierr == 0); } else if ( v_in.isKindOf( kSimpleVector ) ) { SimpleVector & sv = (SimpleVector &) v_in; this->copyFromArray( sv.elements() ); } else { assert( 0 && "Can't copy from unknown OoqpVector type" ); } }
double SimpleVector::dotProductWith( OoqpVector& vec ) { assert( n == vec.length() ); SimpleVector & svec = dynamic_cast<SimpleVector &>(vec); double * vvec = svec.v; double dot1 = 0.0; double dot2 = 0.0; const int size = 8196; int kmax = n / size; int i = 0; int k, imax; for( k = 0; k < kmax; k++ ) { imax = (k + 1) * 8196; for( ; i < imax; i++ ) { dot1 += v[i] * vvec[i]; } dot2 += dot1; dot1 = 0; } for( ; i < n; i++ ) { dot1 += v[i] * vvec[i]; } return dot2 + dot1; }
void SimpleVector::axpy( double alpha, OoqpVector& vec ) { assert( n == vec.length() ); SimpleVector & sv = dynamic_cast<SimpleVector &>(vec); int one = 1; daxpy_( &n, &alpha, sv.v, &one, v, &one ); }
void SimpleVector::componentMult( OoqpVector& vec ) { assert( n == vec.length() ); SimpleVector & sv = dynamic_cast<SimpleVector &>(vec); double * y = sv.v; int i; for( i = 0; i < n; i++ ) v[i] *= y[i]; }
void SimpleVector::axdzpy( double alpha, OoqpVector& xvec, OoqpVector& zvec ) { SimpleVector & sxvec = dynamic_cast<SimpleVector &>(xvec); double * x = sxvec.v; SimpleVector & szvec = dynamic_cast<SimpleVector &>(zvec); double * z = szvec.v; assert( n == xvec.length() && n == zvec.length() ); int i; for( i = 0; i < n; i++ ) { //if(x[i] > 0 && z[i] > 0) v[i] += alpha * x[i] / z[i]; } }
void SimpleVector::componentDiv ( OoqpVector& vec ) { assert( n == vec.length() ); double * pv = v, *lv = v + n; SimpleVector & sv = dynamic_cast<SimpleVector &>(vec); double * y = sv.v; for( ; pv < lv; pv++, y++ ) *pv /= *y; }
void SimpleVector::addSomeConstants( double c, OoqpVector& select ) { SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * map = sselect.v; int i; assert( n == select.length() ); for( i = 0; i < n; i++ ) { if( map[i] ) v[i] += c; } }
void SimpleVector::selectNonZeros( OoqpVector& select ) { SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * map = sselect.v; assert( n == select.length() ); int i; for( i = 0; i < n; i++ ) { if( 0.0 == map[i] ) v[i] = 0.0; } }
void SimpleVector::divideSome( OoqpVector& div, OoqpVector& select ) { if( n == 0 ) return; SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * map = sselect.v; SimpleVector & sdiv = dynamic_cast<SimpleVector &>(div); double * q = sdiv.v; assert( n == div.length() && n == select.length() ); double * lmap = map + n; double * w = v; while( map < lmap ) { if( 0 != *map ) { *w /= *q; } map++; w++; q++; } }
void DenseStorage::fromGetDiagonal( int idiag, OoqpVector& vec ) { int k; int extent = vec.length(); assert( idiag + extent <= n ); assert( idiag + extent <= m ); SimpleVector & sv = (SimpleVector &) vec; for ( k = idiag; k < idiag + extent; k++ ) { sv[k] = M[k][k]; } }
int SimpleVector::somePositive( OoqpVector& select ) { SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * map = sselect.v; assert( n == select.length() ); int i; for( i = 0; i < n; i++ ) { if( 0.0 != map[i] && v[i] <= 0 ) { cout << "Element " << i << " is nonpositive: " << v[i] << endl; return 0; } } return 1; }
int SimpleVector::matchesNonZeroPattern( OoqpVector& select ) { SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * map = sselect.v; double * lmap = map + n; assert( n == select.length() ); double *w = v; while( map < lmap ) { if( *map == 0.0 && *w != 0.0 ) return 0; map++; w++; } return 1; }
void DenseStorage::setToDiagonal( OoqpVector& vec ) { int i,k; int extent = vec.length(); assert( extent <= n ); assert( extent <= m ); SimpleVector & sv = (SimpleVector &) vec; for( i = 0; i < m; i++ ) { for( k = 0; k < n; k++ ) { M[i][k] = 0.0; } } for ( k = 0; k < extent; k++ ) { M[k][k] = sv[k]; } }
double SimpleVector::stepbound(OoqpVector & pvec, double maxStep ) { assert( n == pvec.length() ); SimpleVector & spvec = dynamic_cast<SimpleVector &>(pvec); double * p = spvec.v; double * w = v; double bound = maxStep; int i; for( i = 0; i < n; i++ ) { double temp = p[i]; if( w[i] >= 0 && temp < 0 ) { temp = -w[i]/temp; if( temp < bound ) { bound = temp; } } } return bound; }
double SimpleVector::shiftedDotProductWith( double alpha, OoqpVector& mystep, OoqpVector& yvec, double beta, OoqpVector& ystep ) { assert( n == mystep.length() && n == yvec .length() && n == ystep .length() ); SimpleVector & syvec = dynamic_cast<SimpleVector &>(yvec); double * y = syvec.v; SimpleVector & smystep = dynamic_cast<SimpleVector &>(mystep); double * p = smystep.v; SimpleVector & systep = dynamic_cast<SimpleVector &>(ystep); double * q = systep.v; double dot1 = 0.0; double dot2 = 0.0; const int size = 8196; int kmax = n / size; int i = 0; int k, imax; for( k = 0; k < kmax; k++ ) { imax = (k + 1) * 8196; for( ; i < imax; i++ ) { dot1 += (v[i] + alpha * p[i]) * (y[i] + beta * q[i] ); } dot2 += dot1; dot1 = 0; } for( ; i < n; i++ ) { dot1 += (v[i] + alpha * p[i]) * (y[i] + beta * q[i] ); } return dot2 + dot1; }
void SimpleVector::writefSomeToStream( ostream& out, const char format[], OoqpVector& select ) const { SimpleVector & sselect = dynamic_cast<SimpleVector &>(select); double * s = 0; if( select.length() > 0 ) { s = sselect.v; } int i; for( i = 0; i < n; i++ ) { if( !s || s[i] != 0.0 ) { int j = 0; char c; while( (c = format[j]) != 0 ) { if( c != '%' ) { out << c; } else { // Brain-dead variable substitution, but good enough for this // simple case if( 0 == strncmp( "{value}", &format[j + 1], 7 ) ) { out << v[i]; j += 7; } else if ( 0 == strncmp( "{index}", &format[j + 1], 7 ) ) { out << i; j += 7; } else { out << c; } } j++; } out << endl; } } }
void QpGenData::datarandom( OoqpVector & x, OoqpVector & y, OoqpVector & z, OoqpVector & s ) { double drand( double * ); double ix = 3074.20374; OoqpVectorHandle xdual(la->newVector( nx )); this->randomlyChooseBoundedVariables( x, *xdual, *blx, *ixlow, *bux, *ixupp, &ix, .25, .25, .25 ); { // ofstream x_vec( "x" ); // x->writeToStream( x_vec ); // ofstream eta_vec( "xdual" ); // eta_vec.precision(16); // xdual->writeToStream( eta_vec ); // ofstream blx_vec( "blx" ); // blx->writeToStream( blx_vec ); // ofstream bux_vec( "bux" ); // bux->writeToStream( bux_vec ); } OoqpVectorHandle sprime(la->newVector( mz )); this->randomlyChooseBoundedVariables( *sprime, z, *bl, *iclow, *bu, *icupp, &ix, .25, .25, .5 ); { // ofstream z_vec( "z" ); // z_vec << z << endl; } { Q->randomizePSD( &ix ); // ofstream Q_mat( "Q" ); // Q_mat << Q << endl; } { A->randomize( -10.0, 10.0, &ix ); // ofstream A_mat( "A" ); // A_mat << A << endl; } { C->randomize( -10.0, 10.0, &ix ); // ofstream C_mat( "C" ); // C_mat << C << endl; } y.randomize( -10.0, 10.0, &ix ); { // ofstream y_vec( "y" ); // y_vec << y << endl; } // g = - Q x + A\T y + C\T z + xdual g->copyFrom( *xdual ); Q->mult( 1.0, *g, -1.0, x ); A->transMult( 1.0, *g, 1.0, y ); C->transMult( 1.0, *g, 1.0, z ); // bA = A x A->mult( 0.0, *bA, 1.0, x ); { // ofstream bA_vec( "bA" ); // bA_vec << bA << endl; } // Have a randomly generated sprime. // C x - s = 0. Let q + sprime = s, i.e. q = s - sprime. // Compute s and temporarily store in q C->mult( 0.0, s, 1.0, x ); // Now compute the real q = s - sprime OoqpVectorHandle q(la->newVector( mz )); q->copyFrom( s ); q->axpy( -1.0, *sprime ); // Adjust bl and bu appropriately bl->axpy( 1.0, *q ); bu->axpy( 1.0, *q ); bl->selectNonZeros( *iclow ); bu->selectNonZeros( *icupp ); { // ofstream bl_vec( "bl" ); // bl_vec << bl << endl; // ofstream bu_vec( "bu" ); // bu_vec << bu << endl; } }
void QpGenData::getbA( OoqpVector& bout ) { bout.copyFrom( *bA ); }
void SimpleVector::copyFrom( OoqpVector& vec ) { assert( vec.length() == n ); vec.copyIntoArray( this->v ); }
void QpGenData::getg( OoqpVector& myG ) { myG.copyFrom( *g ); }
void QpGenData::randomlyChooseBoundedVariables( OoqpVector& x, OoqpVector& dualx, OoqpVector& xlow_, OoqpVector& ixlow_, OoqpVector& xupp_, OoqpVector& ixupp_, double * ix, double percentLowerOnly, double percentUpperOnly, double percentBound ) { int i; double drand( double * ); // Initialize the upper and lower bounds on x int n = x.length(); double * sxlow = new double[n]; double * sixlow = new double[n]; double * sxupp = new double[n]; double * sixupp = new double[n]; double * sx = new double[n]; double * sdualx = new double[n]; for( i = 0; i < n; i++ ) { double r = drand(ix); //cout << " r: " << r << " "; if( r < percentLowerOnly ) { //cout << "i= " << i << " Lower bound " << endl; sixlow[i] = 1.0; sxlow[i] = (drand(ix) - 0.5) * 3.0; sixupp[i] = 0.0; sxupp[i] = 0.0; } else if ( r < percentLowerOnly + percentUpperOnly ) { //cout << "i= " << i << " Upper bound " << endl; sixlow[i] = 0.0; sxlow[i] = 0.0; sixupp[i] = 1.0; sxupp[i] = (drand(ix) - 0.5) * 3.0; } else if ( r < percentLowerOnly + percentUpperOnly + percentBound ) { //cout << "i= " << i << " Two-sided bound " << endl; sixlow[i] = 1.0; sxlow[i] = (drand(ix) - 0.5) * 3.0; sixupp[i] = 1.0; sxupp[i] = sxlow[i] + drand(ix) * 10.0; } else { // it is free //cout << "i= " << i << " Free " << endl; sixlow[i] = 0.0; sxlow[i] = 0.0; sixupp[i] = 0.0; sxupp[i] = 0.0; } } xlow_. copyFromArray( sxlow ); ixlow_.copyFromArray( sixlow ); xupp_. copyFromArray( sxupp ); ixupp_.copyFromArray( sixupp ); for ( i = 0; i < n; i++ ) { if( sixlow[i] == 0.0 && sixupp[i] == 0.0 ) { // x[i] not bounded sx[i] = 20.0 * drand(ix) - 10.0; sdualx[i] = 0.0; } else if ( sixlow[i] != 0.0 && sixupp[i] != 0.0 ) { // x[i] is bounded above and below double r = drand( ix ); if( r < 0.33 ) { // x[i] is on its lower bound sx[i] = sxlow[i]; sdualx[i] = 10.0 * drand( ix ); } else if ( r > .66 ) { // x[i] is on its upper bound sx[i] = sxupp[i]; sdualx[i] = -10.0 * drand( ix ); } else { // x[i] is somewhere in between double theta = .99 * drand( ix ) + .005; sx[i] = (1 - theta) * sxlow[i] + theta * sxupp[i]; sdualx[i] = 0.0; } } else if ( sixlow[i] != 0.0 ) { // x[i] is only bounded below if( drand( ix ) < .33 ) { // x[i] is on its lower bound sx[i] = sxlow[i]; sdualx[i] = 10.0 * drand( ix ); } else { // x[i] is somewhere above its lower bound sx[i] = sxlow[i] + 0.005 + 10.0 * drand(ix); sdualx[i] = 0.0; } } else { // x[i] only has an upper bound if( drand(ix) > .66 ) { // x[i] is on its upper bound sx[i] = sxupp[i]; sdualx[i] = -10.0 * drand( ix ); } else { // x[i] is somewhere below its upper bound sx[i] = sxupp[i] - 0.005 - 10.0 * drand(ix); sdualx[i] = 0.0; } } // end else x[i] only has an upper bound } // end for ( i = 0; i < n; i++ ) x.copyFromArray( sx ); dualx.copyFromArray( sdualx ); delete [] sxlow; delete [] sxupp; delete [] sixlow; delete [] sixupp; delete [] sx; delete [] sdualx; }