How can we teach computer programs to solve mathematical problems by imitating human learning models?

Source: Internet
Author: User

 

On weekends, I read books about expert systems, including rules. I suddenly wondered if I could imitate my learning methods to improve the computing power of computer programs?
Imagine a child who knows nothing at the beginning. First, you must tell him what a number is, then tell him what a plus or a minus sign, and then tell him what a multiplication or division means, we also want to tell him that there are multiplication, division, and calculation of multiplication and division, and then introduced brackets that brackets always need to be calculated first. In this way, the more skills he is given, the more powerful his ability to solve problems.
So I want to test it.
Step 1: Teach the computer to learn what numbers are.
The following regular expression tells the "child" that the number is a number that may contain a hyphen (-) or hyphen (-), or hyphen, there may also be a bunch of 0-9 numbers starting with the decimal point. Of course it doesn't matter. In this case, it can recognize numbers.

public final class MathNumber {private MathNumber() {}public static String numberPattern = [-]?[0-9]+([.][0-9]*)?;public static Pattern pattern = Pattern.compile(numberPattern);public static Matcher match(String string) {Matcher match = pattern.matcher(string);if (match.find()) {return match;}throw new RuntimeException(string +  is not a number.);}}

The second step is to tell the "child" about the process of computing the mathematical problem.
If there is a space on both sides, ignore it. Then, check if it is a number. If it is already a number, it means the result is calculated. If not, start with the highest priority. If not, calculate it. If the formula is not found, it indicates that there is a problem with the formula. It is not a legal mathematical formula.
 

Public static String eval (String string) {string = string. trim (); while (! IsMathNumber (string) {// the System to which the same priority belongs. out. println (formula: + string); boolean found = false; for (MathInterface math: mathList) {Matcher matcher = math. match (string); if (matcher. find () {String exp = matcher. group (); String sig =; if (exp. charAt (0) = '-' & matcher. start ()! = 0) {// if it is not the first number, the-sign can only be used as the sig =+;} System. out. println (formula: + exp); String evalResult = math. eval (exp); string = string. substring (0, matcher. start () + sig + evalResult + string. substring (matcher. end (); System. out. println (exp + Calculation Result: + evalResult +, return to original form); found = true; break ;}} if (! Found) {throw new RuntimeException (string + is not a legal mathematical expression) ;}} return string ;}


From now on, the child has been able to solve the problem, but he still does not understand anything. He still does not know what is addition, subtraction, multiplication, and division. There is no way, the child is stupid. Just teach him more.
The following describes how to calculate, add, subtract, multiply, divide, remainder, Parentheses, and index.
 

addMathExpression(new Add()); addMathExpression(new Subtract()); addMathExpression(new Multiply()); addMathExpression(new Devide()); addMathExpression(new Minus()); addMathExpression(new Factorial()); addMathExpression(new Remainder()); addMathExpression(new Bracket()); addMathExpression(new Power()); Collections.sort(mathList, new MathComparator());

Since they are similar, only the implementation methods of addition and parentheses are pasted.
In addition, the priority is 1. It is composed of two numbers plus a "+" sign. The space before the number and the plus sign is useless. When computing, we add two numbers in addition. This computer is better than humans. Tell him how to add the numbers will never be wrong. In addition, it is advantageous to understand the advantages of addition, subtraction, multiplication, division, and first day.

public class Add implements MathInterface {static String plusPattern = BLANK + MathNumber.numberPattern + BLANK+ [+]{1} + BLANK + MathNumber.numberPattern + BLANK;static Pattern pattern = Pattern.compile(plusPattern);static Pattern plus = Pattern.compile(BLANK + \+);@Overridepublic Matcher match(String string) {return pattern.matcher(string);}@Overridepublic int priority() {return 1;}@Overridepublic String eval(String expression) {Matcher a = MathNumber.pattern.matcher(expression);if (a.find()) {expression = expression.substring(a.end());}Matcher p = plus.matcher(expression);if (p.find()) {expression = expression.substring(p.end());}Matcher b = MathNumber.pattern.matcher(expression);if (b.find()) {}return new BigDecimal(a.group()).add(new BigDecimal(b.group())).toString();}}


The following is the brackets. The priority of the brackets is the largest. If there is a bracket, it should be calculated first. Of course, you must first calculate the content in the innermost square brackets. The content in the brackets can be pulled out Before calculation. You don't need to worry about the content outside. After calculation, you can just put it back.

public class Bracket implements MathInterface {static String bracketPattern = BLANK + [(]{1}[^(]*?[)] + BLANK;static Pattern pattern = Pattern.compile(bracketPattern);@Overridepublic Matcher match(String string) {return pattern.matcher(string);}@Overridepublic int priority() {return Integer.MAX_VALUE;}@Overridepublic String eval(String expression) {expression = expression.trim();return MathEvaluation.eval(expression.substring(1,expression.length() - 1));}}

So far, our program "baby" has learned mathematical computation and asked Yi to try it out.

Public static void main (String [] args) {String string = 1 + 2 ^ (4/2) + 5% 2; System. out. println (Result: + MathEvaluation. eval (string ));}

The procedure is as follows:

Formula: 1 + 2 ^ (4/2) + 5% 2 formula: (4/2) formula: 4/2 formula: 4/24/2 Calculation Result: 2.00, back-to-source formula (4/2) the calculation result is 2.00. The formula is 1 + 2 ^ 2.00 + 5% 2. The formula is 2 ^ 2.002 ^ 2.00. The calculation result is 4, return-to-source formula: 1 + 4 + 5% 2 discovery formula: 5% 25% 2 Calculation Result: 1. Return-to-source formula: 1 + 4 + 1 discovery formula: the calculation result of 1 + 41 + 4 is: 5. The formula for solving the Back-to-source formula is: 5 + 1. The formula for finding 5 + 15 + 1 is: 6. The result of the Back-to-source formula is: 6

Haha, the procedure of the baby program is very consistent with that of the person, and the procedure is very easy to understand. Shenma compilation principle, Shenma infix expressions are not used. (The execution efficiency is not necessarily high compared with other algorithms. It is only used to enhance the processing capability of the program through verification rules. Because no in-depth test is conducted, whether Regular Expressions and program logic are strictly written is not thoroughly verified)

In fact, although the program is very simple, it is actually a prototype of a simple rule engine.
First, he loads many business processing rules, such as adding, subtraction, multiplication, division, number insertion, index, and remainder.
Second, his business rules can be expanded constantly.
Third, as long as the facts are given, and finally, through the continuous application of rules, the results will be exported, either the correct results or the facts given are wrong.

If you need source code, go to GIT to get the code.

Git address: http://git.nidongde.net/tinyframework/mathexp.git

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.