Learn

Friday, 22 December 2017

What are the problems dealing with relational database management in java artificial intelligence algorithm


Relational Database Model Has Problems Dealing with Rapidly Changing Data Requirements:

When people are first introduced to Semantic Web technologies their first reaction is often something like, “I can just do that with a database.” The relational database model is an efficient way to express and work with slowly changing data models. There are some clever tools for dealing with data change requirements in the database world (ActiveRecord and migrations being a good example) but it is awkward to have end users and even developers tagging on new data attributes to relational database tables.

This same limitation also applies to object-oriented programming and object modeling. Even with dynamic languages that facilitate modifying classes at runtime, the options for adding attributes to existing models is just too limiting. The same argument can be made against the use of XML constrained by conformance to either DTDs or XML Schemas. It is true that RDF and RDFS can be serialized to XML using many pre-existing XML namespaces for different knowledge sources and their schemas but it turns out that this is done in a way that does not reduce the flexibility for extending data models. XML storage is really only a serialization of RDF and many developers who are just starting to use Semantic Web technologies initially get confused trying to read XML serialization of RDF almost like trying to read a PDF file with a plain text editor and something to be avoided.

A major goal for the rest of this chapter is convincing you that modeling data with RDF and RDFS facilitates freely extending data models and also allows fairly easy integration of data from different sources using different schemas without explicitly converting data from one schema to another for reuse.

RDF: The Universal Data Format

The Resource Description Framework (RDF) is used to encode information and the RDF Schema (RDFS) facilitates using data with different RDF encodings without the need to convert data formats.

RDF data was originally encoded as XML and intended for automated processing. Here we will use two simple to read formats called ”N-Triples” and ”N3.” Sesame can be used to convert between all RDF formats so we might as well use formats that are easier to read and understand. RDF data consists of a set of triple values:

  • Subject
  • Predicate
  • Object

Some of the work with Semantic Web technologies deals with processing news stories, extracting semantic information from the text, and storing it in RDF. We will use this application domain for the examples in this article. We deal with triples like:

  • Subject: A URL or URI of a new website
  • Predicate: A relation like 'containsPerson'
  • Object: A value like 'Bill Clinton'


As previously mentioned, we will use either URIs or string literals as values for subjects and objects. We will always use URIs for the values of predicates. In any case, URIs are usually preferred to string literals because they are unique. We will see an example of this preferred use but first, we need to learn the N-Triple and N3 RDF formats.

RDF was more flexible than Object Modeling in programming languages, relational databases, and XML with schemas. If we can tag new attributes on the fly to existing data, how do we prevent what We might call “data chaos” as we modify existing data sources? It turns out that the solution to this problem is also the solution for encoding real semantics (or meaning) with data: we usually use unique URIs for RDF subjects, predicates, and objects, and usually with a preference for not using string literals. We will try to make this idea more clear with some examples.

Any part of a triple (subject, predicate, or object) is either a URI or a string literal. URIs encode namespaces. For example, the containsPerson predicate in the last example could properly be written as:
http://knowledgebooks.com/ontology/#containsPerson

The first part of this URI is considered to be the namespace for (what we will use as a predicate) “containsPerson.” When different RDF triples use this same predicate, this is some assurance to us that all users of this predicate subscribe to the same meaning. Furthermore, we will see in Section 4.3 we can use RDFS to state equivalency between this predicate (in the namespace http://knowledgebooks.com/ontology/) with predicates represented by different URIs used in other data sources. In an “artificial intelligence” sense, software that we write does not understand a predicate like “containsPerson” in the way that a human reader can by combining understood common meanings for the words “contains” and “person” but for many interesting and useful types of applications that is fine as long as the predicate is used consistently. We will see shortly that we can define abbreviation prefixes for namespaces which makes RDF and RDFS files shorter and easier to read.

What is Semantic web in Java Based Artificial Intelligence Algorithm



What is the Semantic web in Java Based Artificial Intelligence Algorithm:

The SemanticWeb is intended to provide a massive linked set of data for use by software systems just as the World Wide Web provides a massive collection of linked web pages for human reading and browsing. The Semantic Web is like the web in that anyone can generate any content that they want. This freedom to publish anything works for the web because we use our ability to understand the natural language to interpret what we read – and often to dismiss material that based upon our own knowledge we consider to be incorrect.

The core concept for the Semantic Web is data integration and use from different sources. As we will soon see, the tools for implementing the Semantic Web are designed for encoding data and sharing data from many different sources.

There are several very good Semantic Web toolkits for the Java language and platform. I will use Sesame because it is what I often use in my own work and I believe that it is a good starting technology for your first experiments with Semantic Web technologies. This chapter provides an incomplete coverage of Semantic Web technologies and is intended merely as a gentle introduction to a few useful techniques and how to implement those techniques in Java.

The figure below shows a layered set of data models that are used to implement Semantic Web applications. To design and implement these applications we need to think in terms of physical models (storage and access of RDF, RDFS, and perhaps OWL data), logical models (how we use RDF and RDFS to define relationships between data represented as unique URIs and string literals and how we logically combine data from different sources) and conceptual modeling (higher level knowledge representation using OWL).


Figure: Layers of data models used in implementing SemanticWeb applications


Sunday, 3 December 2017

How Does OldBlockState Is Used In JAVA Artificial Intelligence Algorithm Progrmming


Use of OldBlockState:


The next POJO class OldBlockState is used to represent previous states of blocks as they are being moved as the rules in this example “fire.” We will later see rules that will not put a block into a state that it previously existed in:

public static class OldBlockState extends Block {

public OldBlockState(String name,
String onTopOf,

String supporting) {

super(name, onTopOf, supporting);

}

public String toString() {

return "[OldBlockState_" + this.hashCode() +" " + name + " on top of: " + onTopOf +" supporting: " + supporting+"]";


}


}


The next POJO class Goal is used to represent a goal state for the blocks that we are trying to reach:


public static class Goal {
private String supportingBlock;
private String supportedBlock;
public Goal(String supporting, String supported) {
this.supportingBlock = supporting;
this.supportedBlock = supported;
}
public String toString() {
return "[Goal_" + this.hashCode() +
" Goal: supporting block: " +
supportingBlock +
" and supported block: " +
supportedBlock +"]";
}
public void setSupportingBlock(
String supportingBlock) {
this.supportingBlock = supportingBlock;
}
public String getSupportingBlock() {
return supportingBlock;
}
public void setSupportedBlock(
String supportedBlock) {
this.supportedBlock = supportedBlock;
}
public String getSupportedBlock() {
return supportedBlock;
}
}

How to Run JAVA POJO Object Models for Blocks World Example Artificial Intelligence Programming



POJO Object Models for Blocks World Example:

We will use the following three POJO classes (defined in the file DroolsBock- World.java as static inner classes). The first POJO class Block represents the state of one block:


public static class Block {

protected String name;
protected String onTopOf;
protected String supporting;

public Block(String name, String onTopOf, String supporting) {

this.name = name;
this.onTopOf = onTopOf;
this.supporting = supporting;

}

public String toString() {

return "[Block_" + this.hashCode() + " " +
name + " on top of: " + onTopOf +
" supporting: " + supporting+"]";

}

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

public String getOnTopOf() {

return this.onTopOf;

}

public void setOnTopOf(String onTopOf) {

this.onTopOf = onTopOf;

}

public String getSupporting() {

return this.supporting;

}

public void setSupporting(String supporting) {

this.supporting = supporting;

}
}

How Read Rules Utility Works In Java Artificial Intelligence Programming


Read Rule Utility:


The method readRule is a utility for reading and compiling a rule file that was generated automatically by the Eclipse Drools Workbench; in general your projects will have one or more rules files that you will load as in this example. In method readRule we opened an input stream reader on the source code for the example Drools rule file Sample.drl. Drools have the ability to modify the rule syntax to create Domain Specific Languages (DSLs) that match your application or business domain. This can be very useful if you want domain experts to create rules since they can “use their own language.” We will not cover custom DSLs in this chapter but the Drools documentation covers this in detail. Here is the rest of the definition of method readRule:

The readRule utility method can be copied to new rule projects that are not created using the Eclipse Drools Workbench and modified as appropriate to get you started with the Java “boilerplate” required by Drools. This implementation uses Drools defaults, the most important being the “conflict resolution strategy” that defaults to first checking the most recently modified working memory POJO objects to see which rules can fire. This produces a depth-first search behavior. We will modify the readRule utility method later in Section 5.4 when we will need to change this default Drools reasoning behavior from depth first to breadth-first search.

We will need a Plain Old Java Object (POJO) class to represent messages in the example rule set. This demo class was generated by the Eclipse Drools Workbench:

public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;

private int status;

public String getMessage() {

return this.message;

}

public void setMessage(String message) {

this.message = message;

}

public int getStatus() {

return this.status;

}

public void setStatus( int status ) {

this.status = status;

}
}
}

How to use Drolls Framework in Java API Development Artificial Intelligence




Using Drolls in API Java Development

We looked at the sample rules file Sample.drl in the last section which is generated automatically when creating a demo project with the Eclipse Drools Workbench. We will use the other generated file DroolsTest.java as an illustrative example in this section. The file DroolsTest.java is almost 100 lines long so I will list it in small fragments followed by an explanation of each code fragment. The first thing to note is that the Java client code is in the same package as the rules file:


package com.sample;

import java.io.InputStreamReader;

import java.io.Reader;

import org.drools.RuleBase;

import org.drools.RuleBaseFactory;

import org.drools.WorkingMemory;

import org.drools.compiler.PackageBuilder;

import org.drools.rule.Package;


This main function is an example showing how to use a rule package defined in a rule source file. We will see the definition of the utility method readRule that opens a rule file and returns an instance of class RuleBase shortly. After creating an instance of RuleBase we create an instance of the Message class and add it to open memory:


public class DroolsTest {

public static final void main(String[] args) {

try {
        RuleBase ruleBase = readRule();
        WorkingMemory workingMemory =
         ruleBase.newStatefulSession();
        Message message = new Message();
        message.setMessage( "Hello World" );
        message.setStatus( Message.HELLO );
        workingMemory.insert( message );
        workingMemory.fireAllRules();
     }

catch (Throwable t) {

t.printStackTrace();

}

}

The main method creates a new rule base and working memory. Working memory is responsible for maintaining the “facts” in the system – in this case facts are Plain Old Java Objects (POJOs) that are maintained in a collection.

An instance of class Message is created and its status is set to the constant value Message: HELLO. We saw in the last section how the first example rule has a condition that allows the rule to “fire” if there is an instance of the class message that has its status attribute set to this value.




How does Drools Java expert system language and libraries work in Java artificial Intelligence Programming


The primary web site for Drools is www.jboss.org/drools where you can download the source code and documentation.


While there is some interest in using packages like Drools for “business rules” to capture business process knowledge, often as embedded components in large systems, expert systems have historically been built to approach human level expertise for very specific tasks like configuring computer systems and medical diagnosis. The examples in this chapter are very simple and are intended to show you how to embed Drools in your Java applications and to show you a few tricks for using forward chaining rule-based systems. Drools is a Domain Specific Language (DSL) that attempts to provide a syntax that is easier to use than a general-purpose programming language.


It not usually recommend Java IDEs (a personal choice!) but if you already use Eclipse then I suggest that you use the Drools plugins for Eclipse (the “Eclipse Drools Workbench”) which help setting up projects and understand the Drools rule language syntax.

The Eclipse Drools Workbench can automatically generate a small demo which I will go over in some detail in the next two sections. I then design and implement two simple example rule systems for solving block world type problems and for answering help desk questions.

The material in this chapter exclusively covers forward chaining production systems

Figure: Using Drools for developing rule-based systems and then deploying
them.


(also called “expert systems”). Forward chaining systems start with a set of known facts, and apply rules to work towards solving one or more goals. An alternative approach, often used in Prolog programs, is to use backward chaining. Backward chaining systems start with a final goal and attempt to work backwards towards currently known facts.

The phrase, expert systems, was almost synonymous with artificial intelligence in the early and mid 1980s. The application of expert system techniques to real problems, like configuring DEC VAX minicomputers, medical diagnosis, and evaluating seismic data for planning oil exploration had everyone very excited. Unfortunately, expert systems were over hyped and there was an eventual backlash that affected the entire field of AI. Still, the knowledge of how to write expert systems is a useful skill. This chapter contains a tutorial for using the Drools system and also shows how to use machine learning to help generate rules when training data is available.

As seen in Figure, Drools development is interactive: you will work in an environment where you can quickly add and change rules and re-run test cases. This interactive style of development is similar to using PowerLoom as we seen on the blog.



How to Use PowerLoom APIs In Java Artificial Intelligence Framework Programming



Using the PowerLoom APIs in Java Programs:

             Once you interactively develop concepts, rules, and relations then it is likely that you may want to use them with PowerLoom in an embedded mode, making PowerLoom a part of your application. I will get you started with a few Java example programs. The source code for this chapter is in the subdirectory src-powerloom-reasoning.

               If you download the PowerLoom manual (a PDF file) from the PowerLoom web site, you will have the complete Java API documentation for the Java version of Power-Loom (there are also C++ and Common Lisp versions with separate documentation).I have found that I usually use just a small subset of the Java PowerLoom APIs and I have “wrapped” this subset in a wrapper class in the file PowerLoomUtils.java. We will use my wrapper class for the examples in the rest of this chapter.


My wrapper class has the follow public methods:

  • PowerLoomUtils() – constructor initializes the Java PowerLoom runtime system.
  • Load(String fpath) – load a source *.plm file.
  • ChangeModule(String workingModule) – set the current PowerLoom working module (“PL-USER” is the default module).
  • assertProposition(String proposition) – asserts a new proposition; for example:”(and (company c3) (company-name c3 n”Moms Groceryn”))”. Notethat quotation marks are escaped with a backslash character. You can also use single quote characters like: ”(and (company c3) (company-name c3 ’Moms Grocery’))” because I convert single quotes in my wrapper code.
  • createRelation(String relation, int arity) – create a new relation with a specifiedarity (number of “arguments”). For example you could create a relation “owns” with arity 2 and then assert “(owns Elaine ’Moms Grocery’)” – I usually do not use this API since I prefer to place relations (with rules) in a source code file ending in the extention *.plm.
  • doQuery(String query) – returns a list of results from a query. Each result in the list is itself a list. 
You will always want to work in an interactive PowerLoom console for writing and debugging PowerLoom models. I built the model in test.plm (in the subdirectory test data) interactively and we will use it here in an embedded Java example:


PowerLoomUtils plu = new PowerLoomUtils();
plu.load("test_data/test.plm");
plu.changeModule("BUSINESS");
plu.assertProposition(
"(and (company c1)" +
" (company-name c1 \"Moms Grocery\"))");
plu.assertProposition(
"(and (company c2)" +
" (company-name c2 \"IBM\"))");
plu.assertProposition(
"(and (company c3)" +
" (company-name c3 \"Apple\"))");
List answers = plu.doQuery("all ?x (company ?x)");
System.out.println(answers);
// answers: [[C3], [C2], [C1]]
answers = plu.doQuery(
"all (?x ?name)" +
" (and" +
" (company ?x)" +
" (company-name ?x ?name))");
System.out.println(answers);
// answers:
// [[C3, "Apple"],
// [C2, "IBM"],
// [C1, "Moms Grocery"]]
plu.createRelation("CEO", 2);
plu.assertProposition(
"(CEO \"Apple\" \"SteveJobs\")");
answers = plu.doQuery(
"all (?x ?name ?ceo)" +
" (and" +
" (company-name ?x ?name)" +
" (CEO ?name ?ceo))");
System.out.println(answers);
// answers: [[C3, "Apple", "SteveJobs"]]

I have added the program output produced by printing the value of the list variable “answers” as comments after each System.out.println call. In the wrapper API calls that take a string argument, I broke long strings over several lines for formatting to the width of a page; you would not do this in your own programs because of the cost of the extra string concatenation.

We will not look at the implementation of the PowerLoomUtils class – you can read the code if you are interested. That said, I will make a few commments on the Java PowerLoom APIs. The class PLI contains static methods for initializing the system, loading PowerLoom source files. Here are a few examples:

PLI.initialize();
PLI.load("test.plm", null);
PLI.sChangeModule("BUSINESS", null);

How to run PowerLoom Interactively in Java Artificial Intelligence Programming




Running PowerLoom:

We will experiment with PowerLoom concepts, relations, and rules in this section in an interactive command shell. I will introduce more examples of PowerLoom functionality for asserting instances of concepts, performing queries, loading PowerLoom source files, defining relations, using separate modules, and asking Power-Loom to explain the inference process that is used for query processing.

You can run PowerLoom using the command line interface by changing directory to the lib subdirectory from the ZIP file for this book and trying:


java -cp powerloom.jar:stella.jar \\
edu.isi.powerloom.PowerLoom

This starts the PowerLoom standalone system and prints a prompt that includes the name of the current module. The default module name is “PL-USER”. In the first example, when I enter the person concept at the interactive prompt then PowerLoom prints the result of the expression that just entered.

PL-USER |= (defconcept person)
|c|PERSON

PL-USER |= (defconcept parent (?p person))
|c|PARENT

PL-USER |= (defrelation parent-of
((?p1 parent) (?p2 person)))
|r|PARENT-OF

PL-USER |= (assert (person Ken))
|P|(PERSON KEN)

PL-USER |= (assert (person Mark))
|P|(PERSON MARK)

PL-USER |= (assert (parent-of Ken Mark))
|P|(PARENT-OF KEN MARK)

Now that we have entered two concepts, a test relation, and asserted a few facts, we
can look at an example of PowerLoom’s query language:

PL-USER |= (retrieve all ?p (person ?p))
There are 2 solutions:

#1: ?P=MARK
#2: ?P=KEN

PL-USER |= (retrieve all ?p (parent ?p))

There is 1 solution:
#1: ?P=KEN
PL-USER |=

The obvious point to note from this example is that we never specified that Ken was a parent; rather, PowerLoom deduced this from the parent-of relation.

PowerLoom’s command line system prompts you with the string “PL-USER —=“ and you can type any definition or query. Like Lisp, PowerLoom uses a prefix notation and expressions are contained in parenthesis. PowerLoom supports a module system for partitioning concepts, relations, functions, and rules into different sets and as previously mentioned “PL-USER” is the default module. PowerLoom modules can form a hierarchy, inheriting concepts, relations, and rules from parent modules.

The subdirectory test data contains the demo file business.plm written by Robert MacGregor that is supplied with the full PowerLoom distribution. You can load his complete example using:

PL-USER |= (load "../test_data/business.plm")

This is a good example because it demonstrates most of the available functionality of PowerLoom in a short 200 lines. When you are done reading this chapter, please take a few minutes to read through this example file since I do not list it here. There are a few things to notice in this example. Here we see a rule used to make the relation “contains” transitive:

(defrelation contains (

      (?l1 geographic-location)

           (?l2 geographic-location)))

(defrule transitive-contains (=> (and (contains ?l1 ?l2)
         (contains ?l2 ?l3))
          (contains ?l1 ?l3)))

The operator => means that if the first clause is true then so is the second. In English, this rule could be stated “if an instance i1 contains i2 and if instance i2 contains i3 then we can infer that i1 also contains i3.” To see how this rule works in practice, we can switch to the example module “BUSINESS” and find all locations contained inside another location:

PL-USER |= (in-module "BUSINESS")
BUSINESS |= (retrieve all
(?location1 ?location2)
(contains ?location1 ?location2))
There are 15 solutions:
#1: ?LOCATION1=SOUTHERN-US, ?LOCATION2=TEXAS
#2: ?LOCATION1=TEXAS, ?LOCATION2=AUSTIN
#3: ?LOCATION1=TEXAS, ?LOCATION2=DALLAS
#4: ?LOCATION1=UNITED-STATES, ?LOCATION2=SOUTHERN-US
#5: ?LOCATION1=GEORGIA, ?LOCATION2=ATLANTA
#6: ?LOCATION1=EASTERN-US, ?LOCATION2=GEORGIA
#7: ?LOCATION1=UNITED-STATES, ?LOCATION2=EASTERN-US
#8: ?LOCATION1=SOUTHERN-US, ?LOCATION2=DALLAS
#9: ?LOCATION1=SOUTHERN-US, ?LOCATION2=AUSTIN
#10: ?LOCATION1=UNITED-STATES, ?LOCATION2=DALLAS
#11: ?LOCATION1=UNITED-STATES, ?LOCATION2=TEXAS
#12: ?LOCATION1=UNITED-STATES, ?LOCATION2=AUSTIN
#13: ?LOCATION1=EASTERN-US, ?LOCATION2=ATLANTA
#14: ?LOCATION1=UNITED-STATES, ?LOCATION2=GEORGIA
#15: ?LOCATION1=UNITED-STATES, ?LOCATION2=ATLANTA
BUSINESS |=

Here we have fifteen solutions even though there are only seven “contains” relations asserted in the business.plm file – the other eight solutions were inferred. In addition to the “retrieve” function that finds solutions matching a query you can also use the “ask” function to determine if a specified relation is true; for example:

BUSINESS |= (ask (contains UNITED-STATES DALLAS))
TRUE
BUSINESS |=

For complex queries you can use the “why” function to see how PowerLoom solved the last query:

BUSINESS |= (ask (contains southern-us dallas))
TRUE
BUSINESS |= (why)
1 (CONTAINS ?location1 ?location2)
follows by Modus Ponens
with substitution {?l1/SOUTHERN-US, ?l3/DALLAS,
?l2/TEXAS}
since 1.1 ! (FORALL (?l1 ?l3)
(<= (CONTAINS ?l1 ?l3)
(EXISTS (?l2)
(AND (CONTAINS ?l1 ?l2)
(CONTAINS ?l2 ?l3)))))
and 1.2 ! (CONTAINS SOUTHERN-US TEXAS)
and 1.3 ! (CONTAINS TEXAS DALLAS)
BUSINESS |=

By default the explanation facility is turned off because it causes PowerLoom to run more slowly; it was turned on in the file business.plm using the statement: (set-feature justifications)

Basics of Power Loom Framework Java Artificial Intelligence



PowerLoom Framework:

