Skip to content

Commit

Permalink
Merge pull request #16 from neuroneural/optim
Browse files Browse the repository at this point in the history
add option to control optimization config
  • Loading branch information
sajadabvi authored Oct 11, 2023
2 parents 47a8fb3 + c7d922e commit 1b8f171
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
21 changes: 18 additions & 3 deletions gunfolds/solvers/clingo_rasl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
from __future__ import print_function
from string import Template
from gunfolds.utils.clingo import clingo
from gunfolds.utils.calc_procs import get_process_count
from gunfolds.conversions import g2clingo, rate, rasl_jclingo2g,\
drasl_jclingo2g, clingo_preamble,\
numbered_g2clingo, numbered_g2wclingo, encode_list_sccs

CLINGO_LIMIT = 64
PNUM = min(CLINGO_LIMIT, get_process_count(1))
CAPSIZE = 1

all_u_rasl_program = """
{edge(X,Y)} :- node(X), node(Y).
directed(X,Y,1) :- edge(X,Y).
Expand Down Expand Up @@ -312,8 +317,8 @@ def drasl_command(g_list, max_urate=0, weighted=False, scc=False, scc_members=No
return command


def drasl(glist, capsize, timeout=0, urate=0, weighted=False, scc=False, scc_members=None, dm=None,
bdm=None, pnum=None, edge_weights=(1, 1), configuration="tweety"):
def drasl(glist, capsize=CAPSIZE, timeout=0, urate=0, weighted=False, scc=False, scc_members=None, dm=None,
bdm=None, pnum=PNUM, edge_weights=(1, 1), configuration="crafty", optim='optN'):
"""
Compute all candidate causal time-scale graphs that could have
generated all undersampled graphs at all possible undersampling
Expand Down Expand Up @@ -371,6 +376,16 @@ def drasl(glist, capsize, timeout=0, urate=0, weighted=False, scc=False, scc_mem
trendy: Use defaults geared towards industrial problems
:type configuration: string
:param optim: a comma separated string containing configuration for optimization algorithm and optionally a bound
[<arg>[, <bound>]]
<arg>: <mode {opt|enum|optN|ignore}>[,<bound>...]
opt : Find optimal model
enum : Find models with costs <= <bound>
optN : Find optimum, then enumerate optimal models
ignore: Ignore optimize statements
<bound> : Set initial bound for objective function(s)
:type optim: string
:returns: results of parsed equivalent class
:rtype: dictionary
"""
Expand All @@ -383,7 +398,7 @@ def drasl(glist, capsize, timeout=0, urate=0, weighted=False, scc=False, scc_mem
return clingo(drasl_command(glist, max_urate=urate, weighted=weighted,
scc=scc, scc_members=scc_members, dm=dm, bdm=bdm, edge_weights=edge_weights),
capsize=capsize, convert=drasl_jclingo2g, configuration=configuration,
timeout=timeout, exact=not weighted, pnum=pnum)
timeout=timeout, exact=not weighted, pnum=pnum, optim=optim)


def rasl(g, capsize, timeout=0, urate=0, pnum=None, configuration="tweety"):
Expand Down
59 changes: 38 additions & 21 deletions gunfolds/utils/clingo.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
""" This module contains clingo interaction functions """
from __future__ import print_function
from gunfolds.conversions import msl_jclingo2g
from gunfolds.conversions import drasl_jclingo2g
import clingo as clngo
import json
from gunfolds.utils.calc_procs import get_process_count

CAPSIZE = 1000
CLINGO_LIMIT = 64
PNUM = min(CLINGO_LIMIT, get_process_count(1))

CAPSIZE = 1

def run_clingo(command,
exact=True,
timeout=0,
capsize=CAPSIZE,
configuration="tweety",
pnum=None):
pnum=PNUM,
optim='optN'):
"""
Open sub-process and run clingo
Expand All @@ -41,26 +40,35 @@ def run_clingo(command,
trendy: Use defaults geared towards industrial problems
:type configuration: string
:param cpath: clingo path
:type cpath: string
:param pnum: number of parallel threads to run clingo on
:type pnum: integer
:param optim: a comma separated string containing configuration for optimization algorithm and optionally a bound
[<arg>[, <bound>]]
<arg>: <mode {opt|enum|optN|ignore}>[,<bound>...]
opt : Find optimal model
enum : Find models with costs <= <bound>
optN : Find optimum, then enumerate optimal models
ignore: Ignore optimize statements
<bound> : Set initial bound for objective function(s)
:type optim: string
:returns: results of equivalent class
:rtype: dictionary
"""
if pnum is None:
pnum = PNUM
ctrl = clngo.Control(["--warn=no-atom-undefined","--configuration=", configuration, "-t", str(int(pnum)) + ",split", "-n", str(capsize)])
assert len(optim.split(',')) < 3, "optim option only takes 1 or 2 comma-separated parameters"

clingo_control = ["--warn=no-atom-undefined", "--configuration=", configuration, "-t", str(int(pnum)) + ",split", "-n", str(capsize)]
ctrl = clngo.Control(clingo_control)
if not exact:
ctrl.configuration.solve.opt_mode = "opt"
ctrl.configuration.solve.opt_mode = optim
ctrl.add("base", [], command.decode())
ctrl.ground([("base", [])])
models = []
with ctrl.solve(yield_=True, async_=True) as handle:
for model in handle:
models.append([str(atom) for atom in model.symbols(shown=True)])
models.append(([str(atom) for atom in model.symbols(shown=True)], model.cost))
cost = ctrl.statistics["summary"]["costs"]
num_opt = ctrl.statistics["summary"]["models"]["optimal"]
if not exact:
Expand All @@ -72,11 +80,12 @@ def run_clingo(command,


def clingo(command, exact=True,
convert=msl_jclingo2g,
convert=drasl_jclingo2g,
timeout=0,
capsize=CAPSIZE,
configuration="tweety",
pnum=None):
configuration="crafty",
optim='optN',
pnum=PNUM):
"""
Runs ``run_clingo`` and returns parsed equivalent class
Expand Down Expand Up @@ -105,8 +114,15 @@ def clingo(command, exact=True,
trendy: Use defaults geared towards industrial problems
:type configuration: string
:param cpath: clingo path
:type cpath: string
:param optim: a comma separated string containing configuration for optimization algorithm and optionally a bound
[<arg>[, <bound>]]
<arg>: <mode {opt|enum|optN|ignore}>[,<bound>...]
opt : Find optimal model
enum : Find models with costs <= <bound>
optN : Find optimum, then enumerate optimal models
ignore: Ignore optimize statements
<bound> : Set initial bound for objective function(s)
:type optim: string
:param pnum: number of parallel threads to run clingo on
:type pnum: integer
Expand All @@ -119,12 +135,13 @@ def clingo(command, exact=True,
timeout=timeout,
capsize=capsize,
configuration=configuration,
pnum=pnum)
if result[0]=={} or result[0]==[]:
pnum=pnum,
optim=optim)
if result[0] == {} or result[0] == []:
return {}
else:
if not exact:
r = (convert(result[0][-1]), result[1])
r = {(convert(value[0]), value[1][0]) for value in result[0]}
else:
r = {convert(value) for value in result[0]}
r = {convert(value[0]) for value in result[0]}
return r

0 comments on commit 1b8f171

Please sign in to comment.