#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
using namespace daal::data_management;
string datasetFileName = "../data/batch/lbfgs.csv";
const size_t nFeatures = 10;
const size_t nIterations = 1000;
const float stepLength = 1.0e-4f;
float initialPoint[nFeatures + 1] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
float expectedPoint[nFeatures + 1] = { 11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 1, &datasetFileName);
FileDataSource<CSVFeatureManager> dataSource(datasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr data(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
NumericTablePtr dependentVariables(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
NumericTablePtr mergedData(new MergedNumericTable(data, dependentVariables));
dataSource.loadDataBlock(mergedData.get());
services::SharedPtr<optimization_solver::mse::Batch<> > mseObjectiveFunction(
new optimization_solver::mse::Batch<>(data->getNumberOfRows()));
mseObjectiveFunction->input.set(optimization_solver::mse::data, data);
mseObjectiveFunction->input.set(optimization_solver::mse::dependentVariables, dependentVariables);
optimization_solver::lbfgs::Batch<> algorithm(mseObjectiveFunction);
algorithm.parameter.nIterations = nIterations;
algorithm.parameter.stepLengthSequence =
NumericTablePtr(new HomogenNumericTable<>(1, 1, NumericTableIface::doAllocate, stepLength));
algorithm.input.set(optimization_solver::iterative_solver::inputArgument,
NumericTablePtr(new HomogenNumericTable<>(initialPoint, 1, nFeatures + 1)));
algorithm.compute();
NumericTablePtr expectedCoefficients =
NumericTablePtr(new HomogenNumericTable<>(expectedPoint, 1, nFeatures + 1));
printNumericTable(expectedCoefficients,
"Expected coefficients:");
printNumericTable(algorithm.getResult()->get(optimization_solver::iterative_solver::minimum),
"Resulting coefficients:");
printNumericTable(algorithm.getResult()->get(optimization_solver::iterative_solver::nIterations),
"Number of iterations performed:");
return 0;
}