#include #include "ampl/ampl_c.h" int main(int argc, char **argv) { // Create an AMPL instance AMPL *ampl; AMPL_CALL(AMPL_Create(&l)); /* // If the AMPL installation directory is not in the system search path: AMPL_ENVIRONMENT *env; AMPL_EnvironmentCreateWithBin(&env, "full path to the AMPL installation directory",""); AMPL_CALL(AMPL_CreateWithEnv(&l, env)); */ if (argc > 1) AMPL_CALL(AMPL_SetOption(ampl, "solver", argv[1])); // Read the model and data files. const char *modelDirectory = argc == 3 ? argv[2] : "../models"; char mod[256]; char dat[256]; sprintf(mod, "%s%s", modelDirectory, "/diet/diet.mod"); sprintf(dat, "%s%s", modelDirectory, "/diet/diet.dat"); AMPL_CALL(AMPL_Read(ampl, mod)); AMPL_CALL(AMPL_ReadData(ampl, dat)); // Solve AMPL_CALL(AMPL_Solve(ampl, "", "")); // Get objective entity value by AMPL name double value; AMPL_CALL(AMPL_GetValueNumeric(ampl, "Total_Cost", &value)); printf("Objective is: %f\n", value); // Reassign data - specific instances const char *foods[] = {"BEEF", "HAM"}; double costs[] = {5.01, 4.55}; for (size_t i = 0; i < 2; i++) { AMPL_TUPLE *tuple; AMPL_TupleCreateString(&tuple, 1, &foods[i]); AMPL_CALL(AMPL_ParameterInstanceSetNumericValue(ampl, "cost", tuple, costs[i])); AMPL_TupleFree(&tuple); } printf("Increased costs of beef and ham.\n"); // Resolve and display objective AMPL_CALL(AMPL_Solve(ampl, "", "")); AMPL_CALL(AMPL_GetValueNumeric(ampl, "Total_Cost", &value)); printf("New objective is: %f\n", value); // Reassign data - all instances const double elements[8] = {3, 5, 5, 6, 1, 2, 5.01, 4.55}; AMPL_CALL(AMPL_ParameterSetArgsDoubleValues(ampl, "cost", 8, elements)); printf("Updated all costs.\n"); // Resolve and display objective AMPL_CALL(AMPL_Solve(ampl, "", "")); AMPL_CALL(AMPL_GetValueNumeric(ampl, "Total_Cost", &value)); printf("New objective is: %f\n", value); // Get the values of the variable Buy in a dataframe object AMPL_DATAFRAME *dataframe; char *dataframe_string; AMPL_CALL(AMPL_EntityGetValues(ampl, "Buy", NULL, 0, &dataframe)); AMPL_DataFrameToString(dataframe, &dataframe_string); // Print them printf("%s\n", dataframe_string); AMPL_StringFree(&dataframe_string); AMPL_DataFrameFree(&dataframe); // Get the values of an expression into a DataFrame object const char *expr[] = {"{j in FOOD} 100*Buy[j]/Buy[j].ub"}; AMPL_CALL(AMPL_GetData(ampl, expr, 1, &dataframe)); AMPL_CALL(AMPL_DataFrameToString(dataframe, &dataframe_string)); // Print them printf("%s\n", dataframe_string); AMPL_StringFree(&dataframe_string); AMPL_DataFrameFree(&dataframe); //AMPL_EnvironmentFree(&env); AMPL_Free(&l); return 0; }