Simple Model
This is a version of the 'chess2' example, modified to be an Xpress Insight app. It demonstrates how to write a model so that Xpress Insight can capture or inject the model's input data, depending upon the run mode, while still allowing the model developer to run the model from outside of Xpress Insight, e.g. from the Mosel command line.
model Chess2
uses "mmxprs" ! Load the Xpress Optimizer
uses "mminsight" ! Load the Insight interface
public declarations
! Model inputs
UnitTypes: set of string
ResourceTypes: set of string
ResourceLimits: array(ResourceTypes) of real
ProfitPerUnit: array(UnitTypes) of real
UnitResourceRequirements: array(UnitTypes,ResourceTypes) of real
! Mathematical model objects (providing model results)
unitstobuild: array(UnitTypes) of mpvar
ResourceLimitConstraints: array(ResourceTypes) of linctr
MaxProfit: linctr
end-declarations
! Procedure to populate model with initial input data
procedure loaddata
ProfitPerUnit:: (["small","large"])[5,20]
UnitResourceRequirements:: (["small"],["wood","mc_time"]) [1,3]
UnitResourceRequirements:: (["large"],["wood","mc_time"]) [3,2]
ResourceLimits:: (["wood","mc_time"])[200,400]
finalize( UnitTypes )
finalize( ResourceTypes )
end-procedure
! Procedure to build and solve optimization problem
procedure solvemodel
! Build whole units only
forall(u in UnitTypes) unitstobuild(u) is_integer
! Define profit
MaxProfit:= sum(u in UnitTypes) ProfitPerUnit(u)*unitstobuild(u)
! Don't use more than available resources
forall(r in ResourceTypes)
ResourceLimitConstraints(r) := ( sum(u in UnitTypes)
UnitResourceRequirements(u,r)*unitstobuild(u) ) <= ResourceLimits(r)
! Solve the mixed integer problem
insightmaximize(MaxProfit)
! Print the solution to the run log
writeln("Solution:\n Objective: ", getobjval)
forall(u in UnitTypes)
writeln(u, ":", unitstobuild(u).sol)
end-procedure
! Define actions to perform in 'load' execution mode
!@insight.execmodes.LOAD
public procedure doload
! Scenario is being 'loaded' through Xpress Insight
! Initialize input arrays and then terminate
loaddata
end-procedure
! Define actions to perform in 'run' execution mode
!@insight.execmodes.RUN
public procedure dorun
! Scenario is being 'run' through Xpress Insight
! Populate with Insight scenario data and then solve optimization
insightpopulate
solvemodel
end-procedure
! Define actions to perform when model is run standalone (without Xpress Insight)
!@insight.execmodes.NONE
public procedure doloadandrun
! Model is being run outside of Xpress Insight, e.g. from the Mosel command-line tool
! Initialize input arrays then solve optimization
loaddata
solvemodel
end-procedure
! Automatically call the procedure with annotation matching the current execution mode
insightdispatch
end-model Insight will by default manage all public entities of supported types in the scenario, with entities storing linctr and mpvar values in the result data and all others in the input, but you can change this by adding annotations as we'll describe in section Annotations.
