Skip to content

Instantly share code, notes, and snippets.

@Eddy-Morgan
Created February 16, 2024 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eddy-Morgan/b3615cd6c5ce096bbc9334297a4b9bc4 to your computer and use it in GitHub Desktop.
Save Eddy-Morgan/b3615cd6c5ce096bbc9334297a4b9bc4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include "casadi/casadi.hpp"
using namespace std;
using namespace chrono;
using namespace casadi;
int main()
{
SX x = SX::sym("x",2,1);
SX A, b;
A = SX::vertcat({ SX::horzcat({4,2}), SX::horzcat({1,2}), SX::horzcat({1,1}) });
b = SX::vertcat({15, 8, 5});
// Cost and Constraints
SX f = SX::dot( x, SX::vertcat({-3,-2}) );
SX g = SX::mtimes( A, x ) - b;
SXDict nlp;
nlp["f"] = f;
nlp["g"] = g;
nlp["x"] = x;
// Constructing MI-NLP
std::vector<bool> discrete = {0,1};
casadi::Dict casadiOptions;
casadiOptions["discrete"] = discrete;
Function prob = nlpsol( "F", "bonmin", nlp, casadiOptions );
map<string, DM> res;
res = prob( DMDict{ {"lbx",DM({0,0})}, {"ubg", DM({0,0,0})} } );
cout << "xOpt: " << res.at("x") << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment