ai.ccalc - jgrey4296/jgrey4296.github.io GitHub Wiki

CCalc

Main

Infrastructure

# start swipl6 with ccal loaded:
swipl6 -f path/to/ccalc.pl

System Dependencies

Needs swi-prolog < 7 So rebuild from the git ref 406202420c of swipl

On mac: You need to update submodules first before going off HEAD. Make sure to add the compiler flags brew info readline suggests. Solvers need to be put in a os specific (eg: Darwin) solvers subdir. Homebrew install specific version

solvers

For Solvers, use relsat. Walksat also compiles but doesn’t output solutions.

Syntax

Fluents and Actions

Types:

  • simpleFluent.
  • inertialFluent.
  • additiveFluent(type).
  • sdFluent
  • ? rigid
  • action.
  • exogenousAction.
  • attribute of Action.
  • additiveAction(type).
  • ? abAction

#+NAME: Simple example

:- constants
p :: inertialFluent(0..3);
a :: exogenousAction.

Transitions

Define movements between states, using actions underlying language is “caused X if Y”

constraint #1,#2 -> (constraint #1 ; constraint #2) ; constraint #1 -> caused false if -(#1);always #1,#2 -> (always #1 ; always #2) ; always #1 -> caused false after -(#1) ; % or, constraint false after #1.default #1,#2 if #3 after #4 -> (default #1 if #3 after #4; default #2 if #3 after #4) ; default #1 if #2 after #3 -> caused #1 if #1 & #2 after #3 ; default #1,#2 after #3 -> (default #1 after #3 ; default #2 after #3) ; default #1 after #2 -> (default #1 if true after #2) ; default #1,#2 if #3 -> (default #1 if #3 ; default #2 if #3) ; default #1 if #2 -> caused #1 if #1 & #2 ; default #1,#2 -> default #1,#2 if true ; default #1 -> default #1 if true ;exogenous #1,#2 if #3 -> (exogenous #1 if #3 ; exogenous #2 if #3) ; exogenous #1 if #2 -> default #1 = var(sort(#1),-1) if #2 ; exogenous #1,#2 -> (exogenous #1 ; exogenous #2) ; exogenous #1 -> exogenous #1 if true .caused #1 after #3 -> caused #1 if true after #3 ;

possibly caused #1 if #2 after #3 -> default #1 if #2 after #3 ; possibly caused #1 after #2 -> default #1 after #2;

#1 causes #2 if #3 -> caused #2 after #1 & #3 where action_formula(#1), simpleFluent_formula(#2) ;

#1 causes #2 if #3 -> caused #2 if #1 & #3 where action_formula(#1), action_formula(#2), fluent_formula(#3) ;

#1 causes #2 -> #1 causes #2 if true where action_formula(#1);

#1 may cause #2 if #3 -> default #2 after #3 & #4 where tuple_to_conjunct(#1,#4) ;

#1 may cause #2 -> default #2 after #3 where tuple_to_conjunct(#1,#3) ;

nonexecutable #1 if #2 -> always #2 ->> -(#3) where tuple_to_conjunct(#1,#3);

nonexecutable #1 -> nonexecutable #1 if true;

inertial #1,#2 if #3 -> (inertial #1 if #3 ; inertial #2 if #3) ; inertial #1 if #2 -> default #1 = var(sort(#1),-1) after #1 = var(sort(#1),-1) & #2 ;

inertial #1,#2 -> (inertial #1 ; inertial #2) ; inertial #1 -> inertial #1 if true ;

rigid #1,#2 -> (rigid #1 ; rigid #2) where is_constant(#1) ; rigid #1 -> caused false if -(#1 = var(sort(#1),-1)) after #1 = var(sort(#1),-1) where is_constant(#1).

#+NAME: Simple Actions

a causes c.
a causes c=1 if c=0.
nonexecutable a if c=3.

Variables

Uppercase as standard in prolog.

#+NAME: Simple Variables

:- variables
I :: 0..2.

