void AccpmVector::append(const AccpmVector &v) { AccpmVector tmp(size() + v.size()); if (tmp.size() > 0) { memcpy(tmp.addr(), addr(), sizeof(double) * size()); memcpy(tmp.addr() + size(), v.addr(), sizeof(double) * v.size()); } ref(tmp); }
void AccpmGenMatrix::assignColumn(int colId, const AccpmVector &v) { int numCols = size(1); assert(colId >= 0); assert(colId < numCols); assert(size(0) == v.size()); double *destaddr = addr() + size(0)*(colId); memcpy(destaddr, v.addr(), sizeof(double)*v.size()); }
void AccpmGenMatrix::assignRow(int rowId, const AccpmVector &v) { assert(size(1) == v.size()); for (int j = 0; j < size(1); ++j) { (*this)(rowId, j) = v(j); } }
void AccpmVector::times(const AccpmVector &v) { assert(size() == v.size()); for (int i = 0; i < size(); ++i) { (*this)(i) *= v(i); } }
void AccpmVector::rdivide(const AccpmVector &v) { assert(size() == v.size()); for (int i = 0; i < size(); ++i) { double val = v(i); assert(!DBL_CMP(val, 0)); (*this)(i) /= val; } }
bool AccpmVector::operator==(const AccpmVector &v) const { if (size() != v.size()) { return false; } for (int i = 0; i < size(); ++i) { if (!DBL_CMP((*this)(i), v(i))) { return false; } } return true; }
void AccpmGenMatrix::scaleColumn(int i, const AccpmVector &d) { assert(size(0) == d.size()); #if (defined(WIN32)) for (int ix = 0; ix < size(0); ++ix) { (*this)(ix, i) *= d(ix); } #else LaIndex colIndex(i,i); LaIndex rowIndex = index(0); AccpmVector col(RealMatrix::operator()(rowIndex, colIndex)); col.times(d); #endif }
virtual int eval(const AccpmVector &y, AccpmVector &functionValue, AccpmGenMatrix &subGradients, AccpmGenMatrix *info) { AccpmVector Ay(_b->size()); AccpmLAMatVecMult(*_A, y, Ay); functionValue = 0.5 * AccpmLADotProd(Ay, y) - AccpmLADotProd(*_b, y); // A * y - b; AccpmLAAddMult(Ay, -1, *_b); memcpy(subGradients.addr(), Ay.addr(), sizeof(double)*Ay.size()); if (info) { *info = 1; } return 0; }
static void createSDM(AccpmGenMatrix &A, AccpmVector &b) { int n = b.size(); A = 0; b = 0; b(0) = 1; A(0,0) = 2; A(0,1) = -1; A(n-1,n-1) = 2; A(n-1,n-2) = -1; for(int i = 1; i < n-1; ++i) { A(i, i-1) = -1; A(i,i) = 2; A(i, i+1) = -1; } //cout << "A:\n" << A << endl; //cout << "b:\n" << b << endl; }
int MCFSmoothOracleFunction::eval(const AccpmVector &y, AccpmVector &functionValue, AccpmGenMatrix &subGradients, AccpmGenMatrix *info) { if (_data->_type == MCFData::KLEINROCK) { //f2 = - sum(2*sqrt(y .* C1) - 1 - y .* C1); % objective function int n = y.size(); AccpmVector yc3 = _data->_capacity; yc3.times(y); AccpmVector tmp(n); tmp = 1; AccpmLAAddMult(tmp, 1, yc3); tmp.negate(); for (int i = 0; i < n; ++i) { tmp(i) += 2*sqrt(yc3(i)); } functionValue = -tmp.sum(); //df2 = - (sqrt(C1./ y ) - C1); % computes the gradiant AccpmVector c3 = _data->_capacity; tmp = c3; tmp.rdivide(y); for (int i = 0; i < n; ++i) { tmp(i) = sqrt(tmp(i)); } AccpmLAAddMult(c3, -1, tmp); memcpy(subGradients.addr(), c3.addr(), sizeof(double)*n); if (info) { assert(info->size(0) == n); //d2f2 = 0.5 * sqrt(C1) .* y .^(-1.5) ;% computes the diag of the hessian tmp.rdivide(y); AccpmLAScale(0.5, tmp); memcpy(info->addr(), tmp.addr(), sizeof(double)*n); } } return 0; }
int NewtonSolution::warmStart(const Manager &manager) { if (manager.getSizeOfs() == 0) { _s = 1; if (manager.inPhase2()) { _s0 = 1; } return 0; } int n = _s.size(); AccpmVector sp; double smin = ACCPM_PLUS_INF; int ls = manager.getPointGen()._s.size(); if (ls > 1) { for (int i = 0; i < ls - 1; ++i) { if (smin > manager.getPointGen()._s(i)) { smin = manager.getPointGen()._s(i); } } if (manager.inPhase2() && manager.previousPhase2()) { sp = AccpmVector(n - ls + 1); sp = smin; } else { sp = AccpmVector(n - ls); if (smin > manager.getPointGen()._s(ls-1)) { smin = manager.getPointGen()._s(ls-1); } sp = smin; } } else { sp = AccpmVector(n-ls); sp = 1; } if (manager.inPhase2()) { if (manager.previousPhase2()) { assert(n == ls-1 + sp.size()); assert(ls >= 2); _s = AccpmVector(n); LaIndex index(0, ls-2); // Following inject causes trouble on WINDOWS .NET 2003 // _s(index).inject(manager.getPointGen()._s(index)); for (int i = 0; i < ls-1; ++i) { _s(i) = manager.getPointGen()._s(i); } for (int i = ls - 1; i < n; ++i) { _s(i) = sp(i-ls+1); } _s0 = manager.getPointGen()._s(ls-1); } else { _s = manager.getPointGen()._s; _s.append(sp); _s0 = sp(0); } } else { if (ls >= 1) { _s = manager.getPointGen()._s; _s.append(sp); } else { _s = sp; } } return 0; }