             PowerLoom is designed to be an expressive language for knowledge representation and reasoning. As a result, PowerLoom is not a complete reasoning system but makes tradeoffs for completeness of inferences and expressivity vs. computational efficiency. It is interesting to note that Loom and PowerLoom were designed and implemented to solve real-world problems and the tradeoffs to make these problems computationally tractable have informed the design and implementation of these systems. PowerLoom does not make all possible inferences from concepts that it operates on. The PowerLoom distribution contains two very detailed examples for representing relationships between companies and for information dealing with airplanes. These examples are more detailed than the simpler example of data from news stories used in this chapter. We will look one of these examples (business rules and relations) and after working through this chapter, I encourage you to interactively experiment with the two examples that ship with PowerLoom. We will start by defining some terms used in PowerLoom: 

  • concept – the Java equivalent would be an instance of a class
  • relation – specifies a link between two concepts
  • function – functional mapping of one concept to another
  • rule – allows new concepts to be deduced without explicitly asserting them

A relation can specify the types of concepts that a relation connects. An example will make this clear and introduce the Lisp-like syntax of PowerLoom statements:

;;; Concepts:

(defconcept person)

(defconcept parent (?p person))

;;; Relation:

(defrelation parent-of ((?p1 parent) (?p2 person)))

Here I have defined two concepts: person and parent. Note that we have a hierarchy of concept types here: the parent is a more specific concept type than the person concept. All instances that are parents are also of type person. The relation parent of links a parent concept to a person concept.

We will learn more about basic PowerLoom functionality in the next two sections as we use PowerLoom in an interactive session and when we embed PowerLoom in a Java example program.

How to use Login in AI? Which Libraries are used in Artificial Intelligence Logic programming


Logic:

    Logic is the basis for both Knowledge Representation and for reasoning about knowledge. We will encode knowledge using logic and see that we can then infer new facts that are not explicitly asserted.

    First Order Logic was invented by the philosophers Frege and Peirce and is the most widely studied logic system. Unfortunately, full First Order Logic is not computationally tractable for most non-trivial problems so we use more restricted logics. We will use two reasoning systems in this book that support more limited logics:

  • We use PowerLoom is this chapter. PowerLoom supports a combination of limited first order predicate logic and features of description logic. Power-Loom is able to classify objects, use rules to infer facts from existing facts and to perform subsumption (determining class membership of instances).

  • We will use RDF Schema (RDFS) reasoning in Chapter 4. RDFS supports more limited reasoning than descriptive logic reasoners like PowerLoom and OWL Description Logic reasoners.


Examples of Different Logic Types:

Propositional logic is limited to atomic statements that can be either true or false:

Brady-is-a-bird

Brady-has-feathers

First Order Predicate Logic allows access to the structure of logic statements dealing with predicates that operate on atoms. To use a Prolog notation:

feathers(X) :- bird(X).

bird(brady).

Here “feathers” and “bird” are predicates and “Brady” is an atom. The first example states that for all X, if X is a bird, then X has feathers. In the second example, we state that Brady is a bird. Notice that in the Prolog notation that we are using, variables are capitalized and predicate names and literal atoms are lower cases. Here is a query that asks who has feathers:

?- feathers(X).

X = brady

                    In this example, through inference, we have determined a new fact, that Brady has feathers because we know that Brady is a bird and we have the rule (or predicate)  stating that all birds have feathers. Prolog is not strictly a pure logic programming language since the order in which rules (predicates) are defined changes the inference results. Prolog is a great language for some types of projects (I have used Prolog in both natural language processing and in planning projects). We will see that PowerLoom is considerably more flexible than Prolog but does have a steep
learning curve.

Description Logic deals with descriptions of concepts and how these descriptions define the domain of concepts. In terms used in object-oriented programming languages:

                  membership in a class is determined implicitly by the description of the object and not by explicitly stating something like “Brady is a member of the bird class.” Description logics divide statements into relations (historically referred to as TBox) and concepts (historically called ABox). We would say that a statement like “All birds have feathers” is stored in the TBox while a specific assertion like “Brady is a bird” is stored in the ABox. 

How to do Resoning in AI? Which Framework Libraries used for reasoning in Java Artificial Intelligence Programming



Reasoning: 

       The reasoning is a broad topic. In this blog we will concentrate on the use of the PowerLoom descriptive logic reasoning system. PowerLoom is available with a Java runtime and Java API – this is what we will use for the examples in this chapter. PowerLoom can also be used with JRuby. PowerLoom is available in Common Lisp and C++ versions. Additionally, we will look briefly at different kinds of reasoning systems in next blog on the Semantic Web.

