Hello.
I am trying to run the following piece of code, but I am having some errors when compiling.
#include <iostream>
#include <string>
#include <range/v3/all.hpp>
#include "ampl/ampl.h"
namespace rangesv3 = ::ranges;
using namespace std;
using namespace ampl;
int main(int argc, char **argv) {
try {
int n = 10;
// Create an AMPL instance
AMPL ampl;
// sets
ampl.eval("set N;");
ampl.eval("param distance {N, N};");
// N
vector<string> N = rangesv3::views::iota(0, n)
| rangesv3::views::transform([](const int& i) -> string { return to_string(i); })
| rangesv3::to<vector<string>>();
vector<const char *> N_ptr = N
| rangesv3::views::transform([](const string& i_str) -> const char * { return i_str.data(); })
| rangesv3::to<vector<const char *>>();
DataFrame N_df(1, "N");
N_df.setColumn("N", N_ptr.data(), n);
ampl.setData(N_df, "N");
// distances
vector<vector<double>> distances_double (n, vector<double>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
distances_double[i][j] = i * j;
vector<const double *> distances = distances_double
| rangesv3::views::transform([](const vector<double>& distance_line) -> const double * {
return distance_line.data();
})
| rangesv3::to<vector<const double *>>();
DataFrame distance_df = DataFrame(2, StringArgs("N", "N", "distance"));
distance_df.setMatrix(N_ptr.data(), N_ptr.data(), distances.data()); // error here
ampl.setData(distance_df);
return 0;
} catch (const std::exception &e) {
std::cout << e.what() << "\n";
return 1;
}
}
The error is the one below.
~/ampl_test/src/main.cpp: In function ‘int main(int, char**)’:
~/ampl_test/src/main.cpp:53:30: error: no matching function for call to ‘ampl::DataFrame::setMatrix(const char**, const char**, const double**)’
53 | distance_df.setMatrix(N_ptr.data(), N_ptr.data(), distances.data());
| ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What is quite strange, because, if we take a look at the AMPL example dietmodel.cc
, we have almost the same thing:
double amounts[6][8] = {{60, 8, 8, 40, 15, 70, 25, 60},
{20, 0, 10, 40, 35, 30, 50, 20},
{10, 20, 15, 35, 15, 15, 25, 15},
{15, 20, 10, 10, 15, 15, 15, 10},
{928, 2180, 945, 278, 1182, 896, 1329, 1397}, {295, 770, 440, 430, 315, 400, 379, 450}};
// Note the use of ampl::StringArgs to pass an array of strings
df = ampl::DataFrame(2, ampl::StringArgs("NUTR", "FOOD", "amt"));
df.setMatrix(nutrients, foods, amounts);
ampl.setData(df);
Have anyone passed by this before?
Btw, the repository is here.
Thanks and regards.