Files
ortools-clone/examples/cpp/fap_model_printer.h

119 lines
3.8 KiB
C
Raw Normal View History

2021-04-02 10:08:51 +02:00
// Copyright 2010-2021 Google LLC
2012-09-13 16:25:28 +00:00
// 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.
2016-10-05 14:08:27 +02:00
2012-09-13 16:25:28 +00:00
//
// 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>
2018-11-23 08:52:59 +01:00
#include <string>
2012-09-13 16:25:28 +00:00
#include <vector>
2018-11-28 11:32:45 +01:00
#include "absl/strings/str_format.h"
#include "examples/cpp/fap_parser.h"
2012-09-13 16:25:28 +00:00
namespace operations_research {
2016-10-05 14:08:27 +02:00
// Prints the instance of the Frequency Assignment Problem.
2012-09-13 16:25:28 +00:00
class FapModelPrinter {
2020-10-22 23:36:58 +02:00
public:
2020-10-29 14:25:39 +01:00
FapModelPrinter(const std::map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
const std::string& objective, const std::vector<int>& values);
2012-09-13 16:25:28 +00:00
~FapModelPrinter();
void PrintFapObjective();
void PrintFapVariables();
void PrintFapConstraints();
void PrintFapValues();
2020-10-22 23:36:58 +02:00
private:
2012-09-13 16:25:28 +00:00
const std::map<int, FapVariable> variables_;
const std::vector<FapConstraint> constraints_;
2016-10-05 14:08:27 +02:00
const std::string objective_;
2012-09-13 16:25:28 +00:00
const std::vector<int> values_;
DISALLOW_COPY_AND_ASSIGN(FapModelPrinter);
};
2020-10-29 14:25:39 +01:00
FapModelPrinter::FapModelPrinter(const std::map<int, FapVariable>& variables,
const std::vector<FapConstraint>& constraints,
const std::string& objective,
const std::vector<int>& values)
2020-10-22 23:36:58 +02:00
: variables_(variables),
constraints_(constraints),
objective_(objective),
2018-11-23 08:52:59 +01:00
values_(values) {}
FapModelPrinter::~FapModelPrinter() {}
void FapModelPrinter::PrintFapVariables() {
LOG(INFO) << "Variable File:";
2020-10-29 14:25:39 +01:00
for (const auto& it : variables_) {
2018-11-23 08:52:59 +01:00
std::string domain = "{";
for (const int value : it.second.domain) {
2018-11-28 11:32:45 +01:00
absl::StrAppendFormat(&domain, "%d ", value);
2018-11-23 08:52:59 +01:00
}
domain.append("}");
std::string hard = " ";
if (it.second.hard) {
hard = " hard";
}
2018-11-28 11:32:45 +01:00
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
2018-11-23 08:52:59 +01:00
<< hard;
}
}
void FapModelPrinter::PrintFapConstraints() {
LOG(INFO) << "Constraint File:";
2020-10-29 14:25:39 +01:00
for (const FapConstraint& ct : constraints_) {
2018-11-23 08:52:59 +01:00
std::string hard = " ";
if (ct.hard) {
hard = " hard";
}
2018-11-28 11:32:45 +01:00
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;
2018-11-23 08:52:59 +01:00
}
}
void FapModelPrinter::PrintFapObjective() {
LOG(INFO) << "Objective: " << objective_;
}
void FapModelPrinter::PrintFapValues() {
2018-11-28 11:32:45 +01:00
LOG(INFO) << absl::StrFormat("Values(%d): ", values_.size());
2018-11-23 08:52:59 +01:00
std::string domain = " ";
for (const int value : values_) {
2018-11-28 11:32:45 +01:00
absl::StrAppendFormat(&domain, "%d ", value);
2018-11-23 08:52:59 +01:00
}
LOG(INFO) << domain;
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research
#endif // OR_TOOLS_EXAMPLES_FAP_MODEL_PRINTER_H_