Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
TA-20-D3TI18
Project
Overview
Details
Activity
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
Nesta Waldemar Binardo Tambunan
TA-20-D3TI18
Commits
0c26cdbf
Commit
0c26cdbf
authored
3 years ago
by
nesta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
111f0c51
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
0 deletions
+67
-0
csp.py
csp.py
+67
-0
db.sql
db.sql
+0
-0
generatejadwal.py
generatejadwal.py
+0
-0
No files found.
csp.py
0 → 100644
View file @
0c26cdbf
from
typing
import
Generic
,
TypeVar
,
Dict
,
List
,
Optional
from
abc
import
ABC
,
abstractmethod
V
=
TypeVar
(
'V'
)
# variable type
D
=
TypeVar
(
'D'
)
# domain type
# Base class for all constraints
class
Constraint
(
Generic
[
V
,
D
],
ABC
):
# The variables that the constraint is between
def
__init__
(
self
,
variables
:
List
[
V
])
->
None
:
self
.
variables
=
variables
@abstractmethod
def
satisfied
(
self
,
assignment
:
Dict
[
V
,
D
])
->
bool
:
...
# A constraint satisfaction problem consists of variables of type V
# that have ranges of values known as domains of type D and constraints
# that determine whether a particular variable's domain selection is valid
class
CSP
(
Generic
[
V
,
D
]):
def
__init__
(
self
,
variables
:
List
[
V
],
domains
:
Dict
[
V
,
List
[
D
]])
->
None
:
self
.
variables
:
List
[
V
]
=
variables
# variables to be constrained
self
.
domains
:
Dict
[
V
,
List
[
D
]]
=
domains
# domain of each variable
self
.
constraints
:
Dict
[
V
,
List
[
Constraint
[
V
,
D
]]]
=
{}
for
variable
in
self
.
variables
:
self
.
constraints
[
variable
]
=
[]
if
variable
not
in
self
.
domains
:
raise
LookupError
(
"Every variable should have a domain assigned to it."
)
def
add_constraint
(
self
,
constraint
:
Constraint
[
V
,
D
])
->
None
:
for
variable
in
constraint
.
variables
:
if
variable
not
in
self
.
variables
:
raise
LookupError
(
"Variable in constraint not in CSP"
)
else
:
self
.
constraints
[
variable
]
.
append
(
constraint
)
# Check if the value assignment is consistent by checking all constraints
# for the given variable against it
def
consistent
(
self
,
variable
:
V
,
assignment
:
Dict
[
V
,
D
])
->
bool
:
for
constraint
in
self
.
constraints
[
variable
]:
if
not
constraint
.
satisfied
(
assignment
):
return
False
return
True
def
backtracking_search
(
self
,
assignment
:
Dict
[
V
,
D
]
=
{})
->
Optional
[
Dict
[
V
,
D
]]:
# assignment is complete if every variable is assigned (our base case)
if
len
(
assignment
)
==
len
(
self
.
variables
):
return
assignment
# get all variables in the CSP but not in the assignment
unassigned
:
List
[
V
]
=
[
v
for
v
in
self
.
variables
if
v
not
in
assignment
]
# get the every possible domain value of the first unassigned variable
first
:
V
=
unassigned
[
0
]
for
value
in
self
.
domains
[
first
]:
local_assignment
=
assignment
.
copy
()
local_assignment
[
first
]
=
value
# if we're still consistent, we recurse (continue)
if
self
.
consistent
(
first
,
local_assignment
):
result
:
Optional
[
Dict
[
V
,
D
]]
=
self
.
backtracking_search
(
local_assignment
)
# if we didn't find the result, we will end up backtracking
if
result
is
not
None
:
return
result
return
None
\ No newline at end of file
This diff is collapsed.
Click to expand it.
db.sql
0 → 100644
View file @
0c26cdbf
This diff is collapsed.
Click to expand it.
generate
testterbaru
.py
→
generate
jadwal
.py
View file @
0c26cdbf
File moved
This diff is collapsed.
Click to expand it.
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