Program Listing for File codes.h

Return to documentation for file (include/support/codes.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 <exception>
#include <gurobi_c++.h>
#include <string>

enum class ZEROStatus {
  NashEqNotFound,
  NashEqFound,
  Solved,
  NotSolved,
  TimeLimit,
  Numerical,
  Uninitialized
};


template <typename T> class Attr {
private:
  T Object;

public:
  Attr(T value) : Object{value} {};
  [[nodiscard]] T get() const { return Object; }
  void            set(const T &value) { Object = value; }
  // T &operator=(const T &a) { std::cerr << "Operation not allowed. Use set()";
  // } operator T() { std::cerr << "Operation not allowed. Use get()"; }
  Attr() = default;
};

class ZEROAlgorithmData {
public:
  Attr<double> DeviationTolerance{
        51e-4};
  Attr<double> TimeLimit{-1};
  Attr<int>    Threads{0};
  Attr<bool>   PureNashEquilibrium{false};
  Attr<unsigned long int> RandomSeed{42};
};

template <typename DataObjectType> struct ZEROStatistics {
  explicit ZEROStatistics(DataObjectType t) : AlgorithmData{t} {};
  Attr<ZEROStatus> Status         = ZEROStatus::Uninitialized;
  Attr<int>        NumVar         = {0};
  Attr<int>        NumConstraints = {0};
  Attr<int>        NumIterations  = {0};
  Attr<int>        NumNonZero     = {-1};
  Attr<bool> NumericalIssues = {false};
  Attr<double>   WallClockTime = {0};
  Attr<bool>     PureNashEquilibrium{false};
  DataObjectType AlgorithmData;
};

enum class ZEROErrorCode {
  MemoryError  = 100,
  InvalidQuery = 101,
  InvalidData  = 102,
  SolverError  = 103,
  OutOfRange = 104,
  Numeric    = 105,
  IOError    = 106,
  Assertion  = 107,
  Unknown    = 0
};

namespace std {
  std::string to_string(const ZEROErrorCode &code);
  std::string to_string(ZEROStatus st);
} // namespace std

class ZEROException : virtual public std::exception {
protected:
  ZEROErrorCode error_code;
  std::string   error_desc;
  std::string error_additional = "-";

public:
  explicit ZEROException(ZEROErrorCode code) : error_code(code) {
     this->error_desc = std::to_string(error_code);
  };
  explicit ZEROException(ZEROErrorCode code, const std::string &more)
        : error_code(code), error_additional(more) {
     this->error_desc = std::to_string(error_code);
  };
  explicit ZEROException(GRBException &e)
        : error_code(ZEROErrorCode::SolverError),
          error_additional(std::to_string(e.getErrorCode()) + e.getMessage()) {
     this->error_desc = std::to_string(error_code);
  };
  ~ZEROException() noexcept override = default;
  const char *          what() const noexcept override { return this->error_desc.c_str(); };
  virtual ZEROErrorCode which() const noexcept { return error_code; };
  const char *          more() const noexcept { return error_additional.c_str(); };
};

void ZEROAssert(bool        b,
                     const char *callerFn   = __builtin_FUNCTION(),
                     const char *callerFile = __builtin_FILE(),
                     const int   callerLine = __builtin_LINE());