# How to Convert File Formats

## Start Converting

If you need to switch between file formats, we’ve got you covered.

### Why Change File Formats?

Octeract Engine can directly solve any problem with its model saved as a .nl, .mps and .lp file format. However, you might sometimes want to take advantage of the options a modelling language offers. What if you have a .nl file (we know this is unreadable to the human eye) that you want to read and make changes to on AMPL before solving? Say no more. You can now do so in a few steps.

### Which file formats are you working with?

First things first, let’s establish which file formats you’re interested in. Octeract Engine can read and write in a range of file formats. To summarise:

### Before Getting Started

Here are a few must-know commands for file transformation. Having these in your back pocket is as easy as copy and paste.

#### Command to import a file

#files allowed to be imported: .nl, .mps, .lp
Model.import_model_file()

#### Commands to write a particular file format

#write file to .mps file
Model.write_problem_to_MPS_file()

#write file to .nl file
Model.write_problem_to_NL_file()

#write file to .ale file
Model.write_problem_to_ALE_file()

#write file to .lp file
Model.write_problem_to_LP_file()

#write to .mod file
Model.write_problem_to_mod_file()
#write file to .gms file
Model.write_problem_to_GAMS_file()


### Converting on Windows

This tutorial uses the conversion of a .nl file to a .mod file format as an example. For all possible conversions, consult the table above. In this tutorial, we’ll be using Octeract’s Python interface. Now let’s begin the file transformation.

### Step 1: Start a Python Session

To begin, open a PowerShell session. To use the Python interface, type the word python in your session. Alternatively, go to any application that allows you to write in Python. For example, Jupyter notebook, text editor, etc.

python

### Step 2: Create an Empty Model

Copy and paste the snippet below to instantiate an empty model in your session.

from octeract import *
m = Model()


Using the command in the snippet below, open the file you want to transform into another. The table above shows which types of files the Octeract API can read. There is also an example in the code block of reading a .nl file. You’ll need to include the entire path to this file. For example, C:\Users\Octeract\Desktop\Examples\my_example.nl

m.import_model_file(r"my_example.nl") 

### Step 4: Write Problem into Another File Type

To convert your file format into another format the Octeract API can write in, copy and paste the snippet below. The example shows how to write into a .mod file format.

write_problem_type_to_mod_file(C:\Users\Octeract\Desktop\Examples\my_example.mod)

You can also see an example of the whole process in a Jupyter notebook below. If you run the four lines in the snippet, the .nl file will be saved as a .mod file. From here, you can manipulate and solve using AMPL (as long as you have an AMPL licence).

from octeract import *
m = Model()

m.import_model_file(r"C:\Users\Octeract\Desktop\Examples\my_example.nl")
m.write_problem_to_mod_file(r"C:\Users\Octeract\Desktop\Examples\my_example.mod")


### Converting on Linux

This tutorial uses the conversion of a .nl file to a .mod file format as an example. For all possible conversions, consult the table above. Now let’s begin the file transformation.

### Step 1: Start a Python Session

To begin, open a Shell session. To use the Python interface, type python3.7 in your session.

python3.7

### Step 2: Create an Empty Model

Copy and paste the snippet below to instantiate an empty model in your session.

from octeract import *
m = Model()


Using the command in the snippet below, open the file you want to transform into another. The table above shows which types of files the Octeract API can read. There is also an example in the code block below of reading a .nl file. You’ll need to include the entire path to this file. For example, C/Users/Octeract/Desktop/Examples/my_example.nl

m.import_model_file("my_example.nl") 

### Step 4: Write Problem into Another File Type

To convert your file format into another format the Octeract API can write in, copy and paste the snippet below. In the example, a model is written into a .mod file format.

write_problem_type_to_mod_file(C/Users/Octeract/Desktop/Examples/my_example.mod)

You can also see an example of the whole process written in a python 3.7 Shell session below. If you run the four lines in the snippet, the .nl example will be saved as a .mod example. From here, you can manipulate and solve using AMPL (as long as you have an AMPL licence).

from octeract import *
m = Model()

m.import_model_file("C/Users/Octeract/Desktop/Examples/my_example.nl")
m.write_problem_to_mod_file("C/Users/Octeract/Desktop/Examples/my_example.mod")