Class SolverBase
The solver base class. This class provides functionality for the ModelTransformation that needs to be supported by all solver adapters.
Inheritance
Inherited Members
Namespace: OPTANO.Modeling.Optimization
Assembly: Optimization.dll
Syntax
public abstract class SolverBase : ISolver, ICanSolve<Model, Solution>, IAbortable, IDisposable
Constructors
SolverBase(SolverConfiguration, NativeMultiObjectiveSupport, MinOperatorReplacement, MaxOperatorReplacement, FloorOperatorReplacement, CeilOperatorReplacement, AbsOperatorReplacement, NotConstraintReplacement, OrConstraintReplacement, ImplicationConstraintReplacement, SOSHandling, SOSHandling, SOSHandling, TriviallyFeasibleConstraintHandling)
Initializes a new instance of the SolverBase class.
If any operator replacement has the value DoNotReplace
, the SolverAdapter needs to handle that OperatorExpression type manually.
Declaration
protected SolverBase(SolverConfiguration config, NativeMultiObjectiveSupport nativeMultiObjectiveSupport = NativeMultiObjectiveSupport.Unsupported, MinOperatorReplacement minOperatorReplacement = MinOperatorReplacement.ReplaceWithDefaultMethod, MaxOperatorReplacement maxOperatorReplacement = MaxOperatorReplacement.ReplaceWithDefaultMethod, FloorOperatorReplacement floorOperatorReplacement = FloorOperatorReplacement.ReplaceWithDefaultMethod, CeilOperatorReplacement ceilOperatorReplacement = CeilOperatorReplacement.ReplaceWithDefaultMethod, AbsOperatorReplacement absOperatorReplacement = AbsOperatorReplacement.ReplaceWithDefaultMethod, NotConstraintReplacement notConstraintReplacement = NotConstraintReplacement.ReplaceWithDefaultMethod, OrConstraintReplacement orConstraintReplacement = OrConstraintReplacement.ReplaceWithDefaultMethod, ImplicationConstraintReplacement implicationConstraintReplacement = ImplicationConstraintReplacement.ReplaceWithDefaultMethod, SOSHandling sos1Handling = SOSHandling.Emulated, SOSHandling sos2Handling = SOSHandling.Emulated, SOSHandling sos3Handling = SOSHandling.Emulated, TriviallyFeasibleConstraintHandling trivialConstraintHandling = TriviallyFeasibleConstraintHandling.Native)
Parameters
Type | Name | Description |
---|---|---|
SolverConfiguration | config | |
NativeMultiObjectiveSupport | nativeMultiObjectiveSupport | The native support for multiobjective optimization. |
MinOperatorReplacement | minOperatorReplacement | The desired MinOperatorReplacement method. |
MaxOperatorReplacement | maxOperatorReplacement | The desired MaxOperatorReplacement method. |
FloorOperatorReplacement | floorOperatorReplacement | The desired FloorOperatorReplacement method. |
CeilOperatorReplacement | ceilOperatorReplacement | The desired CeilOperatorReplacement method. |
AbsOperatorReplacement | absOperatorReplacement | The desired AbsOperatorReplacement method. |
NotConstraintReplacement | notConstraintReplacement | The desired NotConstraintReplacement method. |
OrConstraintReplacement | orConstraintReplacement | The desired OrConstraintReplacement method. |
ImplicationConstraintReplacement | implicationConstraintReplacement | The desired OPTANO.Modeling.Optimization.SolverBase.ImplicationConstraintReplacement method. |
SOSHandling | sos1Handling | The indicator whether SOS of type S1 should be handled by the solver adapter iteself (Native), or by the ModelTransformation (Emulated). |
SOSHandling | sos2Handling | The indicator whether SOS of type S2 should be handled by the solver adapter iteself (Native), or by the ModelTransformation (Emulated). |
SOSHandling | sos3Handling | The indicator whether SOS of type S3 should be handled by the solver adapter iteself (Native), or by the ModelTransformation (Emulated). |
TriviallyFeasibleConstraintHandling | trivialConstraintHandling | If the solver cannot handle trivially feasible constraints, the user needs to make sure to remove them from the model before calling Solve(Model, Dictionary<Variable, Double>). |
Fields
_busyLock
The busy lock object.
Declaration
protected readonly object _busyLock
Field Value
Type | Description |
---|---|
System.Object |
CachedMergedObjectives
Declaration
protected Dictionary<int, Tuple<int, Objective>> CachedMergedObjectives
Field Value
Type | Description |
---|---|
System.Collections.Generic.Dictionary<System.Int32, System.Tuple<System.Int32, Objective>> |
Properties
_model
Gets or sets the model that this GenericSolverBase<T> is working on.
Declaration
protected Model _model { get; set; }
Property Value
Type | Description |
---|---|
Model |
Configuration
Gets or sets the configuration as SolverConfiguration. To get the solver specific {T}-configuration, cast the ISolver (or SolverBase) variable to GenericSolverBase<T>.
Declaration
public SolverConfiguration Configuration { get; set; }
Property Value
Type | Description |
---|---|
SolverConfiguration |
Implements
CurrentModel
Gets the current Model.
Declaration
public Model CurrentModel { get; }
Property Value
Type | Description |
---|---|
Model |
IsMultiHierachicalModel
Gets a value indicating whether model is multi hierachical.
Declaration
protected bool IsMultiHierachicalModel { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Transformation
Gets the transformation helper.
Declaration
protected ModelTransformation Transformation { get; }
Property Value
Type | Description |
---|---|
ModelTransformation |
TriviallyFeasibleConstraintHandling
Gets the support level for trivial feasible constraints, such as "c: 1 <= 2".
If Unsupported, the user
needs to remove all trivial constraints from the model before calling Solve(Model, Dictionary<Variable, Double>).
Declaration
public TriviallyFeasibleConstraintHandling TriviallyFeasibleConstraintHandling { get; }
Property Value
Type | Description |
---|---|
TriviallyFeasibleConstraintHandling |
Methods
Abort()
Aborts the current optimization.
Declaration
public abstract void Abort()
Implements
AddObjective(Objective)
Internal method to add an Objective to the current solver model.
Should be called from the OPTANO.Modeling.Optimization.Solver.Interfaces.ICanManipulateANativeSolver.AddObjective(OPTANO.Modeling.Optimization.Objective) method.
This method does not check whether the solver
Declaration
protected abstract void AddObjective(Objective objective)
Parameters
Type | Name | Description |
---|---|---|
Objective | objective | The objective to add |
BuildSolverModelAdapterSpecific(Int32)
This method needs to build the solver API's specific model from the current OPTANO Model. It is important to perform the whole work in this call, since benchmark statistics, such as required RAM, and time to build the model, are gathered during the execution of this method.
Declaration
protected abstract void BuildSolverModelAdapterSpecific(int prioLevel = 0)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | The prio Level. |
CanExportFile(FileInfo, ExportTime, Boolean)
Determines a value indicating whether this solver can export the current _model.
Declaration
protected virtual bool CanExportFile(FileInfo outputFile, ExportTime exportTime, bool printHints = false)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileInfo | outputFile | The file to export. |
ExportTime | exportTime | Export before or after solve. |
System.Boolean | printHints |
|
Returns
Type | Description |
---|---|
System.Boolean |
|
ClearLastModel()
Clears the last model.
Declaration
public abstract void ClearLastModel()
Implements
Dispose()
Declaration
public void Dispose()
Implements
DisposeCustom()
Disposes this solver instance. For solvers such as the GurobiSolver, the license that was used for the solver instance is freed when this method is called.
Declaration
protected virtual void DisposeCustom()
ExportFileSolverSpecific(FileInfo)
This method will be called right after (the first call of) BuildSolverModelAdapterSpecific(Int32) and should utilize the Solver-built in capabilities to export models in different file formats. By default this method only exports models as LP, using the LPExporter.
@Devs: Don't forget to check if IsMultiHierachicalModel and to return false
for CanExportFile(FileInfo, ExportTime, Boolean), if your solver cannot export such a model correctly!
ExportFileSolverSpecific(FileInfo) and CanExportFile(FileInfo, ExportTime, Boolean) will only be called, if ModelOutputFile is not null
.
Declaration
protected virtual void ExportFileSolverSpecific(FileInfo filteredFileToWrite)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileInfo | filteredFileToWrite |
GetMergedObjectiveForPriorityLevel(Int32, Boolean)
Get the Objective for the given prioLevel
.
This can either be an unmodified objective, if there exists only 1 objective for the given prioLevel
, or a merged objective, if there are more than 1 objective for the given prioLevel
. The merged objective's Sense will be Minimize.
Declaration
protected Objective GetMergedObjectiveForPriorityLevel(int prioLevel, bool tryGetCached)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | The priority level. |
System.Boolean | tryGetCached | If value is Only use this if you know what your're doing. |
Returns
Type | Description |
---|---|
Objective | The Objective. |
GetRelevantObjectivesForPrioLevel(Int32)
Declaration
protected Objective[] GetRelevantObjectivesForPrioLevel(int prioLevel)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel |
Returns
Type | Description |
---|---|
Objective[] |
RebuildSolverModelAdapterSpecific(Int32)
This method should rebuild the required parts of the Model in the specific Solver-API when a resolve happens. By default, BuildSolverModelAdapterSpecific(Int32) is called, which means that the whole model will be rebuilt. For many solvers, this might not be the intended behavior, since they support warm starts (i.e. efficient resolves without rebuilding of the whole model). Make sure to override this method accordingly.
Declaration
protected virtual void RebuildSolverModelAdapterSpecific(int prioLevel = 0)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | The priority level. |
RemoveConstraint(String)
Internal method to remove a constraint from the current solver model.
Should be called from the OPTANO.Modeling.Optimization.Solver.Interfaces.ICanManipulateANativeSolver.RemoveConstraint(System.String) method.
This method does not check whether the solver
Declaration
protected abstract void RemoveConstraint(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The Name of the constraint to remove. |
ResolveAdapterSpecific(Int32, IDictionary<Variable, Double>)
This method is called, if the passed Model reference of Solve(Model, Dictionary<Variable, Double>) equals the last solved CurrentModel in this solver instance.
If your solver can benefit from, e.g., a warm start, override this method and implement the steps that are required to resolve
a known Model.
By default
, this method will simply call SolveAdapterSpecific(Int32, IDictionary<Variable, Double>), i.e. you will not benefit from any warmstart capabilieties of your solver.
Declaration
protected virtual Solution ResolveAdapterSpecific(int prioLevel = 0, IDictionary<Variable, double> variableValues = null)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | The priority level to resolve for. |
System.Collections.Generic.IDictionary<Variable, System.Double> | variableValues | The start values. |
Returns
Type | Description |
---|---|
Solution | The Solution. |
RestoreModel(Solution)
Restores the model to the initial user input. I.e. all changes that were performed by TransformModel() are reverted.
Declaration
protected void RestoreModel(Solution solution)
Parameters
Type | Name | Description |
---|---|---|
Solution | solution | The solution. |
SetBranchingDirectionAdapterSpecific()
If the solver adapter supports BranchingDirection, override this method and set the respective BranchingDirection. By default, a warning message will be printed, if BranchingDirection is set for at least one Variable in the current Model and the value will be ignored.
Declaration
protected virtual void SetBranchingDirectionAdapterSpecific()
SetBranchingPriorityAdapterSpecific()
If the solver adapter supports BranchingPriority, override this method and set the respective priorities. By default, a warning message will be printed, if BranchingPriority is set for at least one Variable in the current Model and the value will be ignored.
Declaration
protected virtual void SetBranchingPriorityAdapterSpecific()
SetConfigurationAdapterSpecific()
This method is called before every (re-)solve and should set all parameters from the adapter's Configuration in the respective solver.
Declaration
protected abstract void SetConfigurationAdapterSpecific()
SetMultiHierarchicalInterruptionCallback(SolverBase.InterruptMultiHierarchicalOptimization)
Sets the given callback with which the user can specify whether a multi hierarchical optimization should be interrupted.
Declaration
public void SetMultiHierarchicalInterruptionCallback(SolverBase.InterruptMultiHierarchicalOptimization callback)
Parameters
Type | Name | Description |
---|---|---|
SolverBase.InterruptMultiHierarchicalOptimization | callback | The callback delegate. |
Implements
SetTimelimitAndGapForCurrentStage(Int32, TimeSpan, Double)
Declaration
protected virtual void SetTimelimitAndGapForCurrentStage(int prioLevel, TimeSpan timeoutForStage, double mipGapForStage)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | |
System.TimeSpan | timeoutForStage | |
System.Double | mipGapForStage |
Solve(Model, Dictionary<Variable, Double>)
Solves the specified model
. This includes the TransformCurrentModel(), calling the SolveAdapterSpecific(Int32, IDictionary<Variable, Double>) solve-method OR the ResolveAdapterSpecific(Int32, IDictionary<Variable, Double>) solve-method, depending on the model
.
If your SolverAdapter can benefit from a Resolve
make sure to override the ResolveAdapterSpecific(Int32, IDictionary<Variable, Double>) method to do that job. By default
, the resolve-method reroutes to SolveAdapterSpecific(Int32, IDictionary<Variable, Double>). After a solution is obtained, the model
can be restored, i.e. the changes that were performed by TransformCurrentModel() are reverted, if the user specifies this by setting RestoreUserModelAfterSolve to true
.
If specified in CopySolutionToModel, the results of a solved Model will automatically
be written in the OPTANO.Modeling's Variables.
Declaration
public virtual Solution Solve(Model model, Dictionary<Variable, double> variableValues = null)
Parameters
Type | Name | Description |
---|---|---|
Model | model | The Model to solve. |
System.Collections.Generic.Dictionary<Variable, System.Double> | variableValues | The starting values to pass as |
Returns
Type | Description |
---|---|
Solution | The Solution. Before accessing the VariableValues, make sure that the Model is Feasible and the Solution is Feasible or Optimal. |
Implements
SolveAdapterSpecific(Int32, IDictionary<Variable, Double>)
This method should solve the CurrentModel, which is set in Solve(Model, Dictionary<Variable, Double>), before SolveAdapterSpecific(Int32, IDictionary<Variable, Double>) is called.
Declaration
protected abstract Solution SolveAdapterSpecific(int prioLevel = 0, IDictionary<Variable, double> variableValues = null)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | prioLevel | The priority level to solve for. |
System.Collections.Generic.IDictionary<Variable, System.Double> | variableValues | The variable values. |
Returns
Type | Description |
---|---|
Solution | The Solution. |
TransformModel()
Can transform the constraints of the model that contain OperatorExpressions as, e.g., Min into new expressions where these expressions are
replaced by new variables and constraints. This means that in the model also some implicit variables marking
new representing variables are set.
Furthermore, the operator constraints can be translated into common Constraints, so that they can be handled by default MIP solvers.
The resulting constraints will be added to the current model.
Each individual operator (for expressions and
constraints) can be controlled via the System.Enum properties in SolverBase.
Declaration
protected void TransformModel()