fap_model_printer.h 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//
// Prints a model of Frequency Assignment Problem.
// Format: http://www.inra.fr/mia/T/schiex/Doc/CELAR.shtml#synt
//

#ifndef OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_
#define OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_

#include <map>
#include <string>
#include <vector>

#include "absl/strings/str_format.h"
#include "examples/cpp/fap_parser.h"

namespace operations_research {

// Prints the instance of the Frequency Assignment Problem.
class FapModelPrinter {
 public:
  FapModelPrinter(const std::map<int, FapVariable>& variables,
                  const std::vector<FapConstraint>& constraints,
                  const std::string& objective, const std::vector<int>& values);
  ~FapModelPrinter();

  void PrintFapObjective();
  void PrintFapVariables();
  void PrintFapConstraints();
  void PrintFapValues();

 private:
  const std::map<int, FapVariable> variables_;
  const std::vector<FapConstraint> constraints_;
  const std::string objective_;
  const std::vector<int> values_;
  DISALLOW_COPY_AND_ASSIGN(FapModelPrinter);
};

FapModelPrinter::FapModelPrinter(const std::map<int, FapVariable>& variables,
                                 const std::vector<FapConstraint>& constraints,
                                 const std::string& objective,
                                 const std::vector<int>& values)
    : variables_(variables),
      constraints_(constraints),
      objective_(objective),
      values_(values) {}

FapModelPrinter::~FapModelPrinter() {}

void FapModelPrinter::PrintFapVariables() {
  LOG(INFO) << "Variable File:";
  for (const auto& it : variables_) {
    std::string domain = "{";
    for (const int value : it.second.domain) {
      absl::StrAppendFormat(&domain, "%d ", value);
    }
    domain.append("}");

    std::string hard = " ";
    if (it.second.hard) {
      hard = " hard";
    }

    LOG(INFO) << "Variable " << absl::StrFormat("%3d: ", it.first)
              << absl::StrFormat("(degree: %2d) ", it.second.degree)
              << absl::StrFormat("%3d", it.second.domain_index)
              << absl::StrFormat("%3d", it.second.initial_position)
              << absl::StrFormat("%3d", it.second.mobility_index)
              << absl::StrFormat("%8d", it.second.mobility_cost)
              << absl::StrFormat(" (%2d) ", it.second.domain_size) << domain
              << hard;
  }
}

void FapModelPrinter::PrintFapConstraints() {
  LOG(INFO) << "Constraint File:";
  for (const FapConstraint& ct : constraints_) {
    std::string hard = " ";
    if (ct.hard) {
      hard = " hard";
    }

    LOG(INFO) << absl::StrFormat("%3d ", ct.variable1)
              << absl::StrFormat("%3d ", ct.variable2) << ct.type << " "
              << ct.operation << " " << absl::StrFormat("%3d", ct.value)
              << absl::StrFormat("%3d", ct.weight_index)
              << absl::StrFormat("%8d", ct.weight_cost) << hard;
  }
}

void FapModelPrinter::PrintFapObjective() {
  LOG(INFO) << "Objective: " << objective_;
}

void FapModelPrinter::PrintFapValues() {
  LOG(INFO) << absl::StrFormat("Values(%d): ", values_.size());
  std::string domain = " ";
  for (const int value : values_) {
    absl::StrAppendFormat(&domain, "%d ", value);
  }
  LOG(INFO) << domain;
}

}  // namespace operations_research
#endif  // OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_