推理編程
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.pswp.cn/news/387863.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/387863.shtml 英文地址,請注明出處:http://en.pswp.cn/news/387863.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!