Skip to content

bramucas/clingo_python_basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clingo_python_basics

Basic stuff about clingo API

First

This is python3 only.

Installation of clingo

python -m pip install clingo

or, if you use conda

conda install -c potassco clingo

Small example (load_ground_solve.py)

# load_ground_solve.py

import sys
import clingo

# Reads the program given by command line
with open(sys.argv[1], "r") as f:
    ASP_program = f.read()

# Control object is a low-level interface for controlling the grounding/solving process.
ctl = clingo.Control(
    arguments=['-n', '0'],  # Here you can write the arguments you would pass to clingo by command line.
)

ctl.add("base", [], ASP_program)  # Adds the program to the control object.

ctl.ground([("base", [])])  # Grounding...

# Solving...
with ctl.solve(yield_=True) as solution_iterator:
        nanswer=1
        for model in solution_iterator:
            # Model is an instance of clingo.solving.Model class 
            # Reference: https://potassco.org/clingo/python-api/current/clingo/solving.html#clingo.solving.Model
            print(f'Answer {nanswer}')
            print(model)
            nanswer+=1

Usage

python load_ground_solve.py basic_prog.lp
python exploring_models.py basic_prog.lp

About

Basic stuff about clingo API

Topics

Resources

License

Stars

Watchers

Forks

Languages