Program Listing for File nash.h

Return to documentation for file (include/games/nash.h)

/* #############################################
 *             This file is part of
 *                    ZERO
 *
 *             Copyright (c) 2020
 *     Released under the Creative Commons
 *         CC BY-NC-SA 4.0 License
 *
 *              Find out more at
 *        https://github.com/ds4dm/ZERO
 * #############################################*/


#pragma once
#include "zero.h"
#include <armadillo>
#include <gurobi_c++.h>
#include <iostream>
#include <memory>
#include <set>
#include <string>

namespace Game {
  class NashGame {
  private:
     GRBEnv *       Env = nullptr;
     arma::sp_mat   LeaderConstraints;
     arma::vec      LeaderConstraintsRHS;
     unsigned int   NumPlayers;
     VariableBounds Bounds;
     std::vector<std::shared_ptr<MathOpt::MP_Param>> Players;
     arma::sp_mat                                    MarketClearing;
     arma::vec MCRHS;

     std::vector<unsigned int> PrimalPosition;

     std::vector<unsigned int> DualPosition;
     unsigned int MC_DualPosition{};
     unsigned int LeaderPosition{};
     unsigned int numLeaderVar;

     void setPositions();

  public: // Constructors
     explicit NashGame(GRBEnv *e) noexcept : Env{e} {};

     explicit NashGame(GRBEnv *                                               e,
                             const std::vector<std::shared_ptr<MathOpt::MP_Param>> &players,
                             arma::sp_mat                                           MC,
                             arma::vec                                              MCRHS,
                             unsigned int                                           nLeadVar = 0,
                             arma::sp_mat                                           leadA    = {},
                             arma::vec                                              leadRHS  = {});

     NashGame(const NashGame &N);

     ~NashGame() = default;

     // Verbose declaration
     friend std::ostream &operator<<(std::ostream &os, const NashGame &N) {
        os << '\n';
        os << "--------------------------------------------------------------------"
                "---"
            << '\n';
        os << "Nash Game with " << N.NumPlayers << " players" << '\n';
        os << "--------------------------------------------------------------------"
                "---"
            << '\n';
        os << "Number of primal variables:\t\t\t " << N.getNprimals() << '\n';
        os << "Number of dual variables:\t\t\t " << N.getNumDualVars() << '\n';
        os << "Number of shadow price dual variables:\t\t " << N.getNumShadow() << '\n';
        os << "Number of leader variables:\t\t\t " << N.getNumLeaderVars() << '\n';
        os << "--------------------------------------------------------------------"
                "---"
            << '\n';
        return os;
     }

     inline unsigned int getNprimals() const {
        return this->PrimalPosition.back();
     }
     inline unsigned int getNumShadow() const {
        return this->MCRHS.n_rows;
     }
     inline unsigned int getNumLeaderVars() const {
        return this->numLeaderVar;
     }
     inline unsigned int getNumDualVars() const {
        return this->DualPosition.back() - this->DualPosition.front() + 0;
     }

     inline unsigned int getPrimalLoc(unsigned int i = 0) const {
        return PrimalPosition.at(i);
     }

     inline unsigned int getMCDualLoc() const {
        return MC_DualPosition;
     }

     inline unsigned int getLeaderLoc() const {
        return LeaderPosition;
     }

     inline unsigned int getDualLoc(unsigned int i = 0) const {
        return DualPosition.at(i);
     }

     // Members
     const NashGame &formulateLCP(arma::sp_mat &     M,
                                            arma::vec &        q,
                                            perps &            Compl,
                                            VariableBounds &   Bounds,
                                            bool               writeToFile = false,
                                            const std::string &M_name      = "dat/LCP.txt",
                                            const std::string &q_name      = "dat/q.txt") const;

     arma::sp_mat rewriteLeadCons() const;

     inline arma::vec getLeadRHS() const { return this->LeaderConstraintsRHS; }

     inline arma::vec getMCLeadRHS() const {
        return arma::join_cols(arma::join_cols(this->LeaderConstraintsRHS, this->MCRHS),
                                      -this->MCRHS);
     }

     // Check solution and correctness
     std::unique_ptr<GRBModel>
     respond(unsigned int player, const arma::vec &x, bool fullvec = true) const;

     double
     respondSol(arma::vec &sol, unsigned int player, const arma::vec &x, bool fullvec = true) const;

     arma::vec computeQPObjectiveValues(const arma::vec &x, bool checkFeas = false) const;

     bool isSolved(const arma::vec &sol,
                        unsigned int &   violPlayer,
                        arma::vec &      violSol,
                        double           tol = 1e-4) const;

     //  Modify NashGame members
     NashGame &addDummy(unsigned int par = 0, int position = -1);

     NashGame &addLeadCons(const arma::vec &a, double b);

     // Read/Write Nashgame functions
     void write(const std::string &filename, bool append = true, bool KKT = false) const;

     void save(const std::string &filename, bool erase = true) const;

     long int load(const std::string &filename, long int pos = 0);
  };


} // namespace Game