Load model from txt file

382 views
Skip to first unread message

Alberto Manzini

unread,
Jan 13, 2024, 7:32:34 PM1/13/24
to or-tools-discuss
Hi everyone, I've saved my model in a txt file using method
model.ExportToFile(filename)

I don't understand how to load back my model from this file. I assume I have to load the proto message from file and setting it to a (empty) model, but I'm looking for the exact python commands to follow

Thanks for any help

Laurent Perron

unread,
Jan 14, 2024, 2:55:04 AM1/14/24
to or-tools...@googlegroups.com

There are no api. This is used mostly for debugging purposes.


--
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 on the web visit https://groups.google.com/d/msgid/or-tools-discuss/583adb61-1a9a-449f-ab56-44265d58454cn%40googlegroups.com.

watchdogs132

unread,
Jan 14, 2024, 7:29:13 AM1/14/24
to or-tools-discuss
from ortools.sat.python import cp_model
from google.protobuf import text_format

model = cp_model.CpModel()
with open("model.txt", "r") as file:
    text_format.Parse(file.read(), model.Proto())

Alberto Manzini

unread,
Jan 14, 2024, 11:34:54 AM1/14/24
to or-tools-discuss
Cool, many thanks!

Laurent Perron

unread,
Jan 14, 2024, 12:07:27 PM1/14/24
to or-tools...@googlegroups.com
The question is to recover solutions.

You can use model.Get{Int|Bool|Interval}VarFromProtoIndex to rebuild variables to use to recover solutions.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



Alberto Manzini

unread,
Jan 15, 2024, 2:30:51 PM1/15/24
to or-tools-discuss
Hi, I was looking for a method to export and load the model, not the solutions, to debug it
Thank you both

J. E. Marca

unread,
Feb 11, 2026, 1:11:26 PM (13 hours ago) Feb 11
to or-tools-discuss
I tried this advice and it didn't work for me.  

My solution is this:


```python
    from ortools.sat.python import cp_model_helper as cmh, cp_model

    test_file = "tests/data/dumpmodel_20260401_20260402_CpSolverStatus.INFEASIBLE.pbtxt"
    model = cp_model.CpModel()
    model_proto: cmh.CpModelProto = model.Proto()

    with open(test_file, "r") as file:
        model_proto.parse_text_format(str(file.read()))

        model_constraints = model_proto.constraints
        model_variables = model_proto.variables
        print(f"have {len(model_constraints)}  constraints")
        assert len(model_constraints) > 1
        print(f"have {len(model_variables)}  variables")
        assert len(model_variables) > 1
```

Laurent Perron

unread,
Feb 11, 2026, 2:23:19 PM (12 hours ago) Feb 11
to or-tools...@googlegroups.com
def clone(self) -> "CpModel":
"""Reset the model, and creates a new one from a CpModelProto instance."""
clone = CpModel()
clone.proto.copy_from(self.proto)
clone.rebuild_constant_map()
return clone
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


--
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.

J. E. Marca

unread,
Feb 11, 2026, 2:33:49 PM (11 hours ago) Feb 11
to or-tools-discuss
Ah yes, resetting the model is probably important if you want to actually use the model in a solver run.

In my case, I am not interested in re-running the model (it is known to be infeasible).  Instead, I am writing code to inspect it in order to offer the user more details on what is causing the infeasibility.

I just needed to load the pbtxt file in my testing regime order to run tests on my various constraint and variable inspection methods.

Regards,

James
Reply all
Reply to author
Forward
0 new messages