Accessing Attachments
As described in the Developer Guide, Xpress Insight allows files to be attached to an app or scenario. While the model is running within Xpress Insight, the Mosel interface will allow you to query, read and edit attachments of the scenario being run, as well as query and read (but not edit) attachments of the app to which that scenario belongs.
To demonstrate the use of an app and scenario attachments, in this example we have a Distances array that we want to populate with distances between cities. As this data will not change, it would be very wasteful to store a copy of these in every scenario of the app, so we store them in a file distances.dat which is attached to the app. In the example, we call insightgetappattach to copy this file to the working directory and then populate the Distances array from this. (The Distances array is annotated as ignored by Xpress Insight, so it will not be part of the Insight scenario data model.)
To demonstrate the use of scenario attachments, we then save the model's results to a file decorated with today's date, and attach this file to the current scenario.
model RoutePlanner
uses "mmxprs" ! Load the Xpress Optimizer
uses "mminsight" ! Load the Insight interface
! Input data - stored in Insight scenario
!@insight.manage input
public declarations
EvaluationDate: string
Cities: set of string
TrafficLevels: array(Cities,Cities) of real
end-declarations
! Result data - stored in Insight scenario
!@insight.manage result
public declarations
routetaken: array(Cities,Cities) of mpvar
end-declarations
! Data structures used internally in the model
!@insight.manage ignore
declarations
Distances: array(Cities,Cities) of real
end-declarations
! Procedure to build and solve optimization problem
procedure runproblem
! Download app attachment
if insightgetmode <> INSIGHT_MODE_NONE then
! Model is running within Insight, so download distances.dat from Insight server
insightgetappattach("distances.dat")
if insightattachstatus<>INSIGHT_ATTACH_OK then
writeln("Failed to download app attachment")
exit(1)
end-if
else
! Model is running outside of Insight, attachments are not accessible
! so copy from some other location
fcopy( '/Users/me/myfiles/distances.dat', 'distances.dat' )
end-if
! Load into model
initializations from "distances.dat"
Distances
end-initializations
! Perform optimization
buildproblem
optimizeproblem
! Capture results to file
ROUTES_FNAME := "result-"+EvaluationDate+".dat"
fopen(ROUTES_FNAME,F_OUTPUT)
forall (s in Cities, d in Cities | routetaken(s,d).sol=1)
writeln(s,"->",d)
fclose(F_OUTPUT)
! Store as scenario attachment
if insightgetmode <> INSIGHT_MODE_NONE then
! Model is running within Insight, so upload distances.dat to Insight server
insightputscenattach(ROUTES_FNAME)
if insightattachstatus<>INSIGHT_ATTACH_OK then
writeln("Failed to upload scenario attachment")
exit(1)
end-if
end-if
end-procedure
!@insight.execmodes.LOAD
public procedure doload
loaddata
end-procedure
!@insight.execmodes.RUN
public procedure dorun
insightpopulate
runproblem
end-procedure
!@insight.execmodes.NONE
public procedure doloadandrun
loaddata
runproblem
end-procedure
insightdispatch
end-model For clarity, the implementation of the loaddata, buildproblem and optimizeproblem procedures have been omitted from the example.
