From 609c9c142b23f1ffcbed4d804280bc812ed873d7 Mon Sep 17 00:00:00 2001 From: Jeremy Kun Date: Thu, 9 Nov 2023 14:27:09 -0800 Subject: [PATCH] fixme: demonstrate solver integration works --- lib/Analysis/ReduceNoiseAnalysis/BUILD | 2 + .../ReduceNoiseAnalysis.cpp | 43 ++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/Analysis/ReduceNoiseAnalysis/BUILD b/lib/Analysis/ReduceNoiseAnalysis/BUILD index 8950234..90981d0 100644 --- a/lib/Analysis/ReduceNoiseAnalysis/BUILD +++ b/lib/Analysis/ReduceNoiseAnalysis/BUILD @@ -8,6 +8,8 @@ cc_library( hdrs = ["ReduceNoiseAnalysis.h"], deps = [ "//lib/Dialect/Noisy", + "@com_google_ortools//ortools/base", + "@com_google_ortools//ortools/linear_solver", "@llvm-project//llvm:Support", "@llvm-project//mlir:IR", ], diff --git a/lib/Analysis/ReduceNoiseAnalysis/ReduceNoiseAnalysis.cpp b/lib/Analysis/ReduceNoiseAnalysis/ReduceNoiseAnalysis.cpp index 35e572b..c0cc1c1 100644 --- a/lib/Analysis/ReduceNoiseAnalysis/ReduceNoiseAnalysis.cpp +++ b/lib/Analysis/ReduceNoiseAnalysis/ReduceNoiseAnalysis.cpp @@ -2,17 +2,46 @@ #include -#include "llvm/include/llvm/ADT/DenseMap.h" // from @llvm-project -#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project -#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project -#include "mlir/include/mlir/IR/Value.h" // from @llvm-project +#include "mlir/include/mlir/IR/Operation.h" +#include "mlir/include/mlir/IR/Value.h" +#include "ortools/linear_solver/linear_solver.h" +#include "llvm/include/llvm/ADT/DenseMap.h" +#include "llvm/include/llvm/ADT/TypeSwitch.h" + +using namespace operations_research; namespace mlir { namespace tutorial { ReduceNoiseAnalysis::ReduceNoiseAnalysis(Operation *op) { - // Implement analysis here + std::unique_ptr solver(MPSolver::CreateSolver("SCIP")); + + // Create the variables x and y. + MPVariable *const x = solver->MakeNumVar(0.0, 1, "x"); + MPVariable *const y = solver->MakeNumVar(0.0, 2, "y"); + + LOG(INFO) << "Number of variables = " << solver->NumVariables(); + + // Create a linear constraint, 0 <= x + y <= 2. + MPConstraint *const ct = solver->MakeRowConstraint(0.0, 2.0, "ct"); + ct->SetCoefficient(x, 1); + ct->SetCoefficient(y, 1); + + LOG(INFO) << "Number of constraints = " << solver->NumConstraints(); + + // Create the objective function, 3 * x + y. + MPObjective *const objective = solver->MutableObjective(); + objective->SetCoefficient(x, 3); + objective->SetCoefficient(y, 1); + objective->SetMaximization(); + + solver->Solve(); + + LOG(INFO) << "Solution:" << std::endl; + LOG(INFO) << "Objective value = " << objective->Value(); + LOG(INFO) << "x = " << x->solution_value(); + LOG(INFO) << "y = " << y->solution_value(); } -} // namespace tutorial -} // namespace mlir +} // namespace tutorial +} // namespace mlir