      While the material in this chapter will get you started with development using a powerful reasoning system and embedding this reasoning system in Java applications, you will likely want to dig deeper and I suggest sources for further study at the end of this chapter. PowerLoom is a newer version of the classic Loom Descriptive Logic reasoning system written at ISI. The required JAR files for PowerLoom are included in the ZIP file for this book but at some point, you will probably want to download the entire PowerLoom distribution to get more examples and access to documentation; the PowerLoom web site can be found at http://www.isi.edu/isd/LOOM/PowerLoom/.

     While we will look at an example of embedding the PowerLoom runtime and a PowerLoom model in a Java example program, I want to make a general comment on PowerLoom development: you will spend most of your time interactively running PowerLoom in an interactive shell that lets you type in concepts, relations, rules, and queries and immediately see the results. If you have ever programmed in Lisp, then this mode of interactive programming will be familiar to you. As seen in Figure 3.1 after interactive development you can deploy in a Java application. This style of development supports entering facts and trying rules and relations interactively and as you get things working you can paste what works into a PowerLoom source file. 

If you have only worked with compiled languages like Java and C++ this development style may take a while to get used to and appreciate. As seen in Figure
the PowerLoom runtime system, with relations and rules, can be embedded in Java applications that typically clear PowerLoom data memory, assert facts from other live data sources, and then use PowerLoom for inferencing.

Figure: Overview of  PowerLoom for development and
deployment

How Does Alpha Beta search works in Artificial Intelligence Java Programming



Alpha-Beta Search:

         The first game that we will implement will be tic-tac-toe, so we will use this simple game to explain how the min-max search (with alpha-beta cutoffs) works. Figure shows the possible moves generated from a tic-tac-toe position where X has made three moves and O has made two moves; it is O’s turn to move. This is “level 0” in Figure 2.8. At level 0, O has four possible moves. How do we assign a fitness value to each of O’s possible moves at level 0? The basic min-max search algorithm provides a simple solution to this problem: for each possible move by O in level 1, make the move and store the resulting 4 board positions. Now, at level 1,  it is X’s turn to move. How do we assign values to each of X’s possible three moves


Figure: Alpha-beta algorithm applied to part of a game of tic-tac-toe


In Figure above,  we continue to search by making each of X’s possible moves and storing each possible board position for level 2. We keep recursively applying this algorithm until we either reach a maximum search depth, or there is a win, loss, or draw detected in a generated move. We assume that there is a fitness function available that rates a given board position relative to either side. Note that the value of any board position for X is the negative of the value for O. To make the search more efficient, we maintain values for alpha and beta for each search level. Alpha and beta determine the best possible/worst possible move available at a given level. If we reach a situation like the second position in level 2 where X has won, then we can immediately determine that O’s last move in level 1 that produced this position (of allowing X an instant win) is a low valued move for O (but a high valued move for X). This allows us to immediately “prune” the search tree by ignoring all other possible positions arising from the first O move in level

1. This alpha-beta cutoff (or tree pruning) procedure can save a large percentage of search time, especially if we can set the search order at each level with “probably best” moves considered first.

While tree diagrams as seen in Figure  quickly get complicated, it is easy for a computer program to generate possible moves, calculate new possible board positions and temporarily store them, and recursively apply the same procedure to the next search level (but switching min-max “sides” in the board evaluation). We will see in the next section that it only requires about 100 lines of Java code to implement an abstract class framework for handling the details of performing an alpha-beta enhanced
search. The additional game specific classes for tic-tac-toe require about an additional 150 lines of code to implement; chess requires an additional 450 lines of code.

How to Find Paths in Graphs using Artificial Intelligence algorothm Java programming



Finding Paths in Graphs:

      The common type of search space is represented by a graph. A graph is a set of nodes and links.
We characterize nodes as containing the following data:


  • A name and/or other data.
  • Zero or more links to other nodes
  • A position in space (this is optional, usually for display or visualization purposes)
     Links between nodes are often called edges. The algorithms used for finding paths in graphs are very similar to finding paths in a two-dimensional maze. The primary difference is the operators that allow us to move from one node to another. In the last section we saw that in a maze, an agent can move from one grid space to another if the target space is empty. For graph search, a movement operator allows movement to another node if there is a link to the target node.
Fig: UML class diagram for the graph search classes

         As seen in Figure, most of the data for the search operations (i.e., nodes, links, etc.) is defined in the abstract class AbstractGraphSearch. This abstract class is customized through inheritance to use a stack for storing possible moves (i.e., the array path) for depth-first search and a queue for breadth-first search. The abstract class AbstractGraphSearch allocates data required by both derived classes:


final public static int MAX = 50;

protected int [] path = new int[AbstractGraphSearch.MAX];

protected int num_path = 0;

// for nodes:

protected String [] nodeNames =  new String[MAX];

protected int [] node_x = new int[MAX];

protected int [] node_y = new int[MAX];