a causes c=I+1 if c=I.

Macros

#+NAME: Simple Macros

:- macros
n -> 3.

:- constants
p :: inertialFluent(0..n).

Queries

Defined as paths of node -> action -> node of maxstep length.

#+NAME: Simple Queries

%% Calls for finding paths of length 1.
:- query
maxstep :: 1.

Add requirements at particular nodes or edges.

:- query
maxstep :: 2;
0 : c=5; %% '& a'; or ', a;'
0 : a;
1 : a;
maxstep :: 1.

Imports

#+NAME: Import example

	:- include 'file.prolog'.
 %% Any definitions in file are now usable

Sandbox

#+NAME: Non-Session Test

:- macros
   n -> 10.

:- constants
   c :: inertialFluent(0..n);
   a ,
   b :: exogenousAction.

:- variables
   I :: 0..n-1.

a causes c=I+1 if c=I.
b causes c=I+2 if c=I, I+2 < n.

nonexecutable a if c >= n.
nonexecutable b if c >= n-2.

:- query          % prediction
   label :: 1;
   maxstep :: 2;
   0: c=5;
   0: a;
   1: a.

:- query          % postdiction
   label :: 2;
   maxstep :: 2;
   maxstep: c=5;
   0: a;
   1: a.

:- query          % planning
   label :: 3;
   maxstep :: 0..infinity;
   0: c=4;
   maxstep: c=10.

#+NAME: test inertial

:- macros
   n -> 2.

:- sorts
   object.

:- objects
   first, second :: object.

:- constants
   c             :: inertialFluent(0..n);
   d             :: simpleFluent(object);
   a             :: exogenousAction.

:- variables
   I :: 0..n-1.

a causes c=I+1 if c=I.

default d=first after d=second.
default d=second after d=first.

:- query
label :: 1;
maxstep :: 2;
0 : c=0;
1 : a;
maxstep: c=2.

#+NAME: test action

   %% noconcurrency.

   :- macros
      n -> 15.

   :- sorts
      object.

   :- objects
      first, second:: object.

   :- constants
      c :: inertialFluent(0..n);
      d :: simpleFluent(object);
      g :: simpleFluent(boolean);
      a, q :: exogenousAction;
      b :: exogenousAction.

   :- variables
      I :: 0..n-1;
      T :: integer;
      Q :: exogenousAction.

   a causes c=I+2 if c=I, I<n-2.
   q causes c=I-1 if c=I, I>0.

   a causes b.
   b causes a.
   b causes d=second.
   q causes d=first.

   % a and q are mutually exclusive:
   nonexecutable a & q.
   nonexecutable b & q.
   nonexecutable b if not a.

   % non-inertial tick tock
   default d=first after d=second.
   default d=second after d=first.

   default g=false.
   default g=true if c=9.
   default g=true if c=3.

   :- show
   c; a; q; b; g.

   :- query
   label :: 1;
   maxstep :: 8;
   0 : c=0;
   1 : a;
   3 : c>4;
   maxstep : g=true.

:- query
   label :: 2;
   maxstep :: 3;
   0 : c=0;
   1 : a;
   maxstep : g=true.

%% noconcurrency.
:- macros
   n -> 15;
   all_fruit -> apple, pear.

:- sorts
   fruit.

:- objects
   all_fruit :: fruit.

:- constants
   %% Inertial Fluents
   fruit_count(fruit) :: inertialFluent(0..n);
   bought_fruit :: inertialFluent.

:- constants
   %% Simple Fluents
   has_fruit :: simpleFluent;
   no_fruit :: simpleFluent.

:- constants
   %% Actions
   eat(fruit) :: exogenousAction;
   buy(fruit) :: exogenousAction.

:- variables
   I,J :: integer;
   F,G :: fruit.

eat(F) causes fruit_count(F)=I if fruit_count(F)=I+1, I+1<n.
buy(F) causes fruit_count(F)=I+1 if fruit_count(F)=I, I+1<n.
buy(F) causes bought_fruit.

nonexecutable eat(F) & eat(G) if F\=G.
nonexecutable eat(F) if fruit_count(F)=0.
nonexecutable buy(F) if fruit_count(F)=n.

default -has_fruit.
caused has_fruit if fruit_count(F)>0.

default -no_fruit.
caused no_fruit if -has_fruit.

:- show
apple; pear; fruit_count; eat; buy; bought_fruit; no_fruit; has_fruit; no_fruit.

:- query
label :: 1;
maxstep :: 5;
0 : fruit_count(apple)=1, fruit_count(pear)=1;
1 : eat(apple);
2 : eat(pear);
3 : buy(apple);
maxstep: no_fruit.

#+NAME: additive

   %% noconcurrency.

:- maxAdditive :: 7.

:- sorts
   agent;
   item.

:- variables
   It                          :: item;
   M,N                         :: 0..maxAdditive.

:- objects
   buyer              :: agent;
   newspaper,magazine :: item.

:- constants
   sold :: inertialFluent;
   has(agent,item)             :: additiveFluent(0..maxAdditive);
   buy(item)                   :: exogenousAction;
   sell(item)                  :: exogenousAction.

default -sold.
sell(It) causes sold.

buy(It) increments has(buyer,It) by 2.
sell(It) decrements has(buyer,It) by 1.

nonexecutable buy(It) if sell(It).
nonexecutable sell(It) if buy(It).

:- query
   label :: 1;
   maxstep :: 5;
   0: has(buyer, newspaper)=4,
      has(buyer, magazine)=2,
      buy(newspaper),
      buy(magazine);
   maxstep: has(buyer, newspaper)=5, has(buyer, magazine)=3.

Sandbox Session

#+NAME: Basic example

:- macros
   n -> 10.

:- constants
   c :: inertialFluent(0..n);
   a ,
   b :: exogenousAction.

:- variables
   I :: 0..n-1.

a causes c=I+1 if c=I.
b causes c=I+2 if c=I, I+2 < n.

nonexecutable a if c >= n.
nonexecutable b if c >= n-2.

:- query          % prediction
   label :: 1;
   maxstep :: 2;
   0: c=5;
   0: a;
   1: a.

:- query          % postdiction
   label :: 2;
   maxstep :: 2;
   maxstep: c=5;
   0: a;
   1: a.

:- query          % planning
   label :: 3;
   maxstep :: 0..infinity;
   0: c=4;
   maxstep: c=10.

#+NAME: Coins Session Test

loadf 'coins.prolog'.
query 2.

#+NAME: Mytest Session

noconcurrency.

:- constants
a :: exogenousAction;
b :: exogenousAction;
q :: action;
c, d, e :: inertialFluent.

a causes c.
caused d if c.
b causes -d, -c.
caused q if d.
q causes e.
%% caused a if d.

nonexecutable a if c.
nonexecutable b if -c.

:- query
label :: 1;
maxstep :: 4;
0: a, -d, -c;
2: b.

query 1.
:- sorts
 blah.

 :- objects
 h :: blah;
 k :: blah.

 :- variables
 I :: 1..5;
 J :: blah;
 N :: 0..3.

 :- constants
 a :: inertialFluent;
 c :: exogenousAction;
 d :: inertialFluent.
 %% b :: inertialFluent(blah);
 %% d :: sdFluent;
 %% e :: boolean;
 %% f :: action;
 %% g :: exogenousAction;
 %% h :: attribute of g;
 %% i :: additiveAction(integer).

 %% default -a.
 %% default b.
 %% default b=h.
 %% default a=0.

 %% nonexecutable c(J) if a(J)=5.
 c causes d.

 :- query
 label :: 1;
 maxstep :: 2.

query 1.
show_rules.
show_clauses.

Zoo

Giunchiglia

Akman

Lifschitz

Lee and Lifschitz

Other

Links

⚠️ **GitHub.com Fallback** ⚠️