#include <Opt.h>
Inheritance diagram for Opt:
Public Member Functions | |
Opt (ThreadManager *MTHREAD_h, Agent_base *AGENT_h) | |
Constructor. | |
~Opt () | |
void | test () |
standard test from glpk manual. Used to see if everything is installed correctly. | |
void | update () |
this is a "full" update, the matrix is completelly re-wroted | |
void | updateResourceValues () |
update the resource values (availabilities) according to individual agent assets | |
void | debug (string debugMessage="", bool useCallCounter=false) |
print for debugging reasons the current version of the matrix | |
void | solve () |
call glpk to solve the matrix and store the result whitin the OPT object | |
void | saveToCache () |
save the current level of z and optimal quantities to a temp cache | |
void | restoreFromCache () |
restore from the temp cache z and the optimal quantities | |
void | addPixel (const Pixel *px_h) |
add a pixel. The OPT matrix is updated without the need of a full rewriting | |
void | removeLastAddedPixel () |
remove the latest added pixel. The OPT matrix doesn't require a complete recalculation | |
vector< matrixActivities * > | getActs () |
double | getZ () |
double | getStat () |
double | getActivityObjValue (string name_h) |
return the output or error code from the latest solve() call Return the MIP obj value. | |
Private Member Functions | |
void | updateActivities () |
update the activities according to individual agent assets | |
void | updateResourceNames () |
update the resource names according to individual agent assets | |
void | solveGlpk () |
solve the problem using glpk library | |
Private Attributes | |
vector< matrixActivities > | ACTS |
vector of matrixActivities | |
Agent_base * | AGENT |
pointer to the owner of the object istances | |
vector< string > | resourceNames |
name of the resources | |
vector< double > | resourceValues |
value of the resources (availabilities) | |
int | stat |
last output/error value | |
int | cachedStat |
double | z |
last value reached by the target function | |
double | cachedZ |
cashed valued of last value reached by the target function | |
int | actsAddedOnLastPixel |
number of activities added when the last pixel was added |
Agents can decide trought comportamental functions or trought a optimization routine. In the latter case, this is their personal matrix..
The matrix is built first setting the resources and then the activities.
RESOURCES: First the input fixed resources are added. Then each plot owned by the farmer is appended in the resources list (rows).
ACTIVITIES: First are added the non-spacial explicit activities. Then for each spacially-explicit activity it is looped over all the available plots to search for those: a)allowing such activity; b)that have the required object (if needed);
Following activities, for each non-spacial investment a "buy" activity for each of then is appended. Finally, similar with spacial activities, for each spatial investment it is looped over each plot to search plots available for that specific investment (plots: a) with compatible soil type; b) without already such investment object)
Definition at line 47 of file Opt.h.
Opt | ( | ThreadManager * | MTHREAD_h, | |
Agent_base * | AGENT_h | |||
) |
void test | ( | ) |
standard test from glpk manual. Used to see if everything is installed correctly.
Definition at line 919 of file Opt.cpp.
Referenced by addPixel(), and updateActivities().
Here is the caller graph for this function:
void update | ( | ) |
this is a "full" update, the matrix is completelly re-wroted
update() can be called at the initialization or later on. It completelly re-wrote the OPT objects (activities, resourceNames and resourceValues) collecting the data from the current situation of the agent.
Definition at line 52 of file Opt.cpp.
Referenced by Agent_farmer::initMIP(), Agent_farmer::produce(), and Agent_farmer::update().
Here is the caller graph for this function:
void updateResourceValues | ( | ) |
update the resource values (availabilities) according to individual agent assets
Init or completelly re-write the level of the resources available to the agent in the MIP.
Resource can be of four different types according on their SOURCE:
At the end of the "normal" resources, each pixel owned/rented by the agent is added on the list with value "1"
Definition at line 309 of file Opt.cpp.
Referenced by Agent_base::pay(), and update().
Here is the caller graph for this function:
void debug | ( | string | debugMessage = "" , |
|
bool | useCallCounter = false | |||
) |
print for debugging reasons the current version of the matrix
Definition at line 425 of file Opt.cpp.
Referenced by Agent_base::debugOptProblem().
Here is the caller graph for this function:
void solve | ( | ) |
call glpk to solve the matrix and store the result whitin the OPT object
The function call the underlying solver library (currently glpk) to "solve" the current state of the OPT problem.
The value of the objective function is stored on OPT.z, the optimal quantities are stored on the individual OPT.ACTS.at(i).q variables and the solver exit code on OPT.stat.
Definition at line 488 of file Opt.cpp.
Referenced by Agent_farmer::getShadowPrice(), Agent_farmer::initMIP(), Agent_farmer::produce(), and Agent_farmer::test().
Here is the caller graph for this function:
void saveToCache | ( | ) |
save the current level of z and optimal quantities to a temp cache
This function save to a temporary cache the value of the target function reached by the last call to solve() and the optimal quantities od ACTS.
It is used when planning solve() is needed for e.g. calculate the shadow price of plots.
Definition at line 666 of file Opt.cpp.
Referenced by Agent_farmer::getShadowPrice().
Here is the caller graph for this function:
void restoreFromCache | ( | ) |
restore from the temp cache z and the optimal quantities
This function retrieve from the temporary cache the value of the target function and the optimal quantities od ACTS.
It is used when planning solve() is needed for e.g. calculate the shadow price of plots to reset the OPT object to its original conditions.
Definition at line 681 of file Opt.cpp.
Referenced by Agent_farmer::getShadowPrice().
Here is the caller graph for this function:
void addPixel | ( | const Pixel * | px_h | ) |
add a pixel. The OPT matrix is updated without the need of a full rewriting
The function upgrade the OPT object when the agent has availability of a new plot.
The nice thing is that it does that without re-writing the whole matrix, but just adding the relevant activities and resources. And even more nicely, it keeps track of that using the actsAddedOnLastPixel counter, so we can later on remove the last added pixel using Opt::removeLastAddedPixel() ;-)
This function is mainly used to calculate the shadow price of a new plot.
Definition at line 698 of file Opt.cpp.
Referenced by Agent_space::addPlotToMIP(), and Agent_farmer::getShadowPrice().
Here is the caller graph for this function:
void removeLastAddedPixel | ( | ) |
remove the latest added pixel. The OPT matrix doesn't require a complete recalculation
if we just added a pixel with addPixel end we want remove that (e.g. because we was just calculating a shadow price, without a "real" addiction of the pixel to the agent), we call this function to reset the OPT object to their previous condition.
This function uses actsAddedOnLastPixel counter to do its job, and it doesn't require a whole re-writing of the OPT object.
Definition at line 887 of file Opt.cpp.
Referenced by Agent_farmer::getShadowPrice().
Here is the caller graph for this function:
vector< matrixActivities * > getActs | ( | ) |
Definition at line 910 of file Opt.cpp.
Referenced by Agent_base::answerStats(), and Agent_farmer::collectProductionEffects().
Here is the caller graph for this function:
double getZ | ( | ) | [inline] |
Definition at line 61 of file Opt.h.
Referenced by Agent_farmer::collectProductionEffects(), Agent_farmer::getShadowPrice(), and Agent_base::getZ().
Here is the caller graph for this function:
double getActivityObjValue | ( | string | name_h | ) |
return the output or error code from the latest solve() call Return the MIP obj value.
name_h | *FULL* name of the activity (e.g. dWheat_on_plot_1234) |
Definition at line 980 of file Opt.cpp.
Referenced by Agent_farmer::leaveActivity().
Here is the caller graph for this function:
void updateActivities | ( | ) | [private] |
update the activities according to individual agent assets
Init or completelly re-write the activity objects of OPT.
The function loops over the activity moulds and, if the current one is spatially explicit, loops over the agent owned and rented plots to create a plot-dedicated activity (e.g. durumWheat_on_plot_7321).
Before beeing added to the activity vector, each activity is filtered by the agent.
Definition at line 98 of file Opt.cpp.
Referenced by update().
Here is the caller graph for this function:
void updateResourceNames | ( | ) | [private] |
update the resource names according to individual agent assets
Init or completelly re-write the resource names of OPT.
The function loops over the resource moulds and, if the current one is spatially explicit, loops over the agent owned and rented plots to create a plot-dedicated resource name (e.g. plot_7321).
Definition at line 275 of file Opt.cpp.
Referenced by update().
Here is the caller graph for this function:
void solveGlpk | ( | ) | [private] |
solve the problem using glpk library
Initialise the problem, solve it and load the results using the glpk library
Function updated on 2009.07.01 for multithreading (previously it was splitted in several parts for optimisation)
Definition at line 502 of file Opt.cpp.
Referenced by solve().
Here is the caller graph for this function:
vector<matrixActivities> ACTS [private] |
vector of matrixActivities
Definition at line 71 of file Opt.h.
Referenced by addPixel(), debug(), getActivityObjValue(), getActs(), removeLastAddedPixel(), restoreFromCache(), saveToCache(), solveGlpk(), and updateActivities().
Agent_base* AGENT [private] |
pointer to the owner of the object istances
Definition at line 72 of file Opt.h.
Referenced by addPixel(), debug(), Opt(), solveGlpk(), updateActivities(), updateResourceNames(), and updateResourceValues().
vector<string> resourceNames [private] |
name of the resources
Definition at line 73 of file Opt.h.
Referenced by addPixel(), debug(), removeLastAddedPixel(), solveGlpk(), updateResourceNames(), and updateResourceValues().
vector<double> resourceValues [private] |
value of the resources (availabilities)
Definition at line 74 of file Opt.h.
Referenced by addPixel(), debug(), removeLastAddedPixel(), solveGlpk(), and updateResourceValues().
int stat [private] |
last output/error value
Definition at line 75 of file Opt.h.
Referenced by debug(), getStat(), Opt(), restoreFromCache(), saveToCache(), and solveGlpk().
int cachedStat [private] |
double z [private] |
last value reached by the target function
Definition at line 77 of file Opt.h.
Referenced by addPixel(), debug(), getZ(), Opt(), restoreFromCache(), saveToCache(), solveGlpk(), test(), and updateActivities().
double cachedZ [private] |
cashed valued of last value reached by the target function
Definition at line 78 of file Opt.h.
Referenced by restoreFromCache(), and saveToCache().
int actsAddedOnLastPixel [private] |
number of activities added when the last pixel was added
Definition at line 79 of file Opt.h.
Referenced by addPixel(), and removeLastAddedPixel().