 // for links between nodes:

protected int [] link_1 = new int[MAX];

protected int [] link_2 = new int[MAX];

protected int [] lengths = new int[MAX];

protected int numNodes = 0;

protected int numLinks = 0;

protected int goalNodeIndex = -1,

startNodeIndex = -1;


Saturday, 2 December 2017

Which Artificial Intelligence Libraries are used in Java programming



Artificial Intelligence Libraries:


Since this article is about libraries, AI is a very wide field so we will be focusing on the most popular fields today like Natural Language Processing, Machine Learning, Neural Networks and more. In the end, we’ll mention few interesting AI challenges where you can practice your understanding of AI.


1. Apache Jena
Apache Jena is an open source Java framework for building semantic web and linked data applications from RDF data. The official website provides a detailed tutorial on how to use this framework with a quick introduction to RDF specification.

2. PowerLoom Knowledge Representation and Reasoning System

PowerLoom is a platform for the creation of intelligent, knowledge-based applications. It provides Java API with detailed documentation which can be found at this link.

3. d3web 

d3web is an open source reasoning engine for developing, testing and applying problem-solving knowledge to a given problem situation, with many algorithms already included. The official website provides a quick introduction to the platform with many examples and documentation.

4. Eye

An Eye is an open source reasoning engine for performing semi-backward reasoning.

5. Tweety

Tweety is a collection of Java frameworks for logical aspects of AI and knowledge representation. The official website provides documentation and many examples.

# Neural Networks

1. Neuroph

Neuroph is an open source Java framework for neural network creation. Users can create networks through provided GUI or Java code. Neuroph provides API documentation which also explains what neural network actually is and how it works.

2. Deeplearning4j

Deeplearning4j is a deep learning library for JVM but it also provides API for neural network creation. The official website provides many tutorials and simple theoretical explanations for deep learning and neural networks.

# Natural Language Processing

1. Apache OpenNLP

Apache OpenNLP library is a machine learning based toolkit for the processing of natural language text. The official website provides API documentation with information on how to use the library.

2. Stanford CoreNLP

Stanford CoreNLP is the most popular Java NLP framework which provides various tools for performing NLP tasks. The official website provides tutorials and documentation with information on how to use this framework.

# Machine Learning

1. Java Machine Learning Library (Java-ML)

Java-ML is an open source Java framework which provides various machine learning algorithms specifically for programmers. The official website provides API documentation with many code samples and tutorials.

2. RapidMiner

RapidMiner is a data science platform which provides various machine learning algorithms through GUI and Java API. It has a very big community, many available tutorials, and an extensive documentation.

3. Weka 

Weka is a collection of machine learning algorithms which can be applied directly to the dataset, through the provided GUI or called through the provided API. Similar as for RapidMiner, a community is very big, providing various tutorials for Weka and machine learning itself.

4. Encog Machine Learning Framework

Encong is a Java machine learning framework which supports many machine learning algorithms. It’s developed by Jeff Heaton from Heaton Research. The official website provides documentation and many examples.

# Genetic Algorithms

1. Jenetics 

Jenetics is an advanced genetic algorithm written in Java. It provides a clear separation of the genetic algorithm concepts. The official website provides documentation and a user guide for new users.

2. Watchmaker Framework

Watchmaker Framework is a framework for implementing genetic algorithms in Java. The official website provides documentation, examples, and additional information about the framework itself.

# ECJ 23

ECJ 23 is a Java based research framework with strong algorithmic support for genetic algorithms. ECJ is developed at George Mason University’s ECLab Evolutionary Computation Laboratory. The official website provides extensive documentation and tutorials.

1. Java Genetic Algorithms Package (JGAP)

JGAP is a genetic programming component provided as a Java framework. The official website provides documentation and tutorials.

2. Eva

Eva is a simple Java OOP evolutionary algorithm framework.

#Automatic programming

1. Spring Roo

Spring Roo is a lightweight developer tool from Spring. It’s using AspectJ mixins to provide separation of concerns during round-trip maintenance.

2. Acceleo

Acceleo is an open source code generator for Eclipse which generates code from EMF models defined from any metamodel (UML, SysML, etc.).


How to Use Java Framework for Search and Game Playing in Artificial Intelligence



A Java Framework for Search and Game Playing:

               The general interface for the Java classes that we will develop in this section was inspired by the Common LISP game-playing framework written by Kevin Knight and described in (Rich, Knight 1991). The abstract class GameSearch contains the code for running a two-player game and performing an alpha-beta search. This class needs to be sub-classed to provide the eight methods:


public abstract boolean drawnPosition(Position p) 

public abstract boolean wonPosition(Position p, boolean player) 

positionEvaluation(Position p, boolean player) 

public abstract void printPosition(Position p) 

public abstract Position [ ] 

possibleMoves(Position p, boolean player) 

public abstract Position makeMove(Position p, boolean player, Move move) 

public abstract boolean reachedMaxDepth(Position p, int depth) 

public abstract Move getMove() 


              The method drawnP osition should return a Boolean true value if the given position evaluates to a draw situation. The method wonP osition should return a true value if the input position is won for the indicated player. By convention, I use a Boolean true value to represent the computer and a Boolean false value to represent the human opponent. The method positionEvaluation returns a position evaluation for a specified board position and player. Note that if we call positionEvaluation switching the player for the same board position, then the value returned is the negative of the value calculated for the opposing player. The method possibleMoves returns an array of objects belonging to the class Position. In an actual game like chess, the position objects will actually belong to a chess-specific refinement of the Position class (e.g., for the chess program developed later in this chapter, the method possibleMoves will return an array of ChessP osition objects). The method makeMove will return a new position object for a specified board position, side to move, and move.

