From 8d8ed80c50f86c972790693c8cf61db88999eadd Mon Sep 17 00:00:00 2001 From: Geir Horn Date: Sat, 9 Mar 2024 13:28:39 +0100 Subject: [PATCH] Flag for testing available model before allowing solution and explicit checking JSON keys for metric updates Change-Id: Ib2bc4b5273159077e3eaa7786cc2c846110791d0 --- AMPLSolver.cpp | 10 ++++++++++ AMPLSolver.hpp | 10 ++++++++-- MetricUpdater.cpp | 4 ++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/AMPLSolver.cpp b/AMPLSolver.cpp index 30ad9b6..928d318 100644 --- a/AMPLSolver.cpp +++ b/AMPLSolver.cpp @@ -195,6 +195,11 @@ void AMPLSolver::DefineProblem(const Solver::OptimisationProblem & TheProblem, SetAMPLParameter( ConstantName, ConstantRecord.at( InitialConstantValue ) ); } + + // Finally, the problem has been defined and the flag is set to allow + // the search for solutions for this problem. + + ProblemUndefined = false; } // ----------------------------------------------------------------------------- @@ -225,6 +230,10 @@ void AMPLSolver::DataFileUpdate( const DataFileMessage & TheDataFile, void AMPLSolver::SolveProblem( const ApplicationExecutionContext & TheContext, const Address TheRequester ) { + // There is nothing to do if the application model is missing. + + if( ProblemUndefined ) return; + // Setting the metric values one by one. In the setting of NebulOuS a metric // is either a numerical value or a string. Vectors are currently not // supported as values. @@ -356,6 +365,7 @@ AMPLSolver::AMPLSolver( const std::string & TheActorName, Solver( Actor::GetAddress().AsString() ), ProblemFileDirectory( ProblemPath ), ProblemDefinition( InstallationDirectory ), + ProblemUndefined( true ), DefaultObjectiveFunction(), VariablesToConstants() { RegisterHandler( this, &LSolver::DataFileUpdate ); diff --git a/AMPLSolver.hpp b/AMPLSolver.hpp index 94de928..6efaf4f 100644 --- a/AMPLSolver.hpp +++ b/AMPLSolver.hpp @@ -153,6 +153,14 @@ protected: VariableName = "Variable", InitialConstantValue = "Value"; + // Finally, no solution will be produced unless the problem has been + // defined. A flag is therefore set by the message handler indicating + // that the problem has been defined. + +private: + + bool ProblemUndefined; + // The AMPL problem file can contain many objective functions, but can be // solved only for one objective function at the time. The name of the // default objective function is therefore stored together with the model @@ -160,8 +168,6 @@ protected: // label is not provided with the optimisation problem message, an // invalid argument exception will be thrown. -private: - std::string DefaultObjectiveFunction; // To set the constant values to the right variable values, the mapping diff --git a/MetricUpdater.cpp b/MetricUpdater.cpp index cefda49..1d6ccf8 100644 --- a/MetricUpdater.cpp +++ b/MetricUpdater.cpp @@ -142,10 +142,10 @@ void MetricUpdater::UpdateMetricValue( if( MetricValues.contains( TheTopic ) ) { - MetricValues.at( TheTopic ) = TheMetricValue[ NebulOuS::ValueLabel ]; + MetricValues.at( TheTopic ) = TheMetricValue.at( NebulOuS::ValueLabel ); ValidityTime = std::max( ValidityTime, - TheMetricValue[ NebulOuS::TimePoint ].get< Solver::TimePointType >() ); + TheMetricValue.at( NebulOuS::TimePoint ).get< Solver::TimePointType >() ); } }