Candidate elimination algorithm lecture example - algorithm

I am looking through some lecture slides and cannot understand why the bold hypothesis at last G are just discarded, I can come to the same answer but don't understand why they're just discarded.
sky temperature humidity
| | | | | |
Sunny Rainy Warm Coo Normal Low
and the set of positive and negative training examples:
1. ( S W N )+)
2. ( R C L )-)
3 . ( S C N )+)
4. ( S W L )-)
Training with the first example: ( S W N ) +) generalizing…
G = [( ? ? ? )]
S = [( S W N )]
Training with the second example: ( R C L ) -) specializing…
G = [( S ? ? ) ( ? W ? ) ( ? ? N )
S = [( S W N )]
Training with the third example: ( S C N ) +) generalizing…
G = [( S ? ? )( ? ? N )] (the other is discarded )
S = [( S ? N )]
Training with the fourth example: ( S W L ) -) specializing…
G = [( S C ? )( S ? N )( R ? N )(? C N)] (bold are discarded )
S = [( S ? N )]
Convergence, the learned concept must be: [( S ? N )]

G = [( S C ? )( S ? N )( R ? N )(? C N)] (bold are discarded )
It can be simply using the candidate elimination algorithm. According to that the reasons can be summarized as follows.
Inconsistent hypothesis: According to the algorithm we have to first remove the hypotheses which are not consistent with target data(D)
In this case ( R ? N ) is removed it's inconsistent with ( S ? N )
Specific boundary being more general than the general boundary.
If the specific boundy become more specific that the general one. There can be a boundary overlapping.
if we compare derived ( S C ? ) with ( S ? N ) , we can compare middle c with ? of (S ? N). The derived one having a constant makes it more specific compared to the specific boundary. So it should be removed. Same goes with (? C N).
I see the question is bit older but I hope someone would find this useful.

Related

newton square root algorithm condition confusion?

the algorithm is pretty straight forward I implemented it as soon as I saw newtons equation
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
now here is the thing on really small values the algorithm gives a way off answer and on really large values the algorithm exceeds the call stack .
I understand why
what I don't understand is in the first condition ..
if(Math.abs(g*g -x) < e ) why!! if we divide by x before comparing solves the problem e.g:
if(Math.abs(g*g -x) / x < e)
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) / x < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
call the function like this first arg is the number you wanna compute the square root of , second is epsilon which is the range is which when I get a value should be acceptable , you could define an initial guess as a third argument
e.g:
sq( 9 , 0.01)
or:
sq(9 , 0.01 , 2)
Typically, you would specify ε as a fraction of g, not x, since normally you would want to say that the result has some precision ("six digits", for example) which is necessarily​ relative to the result. But it doesn't make much difference aside from interpreting the meaning of the ε parameter.
It is certainly the case that choosing some absolute error threshold makes no sense unless you know that the possible arguments are within a very restricted set of values.

Recursive function in haskell

I am trying to solve some issues with programming in haskell.
I am having this three types and the Hanoi-algorithm:
type Position = Int
type Move = (Position,Position)
type Towers = ([Int],[Int],[Int])
hanoi 1 i j = [(i,j)]
hanoi n i j = hanoi n' i otherT ++ [(i,j)] ++ hanoi n' otherT j
where
n' = n-1
otherT = 1+2+3-i-j -- other tower
Now I wrote a function with makes one single MOVE.
move ::([Move],Towers) -> ([Move],Towers)
move ::([Move],Towers) -> ([Move],Towers)
move ([],(xs,ys,zs) ) = ((error "Error"),(xs,ys,zs) )
move (((a,b): tail), (xs,ys,zs) )
| a > 3 = (tail, ((error "Error"),ys,zs) )
| b > 3 = (tail, ((error "Error"),ys,zs ) )
| otherwise = hilfsfunktion (((a,b): tail), (xs,ys,zs) )
hilfsfunktion (((1,2): tail), ((x:xs),(y:ys),zs) )
| x < y = (tail, (xs, (x:y:ys),zs) )
| x > y = (tail, (xs, (error "too big"),(error "too big")))
hilfsfunktion (((1,2): tail), ((x:xs), [],zs) ) = (tail, (xs, x:[],zs) )
The function is much longer, but you can see that I solved this with Pattern Matching.
Now I try to write a function, that called this "move"-function as long as the list is not empty. So at first I used Pattern Matching for the case, that the list is empty.
all_moves:: ([Move], Towers) -> Towers
all_moves ([],(xs,ys,zs) ) = (xs,ys,zs)
all_moves (((a,b): tail), (xs,ys,zs) ) = ????
Now I need some help, in Java I would use a loop to fix this. I think I have to call the function "move" recursive, but I don't know how to to this.
How can I solve this function?
I'm not sure exactly how your hanoi solution works, but I think this answers your question:
all_moves :: ([Move], Towers) -> Towers
all_moves ([], (xs, ys, zs)) = (xs, ys, zs)
all_moves movetowers = all_moves (move movetowers)
Hopefully you can see why this works - we keep doing a move and then a recursive call until there are no moves left.
I would like to do like this way even though my method is nor recursive as well.
allllmove :: Towers -> Towers
allmove ([0],[0],[0]) = ([0],[0],[0])
allmove ((x:xs),y,z)
length(x:xs) > length z && length z > length y = (xs,(x:y),z)
length(x:xs) >= length z && length (x:xs) > length y = (xs,(x:y),z)
...
..
.

Defining Unlambda-style tree notation in Coq

Here is a definition of polymorphic binary trees I am using in a Coq project.
Inductive tree { X : Type } : Type :=
| t_a : X -> tree
| t_m : tree -> tree -> tree.
A binary tree of natural numbers ( 1 ( ( 2 3 ) 4 ) ), declared using this definition, would be:
t_m ( t_a 1 ) ( t_m ( t_m ( t_a 2 ) ( t_a 3 ) ) ( t_a 4 ) )
As you can see, the definition becomes unusable very quickly with increasing number of leaves. What I want to do is define an Unlambda-style notation for trees so that I can replace the above with
' 1 ' ' 2 3 4
Is this possible?
I tried to get a solution that used only Coq notations, but couldn't get it to work. I suspect that Coq's extensible parser is not powerful enough to understand the notation you want. There is, however, a poor man's solution that involves dependent types. The idea is to write a parser for that notation and use that parser's type to encode the parser state. The type says that the parser "reads" some token (actually, takes that token as an argument to a function call), and goes into some next state that depends on the token it just read.
There's a small subtlety, though, which is that one cannot write that type using just regular Coq function types, because the number of arguments that function would take would depend on all the arguments it is being applied to. One solution is to use a coinductive type to encode this behavior, declaring a coercion to make it look like a function:
Inductive tree (X : Type) : Type :=
| t_a : X -> tree X
| t_m : tree X -> tree X -> tree X.
Arguments t_a {X} _.
Arguments t_m {X} _ _.
CoInductive tree_builder X : nat -> Type :=
| TbDone : tree X -> tree_builder X 0
| TbRead : forall n, (forall o : option X, tree_builder X match o with
| Some x => n
| None => S (S n)
end) ->
tree_builder X (S n).
Arguments TbDone {X} _.
Arguments TbRead {X} _ _.
(* Destructors for tree_builder *)
Definition case0 {X} (x : tree_builder X 0) : tree X :=
match x with
| TbDone t => t
end.
Definition caseS {X n} (x : tree_builder X (S n)) :
forall o : option X, tree_builder X match o with
| Some x => n
| None => S (S n)
end :=
match x with
| TbRead _ f => f
end.
Definition tb X n := tree_builder X (S n).
(* force is what does the magic here: it takes a tb and coerces it to a
function that may produce another tb, depending on what it is applied to. *)
Definition force X n (x : tb X n) : forall o : option X,
match o with
| Some x =>
match n with
| 0 => tree X
| S n' => tb X n'
end
| None =>
tb X (S n)
end :=
fun o =>
match o return tree_builder X match o with
| Some x => n
| None => S (S n)
end ->
match o with
| Some x => match n with
| 0 => tree X
| S n' => tb X n'
end
| None => tb X (S n)
end
with
| Some x => match n return tree_builder X n -> match n with
| 0 => tree X
| S n' => tb X n'
end
with
| 0 => fun t => case0 t
| S _ => fun t => t
end
| None => fun t => t
end (caseS x o).
Coercion force : tb >-> Funclass.
Our parser, then, is just a term of type tb X 0. As it is usually done, it has to be written in continuation-passing style because of the variable number of arguments.
Fixpoint parser_cont_type X (n : nat) : Type :=
match n with
| 0 => tree X
| S n' => tree X -> parser_cont_type X n'
end.
CoFixpoint parser X n : parser_cont_type X n -> tree_builder X n :=
match n with
| 0 => fun k => TbDone k
| S n' => fun k : tree X -> parser_cont_type X n' =>
TbRead n' (fun o => match o return tree_builder X match o with
| Some _ => n'
| None => S (S n')
end
with
| Some x => parser X n' (k (t_a x))
| None => parser X (S (S n')) (fun (t1 t2 : tree X) => k (t_m t1 t2))
end)
end.
Definition parser' X : tb X 0 :=
parser X 1 (fun t => t).
Next, we can define some extra notation to make this easier to use:
Notation "[ x ]" := (Some x) (at level 0).
Notation "''" := None (at level 0).
Notation "!" := (parser' _) (at level 20).
Here's how one could write your example tree, for instance:
Definition my_tree : tree nat := Eval hnf in ! '' [1] '' '' [2] [3] [4].
Notice the initial ! to start a call to the parser, and the [] that are needed to mark the leaves. I also couldn't get Coq's parser to accept ' as a token on its own. Besides these minor details, however, it is fairly close to what you had.
One problem is that, because the parser is defined using Coq functions, one needs to do a little bit of simplification to get a term that is exactly like the one you had originally. This is why I added the Eval call on the definition. This is probably not as practical as a real notation, and the definition is admittedly a bit tricky, but it could be pretty useful for some cases.
Here's a gist with the entire .v file.
UPDATE: I've written a post with a much simplified version of this technique made more generic.

SPOJ Problem Flibonakki time limit exceed

I am trying to solve this problem in Haskell but getting time limit exceed. I applied all my Haskell and mathematical skill to optimize this but all in vain. Could some one please suggest me how to optimize this code further. The sequence F_3 + F_7 + F_11 .... + F_(4n+3) = F_2n*F_(2n+1). I used O(log n) to method to calculate the Fibonacci numbers.
import Data.List
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as BS
matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
matmul [a,b,c] [d,e,f] m = [x,y,z] where
y = (a*e + b*f) `mod` m
z = (b*e + c*f) `mod` m
x = y + z
powM ::[Integer] -> Integer -> Integer -> [Integer]
powM a n m | n == 1 = a
| n == 2 = matmul a a m
| even n = powM ( matmul a a m ) ( div n 2 ) m
| otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m
readInt :: BS.ByteString -> Integer
readInt = fst.fromJust.BS.readInteger
solve::Integer -> BS.ByteString
solve n = BS.pack.show $ mod ( c*d ) 1000000007 where
[c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
--([_,a,_]:_) = powM [[1,2,1],[0,5,3],[0,3,2]] n 1000000007
-- f_3+f_7+f_11+f_15 = f_2n*f_(2n+1)
main = BS.interact $ BS.unlines. map ( solve.readInt ) . tail . BS.lines
Your solving seems to be fast enough but it seems that your main function does not print the answer after each new line. In fact it requires an extra newline to get the last answer so this can be the cause of your timeout! Here is a version that prints each answer directly after the input.
import Data.List
import Data.Maybe
import Control.Monad
import qualified Data.ByteString.Lazy.Char8 as B
import qualified Data.ByteString.Char8 as BC
import qualified Text.Show.ByteString as BS
matmul :: [Integer] -> [Integer] -> Integer -> [Integer]
matmul [a,b,c] [d,e,f] m = [x,y,z] where
y = (a*e + b*f) `mod` m
z = (b*e + c*f) `mod` m
x = y + z
powM :: [Integer] -> Integer -> Integer -> [Integer]
powM a n m | n == 1 = a
| n == 2 = matmul a a m
| even n = powM ( matmul a a m ) ( div n 2 ) m
| otherwise = matmul a ( powM ( matmul a a m ) ( div n 2 ) m ) m
solve :: Integer -> Integer
solve n = mod ( c*d ) 1000000007
where
[c,d,_] = powM [1,1,0] ( 2*n ) 1000000007
readInteger :: B.ByteString -> Integer
readInteger = fst . fromJust . B.readInteger
readInt :: B.ByteString -> Int
readInt = fst . fromJust . B.readInt
get :: IO B.ByteString
get = liftM (B.fromChunks . (:[])) BC.getLine
main :: IO ()
main = do
n <- liftM readInt get
replicateM_ n ( liftM readInteger get >>= B.putStrLn . BS.show . solve )

Produce a sentence from a grammar with a given number of terminals

Say you've got a toy grammar, like: (updated so the output looks more natural)
S -> ${NP} ${VP} | ${S} and ${S} | ${S}, after which ${S}
NP -> the ${N} | the ${A} ${N} | the ${A} ${A} ${N}
VP -> ${V} ${NP}
N -> dog | fish | bird | wizard
V -> kicks | meets | marries
A -> red | striped | spotted
e.g., "the dog kicks the red wizard", "the bird meets the spotted fish or the wizard marries the striped dog"
How can you produce a sentence from this grammar according to the constraint that it must contain a total of n Vs + As + Ns. Given an integer the sentence must contain that many terminals. (note of course in this grammar the minimum possible n is 3).
The following Python code will generate a random sentence with the given number of terminals.
It works by counting the number of ways to produce a sentence of a given length, generating a large random number, and computing the indicated sentence.
The count is done recursively, with memoization.
An empty right hand side produces 1 sentence if n is 0 and 0 sentences otherwise.
To count the number of sentences produced by a nonempty right hand side, sum over i, the number of terminals used by the first symbol in the right hand side.
For each i, multiply the number of possibilities for the rest of the right hand side by the number of possibilities for the first symbol.
If the first symbol is a terminal, there is 1 possibility if i is 1 and 0 otherwise.
If the first symbol is a nonterminal, sum the possibilities over each alternative.
To avoid infinite loops, we have to be careful to prune the recursive calls when a quantity is 0.
This may still loop infinitely if the grammar has infinitely many derivations of one sentence.
For example, in the grammar
S -> S S
S ->
there are infinitely many derivations of the empty sentence: S => , S => S S => , S => S S => S S S => , etc.
The code to find a particular sentence is a straightforward modification of the code to count them.
This code is reasonably efficient, generating 100 sentences with 100 terminals each in less than a second.
import collections
import random
class Grammar:
def __init__(self):
self.prods = collections.defaultdict(list)
self.numsent = {}
self.weight = {}
def prod(self, lhs, *rhs):
self.prods[lhs].append(rhs)
self.numsent.clear()
def countsent(self, rhs, n):
if n < 0:
return 0
elif not rhs:
return 1 if n == 0 else 0
args = (rhs, n)
if args not in self.numsent:
sym = rhs[0]
rest = rhs[1:]
total = 0
if sym in self.prods:
for i in xrange(1, n + 1):
numrest = self.countsent(rest, n - i)
if numrest > 0:
for rhs1 in self.prods[sym]:
total += self.countsent(rhs1, i) * numrest
else:
total += self.countsent(rest, n - self.weight.get(sym, 1))
self.numsent[args] = total
return self.numsent[args]
def getsent(self, rhs, n, j):
assert 0 <= j < self.countsent(rhs, n)
if not rhs:
return ()
sym = rhs[0]
rest = rhs[1:]
if sym in self.prods:
for i in xrange(1, n + 1):
numrest = self.countsent(rest, n - i)
if numrest > 0:
for rhs1 in self.prods[sym]:
dj = self.countsent(rhs1, i) * numrest
if dj > j:
j1, j2 = divmod(j, numrest)
return self.getsent(rhs1, i, j1) + self.getsent(rest, n - i, j2)
j -= dj
assert False
else:
return (sym,) + self.getsent(rest, n - self.weight.get(sym, 1), j)
def randsent(self, sym, n):
return self.getsent((sym,), n, random.randrange(self.countsent((sym,), n)))
if __name__ == '__main__':
g = Grammar()
g.prod('S', 'NP', 'VP')
g.prod('S', 'S', 'and', 'S')
g.prod('S', 'S', 'after', 'which', 'S')
g.prod('NP', 'the', 'N')
g.prod('NP', 'the', 'A', 'N')
g.prod('NP', 'the', 'A', 'A', 'N')
g.prod('VP', 'V', 'NP')
g.prod('N', 'dog')
g.prod('N', 'fish')
g.prod('N', 'bird')
g.prod('N', 'wizard')
g.prod('V', 'kicks')
g.prod('V', 'meets')
g.prod('V', 'marries')
g.prod('A', 'red')
g.prod('A', 'striped')
g.prod('A', 'spotted')
g.weight.update({'and': 0, 'after': 0, 'which': 0, 'the': 0})
for i in xrange(100):
print ' '.join(g.randsent('S', 3))
Perhaps not the best solution, but I'd recursively work my way through each rule of the grammar until I've exceeded the constraint, then pop back and explore another path along the grammar. Keep all the sentences that meet the constraint and throw out all the sentences that don't.
For example, with n = 3:
S -> (${NP} ${VP}) -> ( (the ${N}) ${VP}) -> ( (the (dog) ${VP}) -> ... -> ( (the (dog) ( (kicks) (the ${NP} ) ) ) ) -> ( (the (dog) ( (kicks) (the (dog) ) ) ) )
And then pop back
( (the (dog) ( (kicks) (the ${N} ) ) ) ) -> ( (the (dog) ( (kicks) (the (fish) ) ) ) )
and a little while later...
( (the (dog) ( ${V} ${N} ) ) ) -> ( (the (dog) ( (meets) ${N} ) ) ) -> ( (the (dog) ( (meets) the (dog) ) ) )
etc.
Essentially a depth-first graph search, only you are building the graph as you are searching it (and you stop building parts that exceed the constraints).
This question contains a category error. The grammar you've specified has the appearance of a context free grammar, but the requirement that there be a specific number of terminal nodes requires a recursively enumerable grammar.

Resources