                   The method reachedM axDepth returns a Boolean true value if the search process has reached a satisfactory depth. For the tic-tac-toe program, the method reachedM axDepth does not return true unless either side has won the game or the board is full; for the chess program, the method reachedM axDepth returns true if the search has reached a depth of 4 half moves deep (this is not the best strategy, but it has the advantage of making the example program short and easy to understand). The method getMove returns an object of a class derived from the class Move (e.g., Tic Tac Toe Move or Chess Move).


The GameSearch class implements the following methods to perform game search: 

protected Vector alphaBeta(int depth, Position p, boolean player) 

protected Vector alphaBetaHelper(int depth, Position p, boolean player, float alpha, float beta) 

public void playGame(Position startingPosition, boolean humanPlayFirst) 


How to find Paths and Mazes in Artificial Intelligence Algorithm

Find Paths and Mazes:



Fig:A directed graph representation is shown on the left and a twodimensional grid (or maze) representation is shown on the right. In both representations, the letter R is used to represent the current position (or reference point) and the arrowheads indicate legal moves generated by a search operator.


          The example program used in this section is MazeSearch.java in the directory src/search/maze and I assume that the reader has downloaded the entire example ZIP file for this book and placed the source files for the examples in a convenient place.

           Figure shows the UML class diagram for the maze search classes: depth first and breadth first search. The abstract base class AbstractSearchEngine contains common code and data that is required by both the classes DepthF irstSearch and BreadthF irstSearch. The class M aze is used to record the data for a twodimensional maze, including which grid locations contain walls or obstacles. The class M aze defines three static short integer values used to indicate obstacles, the starting location, and the ending location. The Java class M aze defines the search space. 

          This class allocates a two-dimensional array of short integers to represent the state of any grid location in the maze. Whenever we need to store a pair of integers, we will use an instance of the standard Java class java.awt.Dimension, which has two integer data components: width and height. Whenever we need to store an x-y grid location, we create a new Dimension object (if required), and store the x coordinate in Dimension.width and the y coordinate in Dimension.height. As in the right-hand side of Figure 2.1, the operator for moving through the search space from given x-y coordinates allows a transition to any adjacent grid location that is empty. The Maze class also contains the x-y location for the starting location (startLoc) and goal location (goalLoc). Note that for these examples, the class Maze sets the starting location to grid coordinates 0-0 (upper left corner of the maze in the figures to follow) and the goal node in (width - 1)-(height - 1) (lower right corner in the following figures).

How to use Representation of Search State Space and Search Operators



Representation of Search State Space and Search Operators In Details:


               We will use a single search tree representation in graph search and maze search examples in this chapter. Search trees consist of nodes that define locations in state space and links to other nodes. For some small problems, the search tree can be easily specified statically; for example, when performing the search in game mazes, we can compute and save a search tree for the entire state space of the maze. For many problems, it is impossible to completely enumerate a search tree for a state space so we must define successor node search operators that for a given node produce all nodes that can be reached from the current node in one step; for example, in the 5 2 Search game of chess we can not possibly enumerate the search tree for all possible games of chess, so we define a successor node search operator that given a board position (represented by a node in the search tree) calculates all possible moves for either the white or black pieces. The possible chess moves are calculated by a successor node search operator and are represented by newly calculated nodes that are linked to the previous node. Note that even when it is simple to fully enumerate a search tree, as in the game maze example, we still might want to generate the search tree dynamically as we will do in this chapter).

For calculating a search tree we use a graph. We will represent graphs as a node with links between some of the nodes. For solving puzzles and for game related search, we will represent positions in the search space with Java objects called nodes. Nodes contain arrays of references to both child and parent nodes. A search space using this node representation can be viewed as a directed graph or a tree. The node that has no parent nodes is the root node and all nodes that have no child nodes a called leaf nodes.

Search operators are used to move from one point in the search space to another. We deal with quantized search spaces in this chapter, but search spaces can also be continuous in some applications. Often search spaces are either very large or are infinite. In these cases, we implicitly define a search space using some algorithm for extending the space from our reference position in the space. Figure 2.1 shows representations of search space as both connected nodes in a graph and as a two-dimensional grid with arrows indicating possible movement from a reference point denoted by R. When we specify a search space as a two-dimensional array, search operators will move the point of reference in the search space from a specific grid location to an adjoining grid location. For some applications, search operators are limited to moving up/down/left/right and in other applications operators can additionally move the reference location diagonally. When we specify a search space using node representation, search operators can move the reference point down to any child node or up to the parent node. For search spaces that are represented implicitly, search operators are also responsible for determining legal child nodes, if any, from the reference point.


How To Create SEARCH And Algorithm Using Java Programming



Search:

Early AI research emphasized the optimization of search algorithms. This approach made a lot of

 sense because many AI tasks can be solved effectively by defining state spaces and using search

algorithms to define and explore search trees in this state space. Search programs were frequently

made tractable by using heuristics to limit areas of search in these search trees. This use of heuristics

converts intractable problems to solvable problems by compromising the quality of solutions; this

trade off of less computational complexity for less than optimal solutions has become a standard

design pattern for AI programming. We will see in this chapter that we trade off memory for faster

computation time and better results; often, by storing extra data we can make search time faster, and

make future searches in the same search space even more efficient.


What are the limitations of search? Early on, search applied to problems like checkers and chess

misled early researchers into underestimating the extreme difficulty of writing software that performs

 tasks in domains that require general world knowledge or deal with complex and changing

environments. These types of problems usually require the understanding and then the

implementation of domain specific knowledge. In this chapter, we will use three search problem

domains for studying search algorithms: pat

In this chapter, we will use three search problem domains for studying search algorithms: path

finding in a maze, path finding in a graph, and alpha-beta search in the games tic-tac-toe and chess.