void serializationTest() { Tgs::Random::instance()->seed(0); shared_ptr<KernelEstimationInterpolator> di = buildRandom(); KernelEstimationInterpolator& uut = *di; uut.setStopDelta(0.0001); uut.setSigma(0.3); Tgs::Random::instance()->seed(0); double error = uut.estimateError(); QBuffer buf; buf.open(QBuffer::WriteOnly); uut.writeInterpolator(buf); buf.close(); buf.open(QBuffer::ReadOnly); KernelEstimationInterpolator copy; copy.readInterpolator(buf); Tgs::Random::instance()->seed(0); CPPUNIT_ASSERT_DOUBLES_EQUAL(error, copy.estimateError(), 0.0001); CPPUNIT_ASSERT_DOUBLES_EQUAL(0.3, copy.getSigma(), 0.0001); CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0001, copy.getStopDelta(), 0.0001); }
void optimizationTest() { srand(0); shared_ptr<DelaunayInterpolator> di = buildRandom(); DelaunayInterpolator& uut = *di; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.025, uut.estimateError(), 0.001); vector<double> d(2); d[0] = 0.0; d[1] = 0.0; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.903, uut.interpolate(d)[0], 0.001); d[0] = 0.5; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.568, uut.interpolate(d)[0], 0.001); d[0] = 1.0; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.057, uut.interpolate(d)[0], 0.001); d[0] = 1.5; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.047, uut.interpolate(d)[0], 0.001); d[0] = 2.0; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.036, uut.interpolate(d)[0], 0.001); d[0] = 0.1; CPPUNIT_ASSERT_DOUBLES_EQUAL(0.917, uut.interpolate(d)[0], 0.001); // // write out as a .pgm image // ofstream fs; // fs.open("output.pgm"); // double max = Normal::normal(0, 0.5) / n0; // fs << "P2" << endl; // fs << "100 100" << endl; // fs << "300" << endl; // int maxC = 0; // vector<double> p(2); // for (size_t py = 0; py < 100; py++) // { // for (size_t px = 0; px < 100; px++) // { // double x = px / 50.0 - 1.0; // double y = py / 50.0 - 1.0; // p[0] = x; // p[1] = y; // double e = sqrt(x * x + y * y); // double v = Normal::normal(e, .5) / n0; // const vector<double>& r = uut.interpolate(p); // int c = (int)((v - r[0]) / max * 255 + 128); // c = (int)((r[0]) / max * 255); // cout << r[0] << " "; // maxC = std::max(c, maxC); // fs << c << " "; // } // fs << endl; // } // fs.close(); }
void optimizationTest() { Tgs::Random::instance()->seed(0); shared_ptr<KernelEstimationInterpolator> di = buildRandom(); KernelEstimationInterpolator& uut = *di; uut.setStopDelta(0.0001); uut.setSigma(-1); CPPUNIT_ASSERT_DOUBLES_EQUAL(0.155, uut.getSigma(), 0.001); // // write out as a .pgm image // ofstream fs; // fs.open("output.pgm"); // double max = Normal::normal(0, 0.5) / n0; // fs << "P2" << endl; // fs << "100 100" << endl; // fs << "300" << endl; // int maxC = 0; // vector<double> p(2); // for (size_t py = 0; py < 100; py++) // { // for (size_t px = 0; px < 100; px++) // { // double x = px / 50.0 - 1.0; // double y = py / 50.0 - 1.0; // p[0] = x; // p[1] = y; // double e = sqrt(x * x + y * y); // double v = Normal::normal(e, .5) / n0; // const vector<double>& r = uut.interpolate(p); // int c = (int)((v - r[0]) / max * 255 + 128); // c = (int)((r[0]) / max * 255); // cout << r[0] << " "; // maxC = std::max(c, maxC); // fs << c << " "; // } // fs << endl; // } // fs.close(); }
void serializationTest() { srand(0); shared_ptr<DelaunayInterpolator> di = buildRandom(); DelaunayInterpolator& uut = *di; srand(0); double error = uut.estimateError(); QBuffer buf; buf.open(QBuffer::WriteOnly); uut.writeInterpolator(buf); buf.close(); buf.open(QBuffer::ReadOnly); DelaunayInterpolator copy; copy.readInterpolator(buf); srand(0); CPPUNIT_ASSERT_DOUBLES_EQUAL(error, copy.estimateError(), 0.0001); }
//-------------------------------------------------------------------------- // Test building a model void CoinModelUnitTest(const std::string & mpsDir, const std::string & netlibDir, const std::string & testModel) { // Get a model CoinMpsIO m; std::string fn = mpsDir+"exmip1"; int numErr = m.readMps(fn.c_str(),"mps"); assert( numErr== 0 ); int numberRows = m.getNumRows(); int numberColumns = m.getNumCols(); // Build by row from scratch { CoinPackedMatrix matrixByRow = * m.getMatrixByRow(); const double * element = matrixByRow.getElements(); const int * column = matrixByRow.getIndices(); const CoinBigIndex * rowStart = matrixByRow.getVectorStarts(); const int * rowLength = matrixByRow.getVectorLengths(); const double * rowLower = m.getRowLower(); const double * rowUpper = m.getRowUpper(); const double * columnLower = m.getColLower(); const double * columnUpper = m.getColUpper(); const double * objective = m.getObjCoefficients(); int i; CoinModel temp; for (i=0; i<numberRows; i++) { temp.addRow(rowLength[i],column+rowStart[i], element+rowStart[i],rowLower[i],rowUpper[i],m.rowName(i)); } // Now do column part for (i=0; i<numberColumns; i++) { temp.setColumnBounds(i,columnLower[i],columnUpper[i]); temp.setColumnObjective(i,objective[i]); if (m.isInteger(i)) temp.setColumnIsInteger(i,true);; } // write out temp.writeMps("byRow.mps"); } // Build by column from scratch and save CoinModel model; { CoinPackedMatrix matrixByColumn = * m.getMatrixByCol(); const double * element = matrixByColumn.getElements(); const int * row = matrixByColumn.getIndices(); const CoinBigIndex * columnStart = matrixByColumn.getVectorStarts(); const int * columnLength = matrixByColumn.getVectorLengths(); const double * rowLower = m.getRowLower(); const double * rowUpper = m.getRowUpper(); const double * columnLower = m.getColLower(); const double * columnUpper = m.getColUpper(); const double * objective = m.getObjCoefficients(); int i; for (i=0; i<numberColumns; i++) { model.addColumn(columnLength[i],row+columnStart[i], element+columnStart[i],columnLower[i],columnUpper[i], objective[i],m.columnName(i),m.isInteger(i)); } // Now do row part for (i=0; i<numberRows; i++) { model.setRowBounds(i,rowLower[i],rowUpper[i]); } // write out model.writeMps("byColumn.mps"); } // model was created by column - play around { CoinModel temp; int i; for (i=numberRows-1; i>=0; i--) temp.setRowLower(i,model.getRowLower(i)); for (i=0; i<numberColumns; i++) { temp.setColumnUpper(i,model.getColumnUpper(i)); temp.setColumnName(i,model.getColumnName(i)); } for (i=numberColumns-1; i>=0; i--) { temp.setColumnLower(i,model.getColumnLower(i)); temp.setColumnObjective(i,model.getColumnObjective(i)); temp.setColumnIsInteger(i,model.getColumnIsInteger(i)); } for (i=0; i<numberRows; i++) { temp.setRowUpper(i,model.getRowUpper(i)); temp.setRowName(i,model.getRowName(i)); } // Now elements for (i=0; i<numberRows; i++) { CoinModelLink triple=model.firstInRow(i); while (triple.column()>=0) { temp(i,triple.column(),triple.value()); triple=model.next(triple); } } // and by column for (i=numberColumns-1; i>=0; i--) { CoinModelLink triple=model.lastInColumn(i); while (triple.row()>=0) { assert (triple.value()==temp(triple.row(),i)); temp(triple.row(),i,triple.value()); triple=model.previous(triple); } } // check equal model.setLogLevel(1); assert (!model.differentModel(temp,false)); } // Try creating model with strings { CoinModel temp; int i; for (i=numberRows-1; i>=0; i--) { double value = model.getRowLower(i); if (value==-1.0) temp.setRowLower(i,"minusOne"); else if (value==1.0) temp.setRowLower(i,"sqrt(plusOne)"); else if (value==4.0) temp.setRowLower(i,"abs(4*plusOne)"); else temp.setRowLower(i,value); } for (i=0; i<numberColumns; i++) { double value; value = model.getColumnUpper(i); if (value==-1.0) temp.setColumnUpper(i,"minusOne"); else if (value==1.0) temp.setColumnUpper(i,"plusOne"); else temp.setColumnUpper(i,value); temp.setColumnName(i,model.getColumnName(i)); } for (i=numberColumns-1; i>=0; i--) { temp.setColumnLower(i,model.getColumnLower(i)); temp.setColumnObjective(i,model.getColumnObjective(i)); temp.setColumnIsInteger(i,model.getColumnIsInteger(i)); } for (i=0; i<numberRows; i++) { double value = model.getRowUpper(i); if (value==-1.0) temp.setRowUpper(i,"minusOne"); else if (value==1.0) temp.setRowUpper(i,"plusOne"); else temp.setRowUpper(i,value); temp.setRowName(i,model.getRowName(i)); } // Now elements for (i=0; i<numberRows; i++) { CoinModelLink triple=model.firstInRow(i); while (triple.column()>=0) { double value = triple.value(); if (value==-1.0) temp(i,triple.column(),"minusOne"); else if (value==1.0) temp(i,triple.column(),"plusOne"); else if (value==-2.0) temp(i,triple.column(),"minusOne-1.0"); else if (value==2.0) temp(i,triple.column(),"plusOne+1.0+minusOne+(2.0-plusOne)"); else temp(i,triple.column(),value); triple=model.next(triple); } } temp.associateElement("minusOne",-1.0); temp.associateElement("plusOne",1.0); temp.setProblemName("fromStrings"); temp.writeMps("string.mps"); // check equal model.setLogLevel(1); assert (!model.differentModel(temp,false)); } // Test with various ways of generating { /* Get a model. Try first with netlibDir, fall back to mpsDir (sampleDir) if that fails. */ CoinMpsIO m; std::string fn = netlibDir+testModel; double time1 = CoinCpuTime(); int numErr = m.readMps(fn.c_str(),""); if (numErr != 0) { std::cout << "Could not read " << testModel << " in " << netlibDir << "; falling back to " << mpsDir << "." << std::endl ; fn = mpsDir+testModel ; numErr = m.readMps(fn.c_str(),"") ; if (numErr != 0) { std::cout << "Could not read " << testModel << "; skipping test." << std::endl ; } } if (numErr == 0) { std::cout << "Time for readMps is " << (CoinCpuTime()-time1) << " seconds." << std::endl ; int numberRows = m.getNumRows(); int numberColumns = m.getNumCols(); // Build model CoinModel model; CoinPackedMatrix matrixByRow = * m.getMatrixByRow(); const double * element = matrixByRow.getElements(); const int * column = matrixByRow.getIndices(); const CoinBigIndex * rowStart = matrixByRow.getVectorStarts(); const int * rowLength = matrixByRow.getVectorLengths(); const double * rowLower = m.getRowLower(); const double * rowUpper = m.getRowUpper(); const double * columnLower = m.getColLower(); const double * columnUpper = m.getColUpper(); const double * objective = m.getObjCoefficients(); int i; for (i=0; i<numberRows; i++) { model.addRow(rowLength[i],column+rowStart[i], element+rowStart[i],rowLower[i],rowUpper[i],m.rowName(i)); } // Now do column part for (i=0; i<numberColumns; i++) { model.setColumnBounds(i,columnLower[i],columnUpper[i]); model.setColumnObjective(i,objective[i]); model.setColumnName(i,m.columnName(i)); if (m.isInteger(i)) model.setColumnIsInteger(i,true);; } // Test CoinSeedRandom(11111); time1 = 0.0; int nPass=50; for (i=0; i<nPass; i++) { double random = CoinDrand48(); int iSeed = (int) (random*1000000); //iSeed = 776151; CoinSeedRandom(iSeed); std::cout << "before pass " << i << " with seed of " << iSeed << std::endl ; buildRandom(model,CoinDrand48(),time1,i); model.validateLinks(); } std::cout << "Time for " << nPass << " CoinModel passes is " << time1 << " seconds\n" << std::endl ; } } }