Poor effort and reasoning: Use the C ++ program to solve "who raises fish"

Source: Internet
Author: User
Poor effort and reasoning: Use the C ++ program to solve "who raises fish"

In this issue, programmers mentioned the "Einstein's puzzle" and I noticed the question "who raises fish. The problem is as follows:

1. There are 5 houses in a street and 5 colors are sprayed
2. People of different nationalities living in each room
3. Each person drinks different drinks, cigarettes of different brands, and pets
Known:
1. British people live in a red house
2. Dogs in Swedes
3. Danish drinking tea
4. The green house is on the left of the White House.
5. Green house owners drink coffee
6. People smoking Pall Mall cigarettes raise birds
7. Yellow house owners smoke Dunhill cigarettes
8. People living in the middle House drink milk
9. Norwegian residents stay in the first room
10. The person smoking blends cigarettes lives next to the person who raises the cat.
11. Horse farmers live next to Dunhill cigarettes
12. Those who smoked the blue master drank beer.
13. Germans smoke Prince cigarettes
14. Norwegian residents live next to the Blue House
15. A person who smokes blends cigarettes has a neighbor who drinks water.
The question is: who raises fish?

Find the following information on the Internet: A Manual table solution, a lisp program solution, and a Niu's exhaustive program, which is implemented in C. This article uses the exhaustive method and inference method to answer this question. I used C ++ to solve the problem. Later, after seeing Niu's exhaustive procedure, in order to compare the efficiency of the two, he used C ++ to implement the exhaustive method (refer to Niu's procedure ). On my computer, the run time of the inference method is:

It takes 1000 milliseconds to solve 3828 queries, with an average of 3.8 milliseconds each time.

The average running time on my t7100 notebook is about 1.88 Ms. The running time of the exhaustive method is closely related to the initial data. Compared with the time required by the lisp Program (several hundred to several thousand milliseconds), the C ++ program is indeed faster.

This article mainly introduces algorithms. If you are interested in specific implementations, you can view the source code. From my home page (http://www.fmddlmyy.cn) you can download the complete source code for the inference and exhaustion methods.

1.

The idea of the exhaustive method is very simple: traverse all possible solutions (called the solution space in this article) without constraints, check all constraints one by one until a solution that meets the requirements is found. The key to achieving the exhaustive method is: how to traverse the solution space? In other words, we need to enumerate all possible solutions in a certain order.

The solution of "Who raised fish" can be regarded as five group attributes sorted by room location, for example:

1 Norwegian yellow cat water Dunhill
2 Danish blue horse tea blends
3 British Red Bird milk Pall Mall
4 German green fish Coffee Prince
5 Swedish white dog beer blue master

Each group of attributes is arranged by five elements (not repeated) and has a 120 (5 factorial) Possibility. without constraints, the solution space is 120 to the fifth power. Despite the large space for solutions, the constraints can exclude many solutions. Therefore, the number of checks required is not as many as that literally indicated by the "exhaustive method. The main logic of the exhaustive method is as follows:

Foreach (all columns in attribute 1 ){
If (Violation of constraints ){
Advance to the next cycle;
}
Foreach (all columns in attribute 2 ){
If (Violation of constraints ){
Advance to the next cycle;
}
Foreach (all columns in attribute 3 ){
If (Violation of constraints ){
Advance to the next cycle;
}
Foreach (all columns in attribute 4 ){
If (Violation of constraints ){
Advance to the next cycle;
}
Foreach (all columns in attribute 5 ){
If (compliant with constraints ){
Returns the correct solution;
}
}
}
}
}
}

The judgment before entering the inner loop is very important. Through this judgment, you can skip many checks. If the constraints play a role in the outer layer, you can skip more checks. When the outer loop checks the constraints, the constraints related to the inner attributes cannot be used because the inner attributes are not filled. Therefore, the level at which the attribute is placed in the loop has a significant impact on the total number of checks. For example:

Attribute arrangement (outer-> Inner) Check times of correct solution found Total number of checks (no response is returned after a correct solution is found) Execution time (1000 times average)
Country-color-cigarettes-Pets-drinks 6167 11476 14.6 Ms
Country-color-Pets-drinks-cigarettes 28896 61290 84.2 Ms
Country-cigarettes-drinks-color-Pets 47460 86756 110.3 Ms

It can be seen that carefully arranging attribute locations based on constraints can significantly reduce the total number of checks. There is also a great deal of unexpected "Number of correctly resolved checks" (or "execution time. Let's assume that we use the correct solution as the initial data. It will be done in a few minutes.

The method of exhaustion is similar to the "one power down ten meetings" in martial arts. Let's try again "to crack the odds ".

2 Reasoning

We can let computers gradually derive from the constraints According to human thinking patterns and get the answer. In order to better understand the problem domain, we first agree on some terms.

2.1 exploring problem domains 2.1.1 objects, solutions and Solutions

In this problem domain, we use an object to represent a person with all attributes. The object has six attributes: country, color, pet, beverage, cigarette, and room number. An empty object can be called for all objects with undefined attributes. Two empty objects cannot be distinguished. As long as an object has one or more known attributes, it becomes unique and unique.

Each solution contains five objects. All the known attributes of these five objects are different. The objects in the solution are logically unordered.

A solution set is a set of solutions.

If the reasoning process is considered as a ship's voyage, Wizard will predict this voyage as follows:

The solution is a ship, and the solution is a passenger,
All constraints constitute the environment of the problem domain.
At the beginning of the voyage, there was only one passenger on the ship.
All of its objects
All attributes are uncertain,
It means we have no idea about the environment.
When a ship passes through one constraint,
Passengers on the ship sometimes increase or decrease.
No matter how the passengers change, the ship always has all possibilities.
No matter how passengers change, our knowledge is increasing.
When the ship passes all the constraints,
A passenger will reach the destination,
It is the answer.

Before introducing the detailed reasoning process, let's examine the constraints.

2.1.2 constraints within an object and between objects

Constraints can be divided into two types: Intra-object constraints and inter-object constraints. The constraints in an object describe the relationship between two attributes in an object, for example, "The British live in a red house ". Object constraints describe the relationship between the attributes of the two objects. For example, "the person who smokes the blends cigarette lives next to the person who raises the cat ".

In the constraints of "who raises fish", there are two types of constraints between objects:

  1. Neighbor Relationship, for example, "the person who smokes blends cigarettes lives next to the person who raises the cat ";
  2. Ordered neighbor relationship. For example, "a green house is on the left of a White House ".
2.2 reasoning process 2.2.1 initial state

"We construct an empty solution. All attributes of all its objects are uncertain. It is the only solution in the solution set. It is not fixed to indicate the possibility. This only solution also contains all the possibilities ."

Although the beginning is very philosophical, it is not practical. The actual initial status is as follows:

Solution 1
UK red is uncertain
Not fixed in Sweden
The tea in Denmark is not yet determined
Germany is not yet determined. Prince is not yet determined.
Norwegian undetermined Room 1

I manually selected five "in-object constraints" to initialize the scheme. I specifically selected constraints that cannot appear on the same object. In this way, this solution still contains all possibilities. There is only one solution in the initial state.

2.2.2 use object Constraints

Use the current solution set as the input solution set, and then prepare an empty output solution set. Apply the constraints in the object to all the schemes in the input solution set. For example, if "green house owners drink coffee" is applied to solution 1, there may be two situations:

  1. Constraints can be placed on multiple objects in the solution. For example, "a green house owner drinks coffee" can be placed on an object with undefined color and beverage attributes. Every time you place constraints in a non-conflicting position, a new solution will be created. Place the new solution in the output solution set.
  2. Constraints may conflict with the solution. In this case, simply ignore this scheme.

The example program can print out the derivation process. The derivation process of "using constraints in objects" is as follows:

Initial status
------------------ A total of 1 solutions --------------------

Solution 1
UK red is uncertain
Not fixed in Sweden
The tea in Denmark is not yet determined
Germany is not yet determined. Prince is not yet determined.
Norwegian undetermined Room 1

Constraint added: 5. Green house owners drink coffee
------------------ A total of three solutions --------------------

Solution 1
UK red is uncertain
Green dog coffee in Sweden is not fixed yet
The tea in Denmark is not yet determined
Germany is not yet determined. Prince is not yet determined.
Norwegian undetermined Room 1

Solution 2
UK red is uncertain
Not fixed in Sweden
The tea in Denmark is not yet determined
Germany green undefined Coffee Prince undefined
Norwegian undetermined Room 1

Solution 3
UK red is uncertain
Not fixed in Sweden
The tea in Denmark is not yet determined
Germany is not yet determined. Prince is not yet determined.
Norwegian green undefined coffee undefined Room 1

Constraint added: 12. Beer for blue master users
------------------ A total of 7 solutions --------------------
...

Constraint added: 6. Bird-raising for people who smoke Pall Mall cigarettes
------------------ A total of 16 solutions --------------------
...

Add constraint: 7. Yellow house owners smoke Dunhill cigarettes
------------------ A total of 19 solutions --------------------
...

Constraint added: 9 + 14. Norwegian residents living in the first room + Norwegian residents living in the blue house next door = the second room is blue
------------------ A total of 28 solutions --------------------
...

Constraint added: 8. People living in the middle House drink milk
------------------ A total of 30 solutions --------------------
...

The specified attribute restricted in the object is applied to the undetermined attributes of the solution, which makes the solution clearer. Each output solution set always contains all possibilities after all the constraints are added.

2.2.3 Use constraints between objects

The constraint between objects must be applied to the known attributes of the solution, and then the two schemes determine whether the room number attributes of the two schemes match the specified relationship. To this end, we must ensure that:

  1. Assume that the constraint conditions use an attribute value (for example, if the color is green), the object must have this attribute value;
  2. The property of the room number of all objects in the solution is known.

If the scheme does not meet the above conditions, we must first "Expand" the scheme before applying the constraints to meet the above conditions. The process of "expansion" is to read a solution set and output a solution set. If the read scheme already has a specified attribute value, it is directly put into the output scheme set. If the read scheme does not have a specified attribute value, it will add all the possible schemes after this attribute value is added to the output scheme set.

The extension of the room number attribute only needs to be done once before the first "Inter-object constraint" is applied.

Use the constraints between objects to filter the extended solution set and remove the solution that does not meet the constraints. The final answer is obtained. The derivation process of this part is as follows:

Add constraint: 4. Green house on the left of White House
------------------ A total of four solutions --------------------

Solution 1
Room 3, UK Red Bird milk Pall Mall
Swedish blue dog beer blue master room 2
Danish white undefined tea blends Room 5
Germany green undefined Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Solution 2
Room 3, UK Red Bird milk Pall Mall
Swedish white dog beer blue master room 5
Danish Blue undefined tea blends Room 2
Germany green undefined Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Solution 3
UK blends Room 3
Swedish blue dog beer blue master room 2
Danish white bird tea Pall Mall Room 5
Germany green undefined Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Solution 4
UK blends Room 3
Swedish white dog beer blue master room 5
Danish Blue Bird tea Pall Mall Room 2
Germany green undefined Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Constraint added: 10. The person who smokes blends cigarettes lives next to the person who raises the cat
------------------ A total of four solutions --------------------

Solution 1
Room 3, UK Red Bird milk Pall Mall
Swedish blue dog beer blue master room 2
Danish white undefined tea blends Room 5
Germany green cat Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Solution 2
Room 3, UK Red Bird milk Pall Mall
Swedish white dog beer blue master room 5
Danish Blue undefined tea blends Room 2
Germany green undefined Coffee Prince Room 4
Norwegian yellow cat water Dunhill Room 1

Solution 3
UK blends Room 3
Swedish blue dog beer blue master room 2
Danish white bird tea Pall Mall Room 5
Germany green cat Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Solution 4
UK blends Room 3
Swedish white dog beer blue master room 5
Danish Blue Bird tea Pall Mall Room 2
Germany green cat Coffee Prince Room 4
Norwegian yellow unwatermarked Dunhill Room 1

Constraint added: 15. A person who smokes blends cigarettes has a neighbor who drinks water.
------------------ A total of 1 solutions --------------------

Solution 1
Room 3, UK Red Bird milk Pall Mall
Swedish white dog beer blue master room 5
Danish Blue undefined tea blends Room 2
Germany green undefined Coffee Prince Room 4
Norwegian yellow cat water Dunhill Room 1

Constraint added: 11. The horse-raising person lives next to the person smoking Dunhill cigarettes.
------------------ A total of 1 solutions --------------------

Solution 1
Room 3, UK Red Bird milk Pall Mall
Swedish white dog beer blue master room 5
Danish blue horse tea blends Room 2
Germany green fish Coffee Prince Room 4
Norwegian yellow cat water Dunhill Room 1

Reasoning ends.

Insufficient 2.3

There is another improvement in the current reasoning process: when the room number is extended, there is no need to expand the solution set first. Because the extended room numbers are arranged in several undetermined positions. You can enumerate the arrangement of all room numbers in each solution and filter them using constraints. This reduces the number of solutions that need to be placed in a centralized solution.

This reasoning method is close to the human thinking model. However, this is not the case: People like assumptions and attempts in reasoning. If they cannot get through, they will return and try other possibilities. Humans generally do not list all possibilities at a glance at each step. My algorithms do not have a "try-Backtrack" mechanism. Adding this mechanism will make the logic more complex and seriously violate the concept of Kiss (Keep it simple, stupid. In fact, it is most appropriate to raise the question of "who raises Fish.

I originally wanted to extend the question of "who raises fish" from Level 5 to level 12 to test the performance of the method of exhaustion and reasoning. I also chose a theme of the Wulin conference. However, after preparing 12 sets of attributes, we found that we had to write more than 70 hints to get started. If you are interested, you can use the reasoning program to answer the question.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.