How to dump model protobuf ?

64 views
Skip to first unread message

Shesha Sreenivasamurthy

unread,
Mar 26, 2025, 10:04:59 PM3/26/25
to or-tools-discuss
Greetings,
I am using the following method to dump the protobuf. But it is give me all garbage. Can someone let me know how to dump it ?

static void DumpPresolvedModelToPbtxt(const CpModelProto& model, const char *filename) {
std::ofstream outfile(filename, std::ios::out);
if (!outfile.is_open()) {
std::cerr << "Failed to open file for writing: " << filename << std::endl;
return;
}
model.SerializeToOstream(&outfile);
std::cout << "Model has been dumped to " << filename << std::endl;
outfile.close();
}

CpModelBuilder model;

Add the constraints here.

DumpPresolvedModelToPbtxt(model.Build(), "presolved_model.pbtxt");

Thanks,
Shesha


Laurent Perron

unread,
Mar 26, 2025, 11:51:16 PM3/26/25
to or-tools-discuss
There is an export to file method in all languages. 

--Laurent


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/or-tools-discuss/be0a3260-c26e-4bc9-87eb-19bdb656a498n%40googlegroups.com.

Shesha Sreenivasamurthy

unread,
Mar 27, 2025, 2:36:10 AM3/27/25
to or-tools-discuss
#include <iostream>
#include <stdint.h>
#include <fstream>

#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

static void DumpPresolvedModelToPbtxt(const CpModelProto& model, const char *filename) {
// Open the output file using std::ofstream
std::ofstream output_file(filename);
// Check if the file was successfully opened
if (!output_file.is_open()) {
std::cerr << "Failed to open the output file for writing." << std::endl;
return;
}

// Wrap the std::ofstream with OstreamOutputStream
google::protobuf::io::OstreamOutputStream ostream_output_stream(&output_file);

// Serialize the message in human-readable text format and write to the file
if (!google::protobuf::TextFormat::Print(model, &ostream_output_stream)) {
std::cerr << "Failed to serialize message to text format." << std::endl;
return;
}

std::cout << "Message successfully serialized to '" << filename << "' in human-readable format." << std::endl;
}

CpModelBuilder model;
DumpPresolvedModelToPbtxt(model.Proto(), "presolved_model.pbtxt");

Shesha Sreenivasamurthy

unread,
Mar 27, 2025, 2:37:08 AM3/27/25
to or-tools-discuss
While making use -lortools -lprotobuf
Reply all
Reply to author
Forward
0 new messages