[AMPL 24497] Load parameter into C++

Hello,

I have a parameter in ampl of the form

param Test[SetA];
and I would like to save the values of this parameter into a vector in C++.
Is it possible to do it?

Thanks

Sam

Hi Sam,

Yes, you can retrieve the values and place them into a vector as follows:

ampl::AMPL ampl;
ampl.eval(“set SetA := 1…10; param Test{i in SetA} := 2*i;”);
ampl::DataFrame df = ampl.getParameter(“Test”).getValues();
std::vector values;
for (ampl::VariantRef : df.getColumn(“Test”)) {
values.push_back(v.dbl());
}

ampl.getParameter(“Test”).getValues() returns a ampl::DataFrame with the data for that parameter.
df.getColumn(“Test”) returns the column Test from the ampl::DataFrame.
You can then iterate over this column and place each value into the vector.

Thank you Filipe!!
That’s very helpful!!:relaxed:

Hello,

is it possible that there is mistake?? I tried it on my code but it returns two errors:

  1. expected unqualified-id before ‘:’ token …the one in the for loop
  2. ‘v’ was not declared in this scope …in the for loop

Thanks
Sam

Hi Sam,

Somehow “v” disappeared when copying the example here. In the for loop it should be: “ampl::VariantRef v” as follows:

for (ampl::VariantRef v: df.getColumn(“Test”))

Ok thanks…problem solved!:relaxed: