Copy the Solution Values by User Code
Solve()
returns a Solution object, holding a Solution Status and the result value.
With a default Configuration, OPTANO.Modeling will import the solution back into the model's variables automatically. This perfectly matches to the use case "Create A Model, solve it, report back to user"
In some cases, OPTANO.Modeling shall not copy automatically, e.g. when different solutions shall be compared, if they have been optimized in several steps or similar.
In these cases,
Switch off automatic copying
Automatic copying can be switched off by creating the ModelScope with a custom Configuration, or by altering the value of an existing ModelScope:
using (var scope = new ModelScope())
{
// ...
scope.CopySolutionToModel = false;
// from now on, solver.Solve() will not automatically copy solution values into the variables
// ...
}
Copy Solution Values in User Code
If all Variables are grouped into one or more VariableCollections, just use these to copy the values.
Compare the Getting Started Example.
// import the results back into the model
designModel.Model.VariableCollections.ForEach(vc => vc.SetVariableValues(solution.VariableValues));
Copy Solution Values without using VariableCollections
Copying the values without iterating the set of @VariableCollection works as well, but usually takes a little longer.
// Copy all variables
foreach (var variable in designModel.Model.Variables)
{
// double check, if variable has been part of the model, when it was solved
if (solution.VariableValues.ContainsKey(variable.Name))
{
variable.Value = solution.VariableValues[variable.Name];
}
}