Skip to content

Commit

Permalink
Can now select whether to minimize or maximize objective.
Browse files Browse the repository at this point in the history
Can now specify in driver whether the objective function should be maximized or minimized.
  • Loading branch information
einar90 committed Jan 22, 2016
1 parent f6fd083 commit 50a23f2
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions FieldOpt/GTest/Utilities/driver/driver.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"Optimizer": {
"Type": "Compass",
"Mode": "Maximize",
"Parameters": {
"MaxEvaluations": 10,
"InitialStepLength": 50.0,
Expand Down
4 changes: 4 additions & 0 deletions FieldOpt/GTest/Utilities/settings/test_settings_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ TEST_F(OptimizerSettingsTest, Type) {
EXPECT_EQ(Optimizer::OptimizerType::Compass, settings_.optimizer()->type());
}

TEST_F(OptimizerSettingsTest, Mode) {
EXPECT_EQ(Optimizer::OptimizerMode::Maximize, settings_.optimizer()->mode());
}

TEST_F(OptimizerSettingsTest, Parameters) {
Optimizer::Parameters params = settings_.optimizer()->parameters();
EXPECT_EQ(10, params.max_evaluations);
Expand Down
21 changes: 17 additions & 4 deletions FieldOpt/Optimization/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,35 @@ Optimizer::Optimizer(Utilities::Settings::Optimizer *settings, Case *base_case,
case_handler_ = new CaseHandler(tentative_best_case_);
constraint_handler_ = new Constraints::ConstraintHandler(settings->constraints(), variables);
iteration_ = 0;
mode_ = settings->mode();
}

bool Optimizer::betterCaseFoundLastEvaluation()
{
foreach (Case* c, case_handler_->RecentlyEvaluatedCases()) {
if (c->objective_function_value() > tentative_best_case_->objective_function_value())
return true;
if (mode_ == Utilities::Settings::Optimizer::OptimizerMode::Maximize) {
if (c->objective_function_value() > tentative_best_case_->objective_function_value())
return true;
}
else if (mode_ == Utilities::Settings::Optimizer::OptimizerMode::Minimize) {
if (c->objective_function_value() < tentative_best_case_->objective_function_value())
return true;
}
}
return false;
}

void Optimizer::applyNewTentativeBestCase()
{
foreach (Case* c, case_handler_->RecentlyEvaluatedCases()) {
if (c->objective_function_value() > tentative_best_case_->objective_function_value())
tentative_best_case_ = c;
if (mode_ == Utilities::Settings::Optimizer::OptimizerMode::Maximize) {
if (c->objective_function_value() > tentative_best_case_->objective_function_value())
tentative_best_case_ = c;
}
else if (mode_ == Utilities::Settings::Optimizer::OptimizerMode::Minimize) {
if (c->objective_function_value() < tentative_best_case_->objective_function_value())
tentative_best_case_ = c;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions FieldOpt/Optimization/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class Optimizer
Constraints::ConstraintHandler *constraint_handler_; //!< All constraints defined for the optimization.
int max_evaluations_; //!< Maximum number of objective function evaluations allowed before terminating.
int iteration_; //!< The current iteration.
::Utilities::Settings::Optimizer::OptimizerMode mode_; //!< The optimization mode, i.e. whether the objective function should be maximized or minimized.
};

}
Expand Down
2 changes: 2 additions & 0 deletions FieldOpt/Utilities/settings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ The optimizer section contains optimizer specific settings and parameters. The r
```
"Optimizer": {
"Type": string,
"Mode": "Maximize",
"Parameters": { ... },
"Objective": { ... },
"Constraints": [ {...}, {...}, ... ]
}
```

* `Type` denotes which optmizer is to be used. Currently the only implemented algorithm is `Compass`.
* `Mode` specifies whether the objective function should be `Maximize`d or `Minimize`d.
* `Parameters` holds the optimization algorithm-specific parameters. The structure is shown below.
* `Objective` specifies the objective function to be used by the optimization algorithm. The structure is shown below.
* `Constraints` is an array of objects defining the constraints to be applied to the algorithm/variables. The structure of each constraint object is shown below.
Expand Down
10 changes: 10 additions & 0 deletions FieldOpt/Utilities/settings/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ Optimizer::Optimizer(QJsonObject json_optimizer)
type_ = OptimizerType::Compass;
else throw OptimizerTypeNotRecognizedException("The optimizer type " + type.toStdString() + " was not recognized.");

// Optimizer mode
if (json_optimizer.contains("Mode")) {
QString mode = json_optimizer["Mode"].toString();
if (QString::compare(mode, "Minimize", Qt::CaseInsensitive) == 0)
mode_ = OptimizerMode::Minimize;
else if (QString::compare(mode, "Maximize", Qt::CaseInsensitive) == 0)
mode_ = OptimizerMode::Maximize;
else throw UnableToParseOptimizerSectionException("Did not recognize optimizer Mode setting.");
} else throw UnableToParseOptimizerSectionException("Optimizer Mode keyword must be specified.");

// Optimizer parameters
try {
parameters_.max_evaluations = json_parameters["MaxEvaluations"].toInt();
Expand Down
4 changes: 3 additions & 1 deletion FieldOpt/Utilities/settings/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Optimizer

public:
enum OptimizerType { Compass };
enum OptimizerMode { Maximize, Minimize };
enum ConstraintType { BHP, Rate, SplinePoints };
enum ConstraintWellSplinePointsType { MaxMin, Function};
enum ObjectiveType { WeightedSum };
Expand Down Expand Up @@ -77,6 +78,7 @@ class Optimizer
};

OptimizerType type() const { return type_; } //!< Get the Optimizer type (e.g. Compass).
OptimizerMode mode() const { return mode_; } //!< Get the optimizer mode (maximize/minimize).
Parameters parameters() const { return parameters_; } //!< Get the optimizer parameters.
Objective objective() const { return objective_; } //!< Get the optimizer objective function.
QList<Constraint> *constraints() const { return constraints_; } //!< Get the optimizer constraints.
Expand All @@ -87,7 +89,7 @@ class Optimizer
Parameters parameters_;
Objective objective_;
QList<Constraint> *constraints_;

OptimizerMode mode_;
Constraint parseSingleConstraint(QJsonObject json_constraint);
};

Expand Down
1 change: 1 addition & 0 deletions examples/horzwell_bhp.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"Optimizer": {
"Type": "Compass",
"Mode": "Maximize",
"Parameters": {
"MaxEvaluations": 50,
"InitialStepLength": 100.0,
Expand Down

0 comments on commit 50a23f2

Please sign in to comment.