00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "cpp/print_dimacs_assignment.h"
00015
00016 #include <cstdio>
00017 #include <string>
00018
00019 #include "base/logging.h"
00020 #include "base/stringprintf.h"
00021 #include "graph/ebert_graph.h"
00022 #include "graph/linear_assignment.h"
00023
00024 namespace operations_research {
00025
00026 static void WriteOrDie(const char* buffer,
00027 size_t item_size,
00028 size_t buffer_length,
00029 FILE* fp) {
00030 size_t written = fwrite(buffer, item_size, buffer_length, fp);
00031 if (written != buffer_length) {
00032 fprintf(stderr, "Write failed.\n");
00033 exit(1);
00034 }
00035 }
00036
00037
00038 void PrintDimacsAssignmentProblem(
00039 const LinearSumAssignment<ForwardStarGraph>& assignment,
00040 const TailArrayManager<ForwardStarGraph>& tail_array_manager,
00041 const string& output_filename) {
00042 FILE* output = fopen(output_filename.c_str(), "w");
00043 const ForwardStarGraph& graph(assignment.Graph());
00044 string output_line = StringPrintf("p asn %d %d\n",
00045 graph.num_nodes(),
00046 graph.num_arcs());
00047 WriteOrDie(output_line.c_str(), 1, output_line.length(),
00048 output);
00049
00050 for (LinearSumAssignment<ForwardStarGraph>::BipartiteLeftNodeIterator
00051 node_it(assignment);
00052 node_it.Ok();
00053 node_it.Next()) {
00054 output_line = StringPrintf("n %d\n", node_it.Index() + 1);
00055 WriteOrDie(output_line.c_str(), 1, output_line.length(),
00056 output);
00057 }
00058
00059 tail_array_manager.BuildTailArrayFromAdjacencyListsIfForwardGraph();
00060
00061 for (ForwardStarGraph::ArcIterator arc_it(assignment.Graph());
00062 arc_it.Ok();
00063 arc_it.Next()) {
00064 ArcIndex arc = arc_it.Index();
00065 output_line = StringPrintf("a %d %d %lld\n",
00066 graph.Tail(arc) + 1,
00067 graph.Head(arc) + 1,
00068 assignment.ArcCost(arc));
00069 WriteOrDie(output_line.c_str(), 1, output_line.length(),
00070 output);
00071 }
00072 }
00073
00074 }