Skip to content

Commit

Permalink
fixme: demonstrate solver integration works
Browse files Browse the repository at this point in the history
  • Loading branch information
j2kun committed Nov 9, 2023
1 parent f69a4a6 commit 609c9c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/Analysis/ReduceNoiseAnalysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
43 changes: 36 additions & 7 deletions lib/Analysis/ReduceNoiseAnalysis/ReduceNoiseAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,46 @@

#include <string>

#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<MPSolver> 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

0 comments on commit 609c9c1

Please sign in to comment.