推理编程
Read about the difference between declarative and imperative programming and learn from code examples (Answer Set Programming, Python and C).
了解声明式和命令式编程之间的区别,并从代码示例(答案集编程,Python和C)中学习。
介绍 (Introduction)
The amount of computational problems seems to be unlimited in both industry and science. There is a huge demand for new insights from the vast amount of available data. To obtain this knowledge, dedicated people use all kinds of programming languages for designing and implementing algorithms.
在工业和科学领域,计算问题的数量似乎都是无限的。 从大量可用数据中获得对新见解的巨大需求。 为了获得这些知识,敬业的人们使用各种编程语言来设计和实现算法。
However, many of the current real-world problems are complex (combinatorial) search problems with which we try to automate and optimize processes as well as support people in their decisions. This does not involve encoding of algorithms but encoding given knowledge. In other words, given knowledge about all the rules and constraints to be considered to what is counted as a solution. Instead of writing statements describing the control flow of a computation, declarative programming expresses its logic. What do we want to achieve, not statements about how we can achieve it.
但是,当前许多现实世界中的问题都是复杂的(组合)搜索问题,我们试图通过这些问题来自动化和优化流程,并支持人们的决策。 这不涉及算法的编码,而是编码给定的知识 。 换句话说,已知关于所有规则和约束的知识,可以考虑将其视为解决方案。 声明性编程不是编写描述计算控制流程的语句,而是表达其逻辑。 我们想要实现什么 ,而不是关于如何实现的声明。
This is called declarative programming.
这称为声明式编程。
为什么要进行声明式编程? (Why declarative programming?)
Using a promising declarative programming paradigm, namely Answer Set Programming (ASP, sometimes also Answer Set Prolog), offers unexpected advantages over the popular imperative programming approach, for example:
使用有希望的声明式编程范例,即答案集编程(ASP,有时也称为答案集Prolog),与流行的命令式编程方法相比,具有出乎意料的优势,例如:
- Short solutions (as measured by lines of code) 简短的解决方案(按代码行衡量)
- Transparency (including readability) 透明度(包括可读性)
- Reliability 可靠性
and many more. Once you have broken the problem down into its smallest pieces and written down all the necessary knowledge, you will not only solve the computational challenge. A big advantage is that you have digitized the knowledge and can use it for further problems or make it available in other ways.
还有很多。 将问题分解为最小的部分并写下所有必要的知识后,您不仅会解决计算难题。 一个很大的好处是您已经数字化了知识,可以将其用于其他问题或以其他方式使用。
E
Ë
Furthermore, it strengthens the cooperation between ‘business people’ and programmers. The integration of artificial intelligence is supported and acceptance at all levels will increase. This is a fundamental process that can only be achieved in cooperation with all departments of a company.
此外,它加强了“商人”与程序员之间的合作。 支持人工智能的集成,并且在各个级别的接受度都会增加。 这是一个基本过程,只有与公司所有部门合作才能实现。
什么是ASP?如何运作? (What is ASP and how does it work?)
An answer set program consists of given knowledge formulated as facts, rules (head(X) :- body(X).
) or constraints. The program is loaded by a solver and returns a “stable model”. This so called answer set consists of all facts that can be derived using the given rules and constraints. A finite amount of stable models are generated as solutions, of which one is finally selected.Note: grounding and solving the problems are not within the scope of this article.
答案集程序包括由事实,规则( head(X) :- body(X).
)或约束条件组成的给定知识。 该程序由求解器加载并返回“稳定模型”。 所谓的答案集包括可以使用给定的规则和约束导出的所有事实。 生成有限数量的稳定模型作为解决方案,最后选择其中一个。 注意: 接地和解决问题不在本文讨论范围之内。
Let us consider a famous example from logic about birds and penguins. It is a well known fact that birds can fly. This can be encoded as an answert set rule:
让我们考虑一个关于鸟类和企鹅的逻辑的著名例子。 众所周知,鸟类会飞。 可以将其编码为应答集规则:
canFly(X) :- bird(X).
The rule is read as “If X
is a bird, then X
can fly”. Now let’s add more knowledge! For example, facts that tell the unconditional truth, just as seagull ‘Malvin’ is a bird, but also penguin ‘Roger’ is one.
规则读为“如果 X
是鸟,那么 X
会飞” 。 现在,让我们添加更多知识! 例如,事实证明了无条件的事实,就像海鸥“ Malvin”是一只鸟,而企鹅“ Roger”是一只鸟一样。
canFly(X) :- bird(X).
bird(malvin).
bird(roger).== MODEL OUTPUT ==
Anwer: 1
bird(malvin) bird(roger) canFly(malvin) canFly(roger)
SATISFIABLE
As a result, the answer set tells us the known facts that Malvin and Roger are birds but also concluded that they are able to fly. The biology enthusiasts among us know, that penguins are unable to fly thus the model is wrong!
结果,答案集告诉我们已知的事实,即马尔文和罗杰是鸟类,但也得出结论,他们能够飞翔。 我们当中的生物学爱好者知道,企鹅无法飞行,因此模型是错误的!
In order to get acceptable results we need to add more knowledge in form of facts and integrity constraints to the program. Here, the model needs to know that Roger is not only a bird but also a penguin and that there is no bird that is a penguin which is able to fly.
为了获得可接受的结果,我们需要以事实和完整性约束的形式向程序添加更多知识。 在这里,模型需要知道罗杰不仅是鸟,而且还是企鹅,并且没有鸟是能够飞翔的企鹅。
canFly(X) :- bird(X).
bird(malvin).
bird(roger).
seagull(malvin).
penguin(roger).
:- canFly(X), penguin(X).== MODEL OUTPUT ==
UNSATISFIABLE
This does not bring the expected result and shows how important it is to think thoroughly and very carefully about the problem as well as the solution. The model tells, like stated by the programmer, that a bird can fly but there is no advise for birds that belong to the penguin family. To avoid this, we could add that only birds that are not penguins are able to fly.
这并没有带来预期的结果,并且表明了对问题以及解决方案进行透彻和仔细思考的重要性。 如程序员所述,该模型告诉鸟类可以飞,但是没有建议属于企鹅家族的鸟类。 为了避免这种情况,我们可以补充说,只有不是企鹅的鸟才可以飞。
canFly(X) :- bird(X), not penguin(X).
bird(malvin).
bird(roger).
seagull(malvin).
penguin(roger).
:- canFly(X), penguin(X).== MODEL OUTPUT ==
Answer: 1
bird(malvin) bird(roger) seagull(malvin) penguin(roger) canFly(malvin)
SATISFIABLE
Adding this knwoledge in Line [1], the stable model consists of the expected outcome.
在行[1]中添加此知识,稳定模型包括预期结果。
命令式与声明式编程 (Imperative versus declarative programming)
..解决数独 (.. for solving Sudoku)
Let’s look at another example to highlight the advantages of ASP mentioned above. Sudoku is a well known puzzle game and popular for explaining search problems. Given an initial 9x9 grid of cells containig numbers between 1 and 9 or blanks, all blanks must be filled with numbers. You win Sudoko if you find all values such that every row, column, and 3x3 subsquare contains the numbers 1–9, each with a single occurrence.
让我们看另一个示例,以突出上述ASP的优点。 数独是著名的益智游戏,因解释搜索问题而广受欢迎。 给定一个最初的9x9单元格网格,其中包含1到9之间的数字或空格,所有空格必须用数字填充。 如果您找到所有值,使每一行,每一列和3x3子正方形包含数字1–9,并且每个数字都出现一次,那么您将赢得Sudoko。
Python(命令式) (Python (imperative))
The following Code snippet will show how to solve the Sudoku in Python.
以下代码片段将显示如何在Python中解决Sudoku。
C(必须) (C (imperative))
Solving the Sudoku game in C looks very similar to Python. There is no significant difference in the approach.
用C解决Sudoku游戏看起来与Python非常相似。 该方法没有显着差异。
Both, Python and C, show written statements describing the control flow of a computation, thus, the ‘how to solve it’ and how the state has to be changed for the upcoming step.
Python和C都显示了描述计算控制流的书面语句,从而描述了“ 如何求解”以及在接下来的步骤中必须如何更改状态。
ASP(说明性) (ASP (declarative))
Last but no least, let’s have a look at the ASP code snippet — after initalization, we are able to solve the Sudoku with less than 10 lines of code!
最后但并非最不重要的一点,让我们看一下ASP代码段–初始化后,我们可以用少于10行的代码来解决Sudoku!
% Initialize the game (given numbers)
sudoku(1, 1, 5).
sudoku(1, 2, 3).
sudoku(1, 5, 7).
sudoku(2, 1, 6).
sudoku(2, 4, 1).
sudoku(2, 5, 9).
sudoku(2, 6, 5).
sudoku(3, 2, 9).
sudoku(3, 3, 8).
sudoku(3, 8, 6).
sudoku(4, 1, 8).
sudoku(4, 5, 6).
sudoku(4, 9, 3).
sudoku(5, 1, 4).
sudoku(5, 4, 8).
sudoku(5, 6, 3).
sudoku(5, 9, 1).
sudoku(6, 1, 7).
sudoku(6, 5, 2).
sudoku(6, 9, 6).
sudoku(7, 2, 6).
sudoku(7, 7, 2).
sudoku(7, 8, 8).
sudoku(8, 4, 4).
sudoku(8, 5, 1).
sudoku(8, 6, 9).
sudoku(8, 9, 5).
sudoku(9, 5, 8).
sudoku(9, 8, 7).
sudoku(9, 9, 9).
% define the grid
n(1..9).
x(1..9).
y(1..9).% each field contains exactly one number from 1 to 9
{sudoku(X,Y,N): n(N)} = 1 :- x(X) ,y(Y).% helper
subgrid(X,Y,A,B) :- x(X), x(A), y(Y), y(B),(X-1)/3 == (A-1)/3, (Y-1)/3 == (B-1)/3.% constraints
:- sudoku(X,Y,N), sudoku(A,Y,N), X!=A.
:- sudoku(X,Y,N), sudoku(X,B,N), Y!=B.
:- sudoku(X,Y,V), sudoku(A,B,V), subgrid(X,Y,A,B), X != A, Y != B.#show sudoku/3.== MODEL OUTPUT ==
Answer: 1
('sudoku', (2, 1, 6)), ('sudoku', (1, 2, 3)), ('sudoku', (9, 4, 2)), ('sudoku', (7, 2, 6)), ('sudoku', (7, 3, 1)), ('sudoku', (1, 9, 2)), ('sudoku', (5, 1, 4)), ('sudoku', (7, 4, 5)), ('sudoku', (4, 3, 9)), ('sudoku', (9, 1, 3)), ('sudoku', (4, 5, 6)), ('sudoku', (5, 6, 3)), ('sudoku', (2, 4, 1)), ('sudoku', (8, 1, 2)), ('sudoku', (8, 8, 3)), ('sudoku', (7, 9, 4)), ('sudoku', (8, 7, 6)), ('sudoku', (5, 4, 8)), ('sudoku', (7, 6, 7)), ('sudoku', (8, 6, 9)), ('sudoku', (6, 5, 2)), ('sudoku', (9, 7, 1)), ('sudoku', (3, 4, 3)), ('sudoku', (4, 7, 4)), ('sudoku', (3, 3, 8)), ('sudoku', (4, 8, 2)), ('sudoku', (1, 7, 9)), ('sudoku', (9, 9, 9)), ('sudoku', (9, 8, 7)), ('sudoku', (5, 9, 1)), ('sudoku', (4, 4, 7)), ('sudoku', (6, 9, 6)), ('sudoku', (7, 7, 2)), ('sudoku', (2, 7, 3)), ('sudoku', (5, 5, 5)), ('sudoku', (1, 5, 7)), ('sudoku', (1, 1, 5)), ('sudoku', (6, 3, 3)), ('sudoku', (2, 6, 5)), ('sudoku', (2, 9, 8)), ('sudoku', (8, 3, 7)), ('sudoku', (3, 6, 2)), ('sudoku', (3, 8, 6)), ('sudoku', (2, 8, 4)), ('sudoku', (1, 3, 4)), ('sudoku', (8, 2, 8)), ('sudoku', (3, 7, 5)), ('sudoku', (9, 5, 8)), ('sudoku', (4, 9, 3)), ('sudoku', (6, 4, 9)), ('sudoku', (7, 5, 3)), ('sudoku', (2, 5, 9)), ('sudoku', (8, 5, 1)), ('sudoku', (5, 3, 6)), ('sudoku', (4, 6, 1)), ('sudoku', (3, 9, 7)), ('sudoku', (1, 4, 6)), ('sudoku', (9, 2, 4)), ('sudoku', (5, 8, 9)), ('sudoku', (1, 8, 1)), ('sudoku', (6, 6, 4)), ('sudoku', (5, 7, 7)), ('sudoku', (7, 1, 9)), ('sudoku', (3, 5, 4)), ('sudoku', (6, 8, 5)), ('sudoku', (5, 2, 2)), ('sudoku', (2, 3, 2)), ('sudoku', (8, 9, 5)), ('sudoku', (9, 6, 6)), ('sudoku', (9, 3, 5)), ('sudoku', (6, 2, 1)), ('sudoku', (3, 1, 1)), ('sudoku', (4, 2, 5)), ('sudoku', (6, 1, 7)), ('sudoku', (4, 1, 8)), ('sudoku', (8, 4, 4)), ('sudoku', (2, 2, 7)), ('sudoku', (3, 2, 9)), ('sudoku', (1, 6, 8)), ('sudoku', (6, 7, 8)), ('sudoku', (7, 8, 8))}
SATISFIABLE
Note: using for example a Python wrapper for ASP, the result can look as handy as in the code examples above.
注意 :例如,使用ASP的Python包装器,结果看起来就像上面的代码示例一样方便。
Compared to Python and C, this is all about the ‘what’ and not about the ‘how’ — a fundamental difference between the two approaches.
与Python和C相比,这全都涉及“什么”而不是“如何”,这是这两种方法之间的根本区别。
At the very beginning, we define that the Sudoku board is 9x9 cells (or fields) and we use the numbers from 1 to 9 as the values to fill in. Following the basic rule that only one number between 1 and 9 may be filled in each field, we define a little helper. It states which fields refer to the same sub-grid. Finally, we only need the constraints to tell the solver what is possible and what is not. The first line checks, whether the choosen number is unique in the row whereas the second constraint checks the rule for columns. The third constraint completes the rules of the game, according to which a number must also be unique in the subgrid.
在一开始,我们定义Sudoku板是9x9单元(或字段),并使用1到9之间的数字作为填充值。遵循基本规则,即只能填充1到9之间的一个数字每个领域,我们定义一个小帮手。 它指出哪些字段引用相同的子网格 。 最后,我们只需要约束就可以告诉求解器什么是可能的,什么不是。 第一行检查选择的数字在行中是否唯一,而第二行约束检查列的规则。 第三个约束完善了游戏规则,根据该规则,子网格中的数字也必须唯一。
That was it. I admit the syntax takes getting used to. But once you’re familiar with it, you can create incredibly readable programs that solve highly complex problems for us.
就是这样 我承认语法需要习惯。 但是一旦您熟悉了它,就可以创建可读性极强的程序,为我们解决高度复杂的问题。
摘要 (Summary)
ASP is a very promising tool for knowledge preservation and declarative problem solving in the area of Knowledge Representation and Reasoning.
ASP是用于知识表示和推理领域的知识保存和声明式问题解决的非常有前途的工具。
Short solutions — Apart from the initial effort to map the Sudoku game, ASP provides by far the shortest way (measured in lines of code) to the solution.Transparency — There is no need to create rules which are unclear to the user and which concern the status of the programme. Only the basic conditions and the three rules of the game must be coded in order to end up at the correct result.Reliability — All rules of the game are fixed and the solver cannot create a black box with solution steps that are not traceable afterwards. This may not be important in the example given, but it is a major issue in many industrial applications.
简短的解决方案 -除了最初尝试绘制Sudoku游戏的映射外,ASP还提供了迄今为止解决方案的最短方法(以代码行衡量)。 透明度 -无需创建用户不清楚且与程序状态有关的规则。 为了最终得到正确的结果,只有游戏的基本条件和三个规则必须被编码。 可靠性 -游戏的所有规则都是固定的,求解器无法创建一个黑匣子,其解决方案步骤不可追溯。 在给出的示例中,这可能并不重要,但在许多工业应用中这是一个主要问题。
To mention only a few real-world examples, decision support for space shuttles at NASA, train scheduling in Switzerland — one of the world’s densest train network— , or team building at the largest transshipment terminal on the Mediterranean coast are evidence of its potential.
仅举几个实际的例子,对美国宇航局航天飞机的决策支持,瑞士的火车时刻表(世界上最密集的火车网络之一)或在地中海沿岸最大的转运码头的团队建设都证明了其潜力。
底线 (Bottom Line)
The acceptance of artificial intelligence in industry is still very low. In addition, many companies are simply overwhelmed by the existing flood of data.
人工智能在工业中的接受程度仍然很低。 此外,许多公司对现有的大量数据不知所措。
Bring the knowledge of operational subject matter experts together with the strengths of Artificial Intelligence.
将操作主题专家的知识与人工智能的优势结合在一起。
ASP offers an unique opportunity to digitalize and protect existing knowledge. Due to the good readability and the required understanding of the process as well as the problem to be solved, IT and business move closer together and thus achieve better and more sustainable success.
ASP为数字化和保护现有知识提供了独特的机会。 由于良好的可读性和对流程以及所要解决问题的理解要求,IT和业务部门之间的距离越来越近,从而获得了更好,更可持续的成功。
你喜欢这个故事吗? (Did you like the story?)
I appreciate likes and retweets of this article – there will be more about this topic soon! You can also find an executive summary here.
我喜欢本文的喜欢和转发-很快将有更多关于此主题的信息! 您也可以在此处找到执行摘要。
翻译自: https://towardsdatascience.com/knowledge-representation-and-reasoning-with-answer-set-programming-376e3113a421
推理编程
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387863.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!