#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
using namespace daal::algorithms::optimization_solver;
using namespace daal::data_management;
string datasetFileName = "../data/batch/custom.csv";
const size_t nIterations = 1000;
const size_t nFeatures = 4;
const float learningRate = 0.01f;
const double accuracyThreshold = 0.02;
float initialPoint[nFeatures + 1] = {1, 1, 1, 1, 1};
int main(int argc, char *argv[])
{
FileDataSource<CSVFeatureManager> dataSource(datasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
daal::services::Status s;
NumericTablePtr data = HomogenNumericTable<>::create(nFeatures, 0, NumericTable::doNotAllocate, &s);
checkStatus(s);
NumericTablePtr dependentVariables = HomogenNumericTable<>::create(1, 0, NumericTable::doNotAllocate, &s);
checkStatus(s);
NumericTablePtr mergedData = MergedNumericTable::create(data, dependentVariables, &s);
checkStatus(s);
dataSource.loadDataBlock(mergedData.get());
size_t nVectors = data.get() ? data->getNumberOfRows() : 1;
services::SharedPtr<logistic_loss::Batch<> > batch(new logistic_loss::Batch<>(nVectors));
batch->input.set(logistic_loss::data, data);
batch->input.set(logistic_loss::dependentVariables, dependentVariables);
optimization_solver::sgd::Batch<> sgdAlgorithm(batch);
sgdAlgorithm.input.set(optimization_solver::iterative_solver::inputArgument,
HomogenNumericTable<>::create(initialPoint, 1, nFeatures + 1, &s));
checkStatus(s);
sgdAlgorithm.parameter.learningRateSequence =
HomogenNumericTable<>::create(1, 1, NumericTable::doAllocate, learningRate, &s);
checkStatus(s);
sgdAlgorithm.parameter.nIterations = nIterations;
sgdAlgorithm.parameter.accuracyThreshold = accuracyThreshold;
s = sgdAlgorithm.compute();
checkStatus(s);
printNumericTable(sgdAlgorithm.getResult()->get(optimization_solver::iterative_solver::minimum), "Minimum:");
printNumericTable(sgdAlgorithm.getResult()->get(optimization_solver::iterative_solver::nIterations), "Number of iterations performed:");
return 0;
}