Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
twd-solver
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
adc2019
twd-solver
Commits
a1d0df36
Commit
a1d0df36
authored
Aug 22, 2019
by
tawada
Browse files
Options
Browse Files
Download
Plain Diff
merge adc2019-system
parents
d5529338
a4f61528
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
128 additions
and
9 deletions
+128
-9
.gitignore
.gitignore
+2
-0
solver.py
solver.py
+9
-9
twd_solver.py
twd_solver.py
+117
-0
No files found.
.gitignore
0 → 100644
View file @
a1d0df36
/*.txt
/__pycache__
solver.py
View file @
a1d0df36
# パッケージ
# パッケージ
#%matplotlib inline
#
%matplotlib inline
import
numpy
as
np
import
numpy
as
np
import
cv2
as
cv
# opencv
import
matplotlib
import
matplotlib.pyplot
as
plt
# matplotlibの描画系
from
matplotlib
import
cm
from
collections
import
OrderedDict
from
collections
import
OrderedDict
import
os
import
os
import
math
import
math
...
@@ -13,6 +9,11 @@ import re
...
@@ -13,6 +9,11 @@ import re
import
glob
import
glob
import
time
import
time
if
__name__
==
'__main__'
:
import
cv2
as
cv
# opencv
import
matplotlib
import
matplotlib.pyplot
as
plt
# matplotlibの描画系
from
matplotlib
import
cm
def
split
(
string
):
def
split
(
string
):
re_list
=
re
.
split
(
'[ X#@,()
\t\n
]'
,
string
)
re_list
=
re
.
split
(
'[ X#@,()
\t\n
]'
,
string
)
...
@@ -188,9 +189,8 @@ def show(index, read=None):
...
@@ -188,9 +189,8 @@ def show(index, read=None):
def
add
(
e
,
_dict
):
def
add
(
e
,
_dict
):
if
e
not
in
_dict
.
keys
():
if
e
not
in
_dict
.
keys
():
_dict
[
e
]
=
len
(
_dict
)
+
1
_dict
[
e
]
=
len
(
_dict
)
+
1
def
print_cnf
(
nodes
,
cnfs
):
def
print_cnf
(
nodes
,
cnfs
,
filename
=
'p.txt'
):
s
=
[]
s
=
[]
filename
=
'p.txt'
with
open
(
filename
,
'w'
)
as
file
:
with
open
(
filename
,
'w'
)
as
file
:
s
.
append
(
f
'p cnf {len(nodes)} {len(cnfs)}
\n
'
)
s
.
append
(
f
'p cnf {len(nodes)} {len(cnfs)}
\n
'
)
file
.
write
(
''
.
join
(
s
))
file
.
write
(
''
.
join
(
s
))
...
@@ -679,5 +679,5 @@ def main():
...
@@ -679,5 +679,5 @@ def main():
#img = QA2img(Q,A)
#img = QA2img(Q,A)
#plt.imshow(img)
#plt.imshow(img)
main
()
if
__name__
==
'__main__'
:
main
()
twd_solver.py
0 → 100644
View file @
a1d0df36
import
argparse
import
os
import
subprocess
if
__name__
==
'__main__'
:
import
solver
else
:
from
solvers.twd
import
solver
proc
=
None
stop_flag
=
False
def
stop
():
global
proc
global
stop_flag
stop_flag
=
True
if
not
proc
is
None
:
proc
.
terminate
()
print
(
'stopped'
)
def
solve
(
params
):
global
proc
global
stop_flag
stop_flag
=
False
# 必要な変数があるか確認
assert
(
'problem'
in
params
)
assert
(
'timeout'
in
params
)
problem
=
params
[
'problem'
]
timeout
=
params
[
'timeout'
]
basedir
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
))
problem_filepath
=
f
'{basedir}/tmp.txt'
with
open
(
problem_filepath
,
'w'
)
as
fp
:
fp
.
write
(
params
[
'problem'
])
if
not
stop_flag
:
Q
=
solver
.
readQ
(
problem_filepath
)
if
not
stop_flag
:
nodes
,
cnfs
=
solver
.
generate_sat
(
Q
)
cnf_filepath
=
f
'{basedir}/p.txt'
if
not
stop_flag
:
solver
.
print_cnf
(
nodes
,
cnfs
,
filename
=
cnf_filepath
)
returncode
=
0
if
not
stop_flag
:
solution_filepath
=
f
'{basedir}/a.txt'
cmds
=
f
'minisat {cnf_filepath} {solution_filepath}'
.
split
()
proc
=
subprocess
.
Popen
(
cmds
,
# stderr=subprocess.PIPE,
# stdout=subprocess.PIPE
)
try
:
# outs, errs = proc.communicate()
proc
.
communicate
()
status
=
'done'
except
Exception
as
ex
:
status
=
'failed'
finally
:
returncode
=
proc
.
returncode
# outs = outs.decode()
# errs = errs.decode()
# print(outs)
# print(errs)
solution
=
''
if
returncode
==
10
:
# 10 when SAT
status
=
'done'
if
not
stop_flag
:
A
=
solver
.
read_satA
(
solution_filepath
,
Q
,
nodes
)
if
not
stop_flag
:
solution
+=
f
'SIZE {A["w"]}X{A["h"]}
\n
'
for
r
in
A
[
'map'
]:
solution
+=
','
.
join
(
str
(
v
)
for
v
in
r
)
+
'
\n
'
for
bi
,
bv
in
A
[
'BLOCK'
]
.
items
():
solution
+=
f
'BLOCK#{bi} @({bv["x"]},{bv["y"]})
\n
'
solution
+=
'
\n
'
else
:
status
=
'failed'
return
{
'status'
:
status
,
'solution'
:
solution
}
def
main
(
args
):
with
open
(
args
[
'problem'
],
'r'
)
as
fp
:
problem
=
fp
.
read
()
params
=
{
'problem'
:
problem
,
'timeout'
:
args
[
'timeout'
]
}
ret
=
solve
(
params
)
print
(
ret
)
if
__name__
==
"__main__"
:
parser
=
argparse
.
ArgumentParser
(
description
=
'Wrapper Script for ADC2019 Solver'
)
parser
.
add_argument
(
'problem'
,
type
=
str
,
help
=
'File path to the problem file'
)
parser
.
add_argument
(
'-t'
,
'--timeout'
,
type
=
int
,
default
=
10
,
help
=
'Timeout for the process of the solver'
)
args
=
vars
(
parser
.
parse_args
())
main
(
args
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment