CLIPS - Adding the slots of two facts - add

I am fairly new to CLIPS , been trying to implement a program , based on a lecture , that adds the values of 2 slots , of two facts respectively. I begin with defining the templates of o1 , o2 (the two facts that we add) and o3 (where we store the result) . Each of the first two facts , o1 and o2 , have 2 slots , s1 and s2 . The slots of o3 are r1 and r2 . Then I proceed to initialize the slots o1 and o2 , using deffacts . Then , ending , using defrule , I set variables to the slots of o1 and o2 and using assert to generate the o3 fact , which slots r1 will have the sum of values of the first slot of the facts o1 and o2 , and r2 will the sum of the second . I'm using ubuntu , loading the clp clips file with clips -f try.clp command , and then executing (run) and at last , (facts) , but can't see the new facts , or either of the pre-existing ones . Can someone guide me with this ? I'm posting my code and screenshot of running the program.
Here is my code :
(deftemplate o1
(slot s1)
(slot s2))
(deftemplate o2
(slot s1)
(slot s2))
(deftemplate o3
(slot r1)
(slot r2))
; Facts
(deffacts F
(o1 (s1 3)(s2 5))
(o2 (s1 7)(s2 9)))
; Rules
(defrule R
(o1 (s1 ?x1)(s2 ?y1))
(o2 (s1 ?x2)(s2 ?y2))
=>
(assert (o3(r1 (+ ?x1 ?x2))(r2 (+ ?y1 ?y2))))
)
and screenshot with running the program :

You need to issue a (reset) command before your (run) command. Reset removes all existing facts and then asserts the facts specified in your deffacts.

Related

correlation vs causation (A cor B => ∃ D such that DcausA and DcausB)?

As explained in the title , I wonder how false/true this statement is :
if A and B are correlated , there must be some variable D that has a causation relationship with both B and A .
example : (A : ice cream , B: sun-screen , D : sun)
Intuitively it seem to me that it is true , if we suppose that the statement is false , i feel that the reasoning will lead to some contradiction ( A and B should be correlated by pure chance , without any latent variable that explains both )
Lets conduct this reasoning
suppose A and B are correlated , there must be a Z that causes A (under the assumption that we live in a world of cause and effect - let's not take into account weird observations done in quantum mechanics that are against causality )
In the same way B has some variables Wi that causes it
saying that there is no hidden causation implies that the intersection of set 1(vars that cause A) and set 2 (vars that cause B) is None
to me this sounds very unlikely , since it implies a world where many entities evolve with no interactions , to me this view , and the butterfly effect can not exist in the same world
Thank you for taking time to read this , and helping me push this thought further .

Inserting a regular language into other regular language

Let L1 and L2 be the regular languages over the alphabet {a,b}. We define the language L3 as follows:
L3 = {pqr | pr ∈ L1, q ∈ L2}
L3 is obtained by inserting a string from L2 inside a string from L1. Is language L3 is still regular or not?
I am trying to solve this problem. Is it possible to prove this using string substitution property or homomorphism of regular language? Is there any better and easier way to prove this?
Here is a high-level description of a construction to show there is an NFA accepting L3.
Let M1 and M2 be minimal DFAs such that L(M1) = L1 and L(M2) = L2. Copy M1 so there are two copies, M1[1] and M1[2]. Copy M2 so there are |Q1| copies M2[1], M2[2], …, M2[|Q1|]. Also, number the states q1, q2, …, q|Q1| of M1. Now construct an NFA for M3 as follows:
From state qk of M1[1], add a lambda/epsilon transition to the initial state of M2[k]
From the accepting state(s) of M2[k], add lambda/epsilon transitions to state qk of M1[2]
The accepting states are the accepting states of M1[2]
The initial state is the initial state of M1[1].
This NFA reads some input and then nondeterministically jumps to an instance of M2. It then reads a string in M2 and jumps back to where it left off in the next copy of M1 where it can accept. This NFA has a number of states equal to 2|Q1| + |Q1| * |Q2|.

how to make an optimal combinatorial selection in R

The problem I'm trying to solve is basically the same as the one in this post:
https://stats.stackexchange.com/questions/339935/python-library-for-combinatorial-optimization
And my current implementation uses indeed a genetic algorithm based optimizer.
However, I would like to solve it as a binary linear programming problem (at least try, even though it's 'NP-hard', apparently).
My question is how to formulate the LP in the best way, because I am not sure I am doing it right.
The following is a simplified version of what I'm dealing with, which however shows exactly where the problem lies.
We make m*n (in this case 6) objects by a combinatorial process taking m (3) objects of type 'R1' (say {A,B,C}) and n (2) objects of type 'R2' (say {X,Y}).
The 6 objects {AX,AY,BX,BY,CX,CY} are evaluated and each gets a score D, in this case {0.8,0.7,0.5,0.9,0.4,0.0}, in this order.
CL <- cbind(expand.grid(R2=LETTERS[24:25],R1=LETTERS[1:3],stringsAsFactors = FALSE),D=c(0.8,0.7,0.5,0.9,0.4,0.0))
Now we want to select 2 distinct R1's and 1 R2 such that the sum of D is maximal.
In this example, the answer is R1 = {A,B}, R2 = {Y}.
However, one would not get to such conclusion taking, for instance, the 2 R1's and the R2 with the highest average D.
It would work for R1, but not for R2:
aggregate(D~R1,CL,mean)
# R1 D
#1 A 0.75
#2 B 0.70
#3 C 0.20
aggregate(D~R2,CL,mean)
# R2 D
#1 X 0.5666667
#2 Y 0.5333333
I know how to formulate this as a linear programming problem; only I am not sure my formulation is efficient, because basically it results in a problem with mn+m+n variables and 2(m+n)+2 constraints.
The main difficulty is that I need somehow to count the number of distinct R1's and R2's chosen, and I don't know any way of doing that apart from what I will show below (and is also described in my other post here).
This is what I would do:
CL["Entry"] <- seq_len(dim(CL)[[1]])
R1.mat <- table(CL$R1,CL$Entry)
R2.mat <- table(CL$R2,CL$Entry)
N_R1 <- dim(R1.mat)[[1]]
N_R2 <- dim(R2.mat)[[1]]
N_Entry <- dim(CL)[[1]]
constr.mat <- NULL
dir <- NULL
rhs <- NULL
constr.mat <- rbind(constr.mat,cbind(R1.mat,-diag(table(CL$R1)),matrix(0,N_R1,N_R2)))
dir <- c(dir,rep("<=",N_R1))
rhs <- c(rhs,rep(0,N_R1))
constr.mat <- rbind(constr.mat,cbind(R2.mat,matrix(0,N_R2,N_R1),-diag(table(CL$R2))))
dir <- c(dir,rep("<=",N_R2))
rhs <- c(rhs,rep(0,N_R2))
constr.mat <- rbind(constr.mat,constr.mat)
dir <- c(dir,rep(">=",N_R1+N_R2))
rhs <- c(rhs,1-table(CL$R1),1-table(CL$R2))
constr.mat <- rbind(constr.mat,c(rep(0,N_Entry),rep(1,N_R1),rep(0,N_R2)))
dir <- c(dir,"==")
rhs <- c(rhs,2)
constr.mat <- rbind(constr.mat,c(rep(0,N_Entry),rep(0,N_R1),rep(1,N_R2)))
dir <- c(dir,"==")
rhs <- c(rhs,1)
obj <- c(aggregate(D~Entry,CL,c)[["D"]],rep(0,N_R1+N_R2))
Which can be solved for instance by lpSolve:
sol <- lp("max", obj, constr.mat, dir, rhs, all.bin = TRUE,num.bin.solns = 1, use.rw=FALSE, transpose.constr=TRUE)
sol$solution
#[1] 0 1 0 1 0 0 1 1 0 0 1
showing that products {AY,BY} were selected, corresponding to R1 = {A,B} and R2 = {Y}:
CL[as.logical(sol$solution[1:N_Entry]),]
# R2 R1 D Entry
#2 Y A 0.7 2
#4 Y B 0.9 4
I found that on large problems lpSolve gets stuck for ages; Rsymphony seemed to perform better.
But again, my main question is: is this way of formulating the LP efficient? Should I do it differently?
Thanks!
EDIT
In the meantime, working on a somewhat related problem, I found that only one set of constraints may be sufficient, if one adds 'costs' (in this example, negative) to the objective vector for the 'distinct R1 and R2' variables.
So here, instead of:
obj <- c(aggregate(D~Entry,CL,c)[["D"]],rep(0,N_R1+N_R2))
I would do:
obj <- c(aggregate(D~Entry,CL,c)[["D"]],rep(-1,N_R1+N_R2))
This would make m+n constraints unnecessary.
It still remains a huge problem to solve, even for relatively small m, n, so if anyone can advise how to do it better...
I had a look at lp.transport, but that would be limited to 2 dimensions (i.e. only R1 and R2, not R1, R2, R3 for instance), and I don't think you can constrain the number of distinct objects per category in that kind of solver.

Showing that the intersection of two language accepted by NFA is undecidable

I am having problem with this problem.
Let A = {〈N1, N2〉 | N1 and N2 are NFAs and L(N1) ∩ L(N2) =∅}. Show that A is decidable.
Any help is appreciated.
Given an input , here is an algorithm that determines whether L(N1) ∩ L(N2) =∅:
determinize N1 and N2 into D1 and D2 using the powerset construction. slow, but effective.
intersect D1 and D2 into M using the Cartesian product machine construction.
minimize M into M' using some DFA minimization algorithm
see whether M' has an accepting state. if so, halt-reject; otherwise, halt-accept.
This is an effectively computable procedure for determining inclusion and/or exclusion from the set, so the set is decidable.

haskell card filter [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a group of cards
initialCards :: GameState
initialCards = [[Card s1 r1, Card s2 r2] | s1 <- [minBound .. maxBound]::[Suit], r1 <- [minBound.. maxBound]::[Rank],
s2 <- [minBound .. maxBound]::[Suit], r2 <- [minBound .. maxBound]::[Rank],
s1 /= s2, r1 /= r2]
for example , i want to keep the [Card], which have rank Suit and Diamond,
how can i write the code.
You use the filter function on the list of cards and supply it with a function that maps Suits and Diamonds to True.

Resources