Skip to content

Commit

Permalink
Merge pull request #25 from conjure-cp/solutions-in-one-file
Browse files Browse the repository at this point in the history
Use --solutions-in-one-file
  • Loading branch information
ozgurakgun authored Mar 2, 2023
2 parents df7c51f + 004e107 commit 9daad21
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

on:
pull_request:
workflow_dispatch: # can be triggered manually
push:
branches:
- main # run for the main branch
pull_request: # and for PRs
# other branches that want testing must create a PR

jobs:
test:
Expand Down
8 changes: 4 additions & 4 deletions conjure/conjure.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def solve(self, args: str, code: str, shell_params: dict) -> dict:
else:
raise Exception("Variable {0} is not defined".format(p))
temp_params_file = conjurehelper.create_params_file(params)
shell_output = Popen(["conjure solve -ac " + temp_essence_file + " " + temp_params_file +
" --output-format=json --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE)
shell_output = Popen(["conjure solve " + temp_essence_file + " " + temp_params_file +
" --output-format=json --solutions-in-one-file --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE)
else:
shell_output = Popen(["conjure solve -ac " + temp_essence_file +
" --output-format=json --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE)
shell_output = Popen(["conjure solve " + temp_essence_file +
" --output-format=json --solutions-in-one-file --solver=chuffed " + args, ], shell=True, stdout=PIPE, stderr=PIPE)

_, error = shell_output.communicate()
if(error):
Expand Down
23 changes: 12 additions & 11 deletions conjure/conjurehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,19 @@ def create_params_file(self, params={}) -> str:
return self.create_temp_file("param.json", tempstr)

def read_solution_json_file(self) -> dict:
solutions = []
try:
if os.path.isdir('./conjure-output'):
files = sorted(os.listdir('./conjure-output'))
for f in files:
if f.endswith('.json'):
with open('./conjure-output/' + f) as file:
solutions.append(json.loads(file.read()))
except:
raise Exception('Error while reading json solution file(s).')

return {"conjure_solutions": solutions}
for p in os.listdir('conjure-output'):
if p.endswith('solutions.json'):
with open("conjure-output/" + p) as f:
filecontent = f.read()
# there is a bug in Conjure's latest release...
if filecontent.strip() == "]":
return {"conjure_solutions": []}
else:
with open("conjure-output/" + p) as f:
return {'conjure_solutions': json.load(f)}
except Exception as e:
raise Exception('Error while reading json solution file.')

def clean_tmp_files(self) -> None:
# remove conjure-output-folder
Expand Down
6 changes: 1 addition & 5 deletions conjure/conjuremagics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ def conjure(self, args, code):
try:
if code not in self.conjure_models: # we won't add code to models if the code is already there
self.conjure_models.append(code)
resultdict = conjure.solve(args, '\n'.join(
self.conjure_models), dict(self.shell.user_ns))
resultdict = conjure.solve(args, '\n'.join(self.conjure_models), dict(self.shell.user_ns))

except Exception as err:
self.conjure_models.pop()
print("{}: {}".format(type(err).__name__, err), file=sys.stderr)
return

# assign results to notebook environment
for key, value in resultdict.items():
self.shell.user_ns[key] = value
if len(resultdict['conjure_solutions']) == 1:
# assign results of single solution to notebook environment
for key, value in resultdict['conjure_solutions'][0].items():
Expand Down

0 comments on commit 9daad21

Please sign in to comment.