-
Notifications
You must be signed in to change notification settings - Fork 1
LP problem mask and QUBO Reformulation #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tubadzin
wants to merge
38
commits into
develop
Choose a base branch
from
lp-problem-mask
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
139e51c
feat: add LP / MIP Problems to backend, add google OR-Tools MIP Probl…
tubadzin 7e2e249
feat: add OR-Tools MIP wrapper using build-in cbc solver
tubadzin 93d038c
feat: add CPLEX converter from and to .lp and .mps files
tubadzin f789971
chore: add paths for OR-Tools solvers
tubadzin c50ef0f
refactor: remove unused statements
tubadzin fc91486
feat: add simple resource problem
tubadzin bab5da1
refactor: adapt problem description
tubadzin 011be8d
refactor: rename lp to mip.
tubadzin 9e8dbb3
feat: add converters
tubadzin 8cd7955
feat: add qubo gams translation
tubadzin e0b2442
feat: add lp testcase
tubadzin 757166b
feat: add qubo mip solver
tubadzin d29c947
feat: adapt gams parsing logic
cb0dec4
fix: undo hardcoded gams and python paths
tubadzin 10fc65c
chore: remove unused scripts
tubadzin 0d1d9ab
feat: adapt MIP solvers logic
tubadzin 654724f
refactor: remove whitelines
tubadzin 98a7d2f
fix: adapt input arguments
tubadzin a9ab795
feat: add testing for mip / lp solvers
tubadzin 6f91ee3
fix: adapt output parsing logic in gams mip solver
tubadzin 2d14e34
fix: repair input paths for qubo mip solver
33ae137
chore: remove unused files
539e8d0
chore: remove unused files
5ebd946
chore: adapt dependencies
0a435fd
feat: add cplex mip solver
a3bce39
chore: adapt properties for cplex solver
10e54a6
refactor: rename directory
a736af4
refactor: rename ortools occurances with cplex
1957b3c
refactor: add comments, expand explanations
71f5711
refactor: import checkstyle
4c53204
fix: adapt cplex logic to always write solution, not only in optimal …
d805bdd
refactor: line brakes
79e7652
refactor: add comment in gams mip solver
c54817e
chore: merge develop
tubadzin efb770f
fix: undo merge PythonProcessRunner change
tubadzin 9f4b020
feat: add mip cplex solver
tubadzin e126e75
refactor: unused imports, naming schemes
beb374b
feat: split translation functionality from solve functionality
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import argparse | ||
| import cplex | ||
| import sys | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="Solve a MIP problem from an MPS file using IBM CPLEX." | ||
| ) | ||
| parser.add_argument("input_file", help="Path to the input MPS file.") | ||
| parser.add_argument("output_file", help="Path to write the solution.") | ||
| args = parser.parse_args() | ||
|
|
||
| try: | ||
| cpx = cplex.Cplex() | ||
| cpx.read(args.input_file) | ||
| except cplex.exceptions.CplexError as exc: | ||
| print("Error reading the MPS file:", exc) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| try: | ||
| cpx.solve() | ||
| except cplex.exceptions.CplexError as exc: | ||
| print("Error during solve:", exc) | ||
| sys.exit(1) | ||
|
|
||
| status = cpx.solution.get_status() | ||
| with open(args.output_file, 'w') as out_file: | ||
| out_file.write("Solution status: {}\n".format(cpx.solution.get_status_string())) | ||
| out_file.write("Objective value = {}\n".format(cpx.solution.get_objective_value())) | ||
| var_names = cpx.variables.get_names() | ||
| var_values = cpx.solution.get_values() | ||
| for name, val in zip(var_names, var_values): | ||
| out_file.write("{:<15} = {}\n".format(name, val)) | ||
| print("Solution written to", args.output_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cplex |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2024 GAMS Software GmbH <[email protected]> | ||
| Copyright (c) 2024 GAMS Development Corp. <[email protected]> | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| $setEnv GAMSINP "%INPUT%" | ||
| $setEnv GAMSPEN "%PENALTY%" | ||
|
|
||
| $onEmbeddedCode Python: | ||
| import os | ||
| import sys | ||
| import subprocess | ||
| import re | ||
|
|
||
| inp_file = os.environ.get('GAMSINP') | ||
| print(f"{inp_file}") | ||
|
|
||
| if not os.path.exists(inp_file): | ||
| sys.exit(f"*** ERROR: input file not found: {inp_file}") | ||
|
|
||
| # Replace .lp or .mps with .gms | ||
| base, ext = os.path.splitext(inp_file) | ||
| new_name = base + '.gms' | ||
|
|
||
| cmd = ['mps2gms', inp_file] | ||
| print(f"--- Running: {' '.join(cmd)}") | ||
| res = subprocess.run( | ||
| cmd, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE | ||
| ) | ||
|
|
||
| with open(new_name, 'r', encoding='utf-8', errors='ignore') as f: | ||
| content = f.read() | ||
|
|
||
| pattern = re.compile( | ||
| r'Solve\s+(\S+)\s+using\s+(\S+)\s+(maximizing|minimizing)\s+(\S+)\s*;', | ||
| re.IGNORECASE | ||
| ) | ||
| match = pattern.search(content) | ||
| if not match: | ||
| sys.exit('*** ERROR: No "Solve ... using ..." statement found in generated .gms file') | ||
|
|
||
| model_name = match.group(1) # e.g. "m" | ||
| model_type = match.group(2) # e.g. "LP" or "MIP" | ||
| sense_text = match.group(3) # "maximizing" or "minimizing" | ||
| obj_var = match.group(4) # e.g. "x7" | ||
|
|
||
| if sense_text.lower().startswith('max'): | ||
| gams_sense = 'max' | ||
| else: | ||
| gams_sense = 'min' | ||
|
|
||
| # Create the replacement line using the matched groups | ||
| penalty = os.environ.get("GAMSPEN", "100") # Default to 100 if not set | ||
| new_line = f'$batinclude %IDIR% {model_name} {model_type} {gams_sense} {obj_var} {penalty} -logOn=2' | ||
|
|
||
| # Replace the matching "Solve ..." line with the new line (only the first occurrence) | ||
| new_content = pattern.sub(new_line, content, count=1) | ||
|
|
||
| solution_block = r''' | ||
| $setNames "%INPUT%" fp fn fe | ||
| $if not set SOLOUTPUT $set SOLOUTPUT %fp%solution%fe% | ||
| file solFile / "%SOLOUTPUT%" /; | ||
| put solFile 'c Solution of %INPUT%'; | ||
| put solFile / 's m '; | ||
|
|
||
| if((m.modelstat = %modelStat.Optimal% or m.modelstat = %modelStat.IntegerSolution%) or m.solvestat = %solveStat.NormalCompletion%, | ||
| put solFile / 'Optimal solution found.'; | ||
| put solFile / 'Objective value: ' obj.l:0:0; | ||
| put solFile / 'Variable values:'; | ||
| loop(j, | ||
| put solFile / j.tl:0 ' = ' xi.l(j):0:0; | ||
| ); | ||
| else | ||
| put solFile / 'Error: no solution was returned.'; | ||
| ); | ||
| ''' | ||
|
|
||
| # Append the solution parsing code at the end of the .gms file. | ||
| new_content += "\n" + solution_block | ||
| # Write the modified content back to the .gms file | ||
| with open(new_name, 'w', encoding='utf-8', errors='ignore') as f: | ||
| f.write(new_content) | ||
| $offEmbeddedCode | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mip_to_qubo_reformulation and qubo_solve should be named according to what they do. So creating gams representation of mip for the first and for the second gams to qubo formulation