lv:-
	tell('animal_exNum58_LeaveOneOut.txt'),	

	%findall(EIp,ex(EIp,Ep,1),PosEI0s), % full size learning
	%findall(EIn,ex(EIn,En,0),NegEIs),
	numbersList(9,13,PosEI0s),
	numbersList(38,47,NegEIs),
	
	length(NegEIs,NK),
	unCoverRecord(PosEI0s,PosEIs), % reduce = remove those already explained by the examples
	length(PosEIs,PK),
	
	append(PosEIs,NegEIs,AllEI),write({PK,NK}),write('Totally '),nl,

	%AllEI=[1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,22,23,24],
	%[1,2,3,4,5,6,7,10,11,12,14,15,16,17,18,19,20,22,23,24],%[1,4,5,7,10,11], %[1,3,4,5,6,7,8,9,10,11],
	
	leaveOneOut(AllEI,AllEI,Results),
	% Results is a list of Uncovered test data -- you may trace the learning result and see why
	percentage(Results,AllEI,PA),
	write('Predictive Accuracy is'),write(PA),nl,
	told.




leaveOneOut([],_,[]).
leaveOneOut([TestID|Folds],Data,Results):-
	leaveOneOut(Folds,Data,PreResults),

	append(Da,[TestID|Db],Data),append(Da,Db,TrainingData),write('Training data are '),write(TrainingData),nl,
	
	
	(set(predictionMode, bayesien)->
		write('start'),nl,
		generateBayesianPredictionModel(TrainingData,Model), write('Finish stage of generating'),nl,print_list(Model),nl,
		bayesianPrediction(TestID,Model,ModelOpinion), write('Finish stage of predicting and model opinion is '),write(ModelOpinion),nl,
		checkCorrectPrediction(ModelOpinion,TestID,OneResult)
		;
		% Covering Algorithm
		sepPN_ID(TrainingData,TrainPE,TrainNE),write('Training data are '), write(TrainPE), write(TrainNE),nl,
		generateMAP_hypothesis(TrainPE,TrainNE,Hypothesis),
		mapHypothesisPrediction(TestID,Hypothesis,OneResult)
	),
	
	append(OneResult,PreResults,Results).



/*************** This part belongs to covering *****************/
generateMAP_hypothesis(TrainPE0,TrainNE,FinalT):-
		% since the order of providing example does matter when using the , to be fair, permutate the order before training
		permutateList(TrainPE0,TrainPE),write('(((( Positive Exs to learn '), write(TrainPE), 	% otherwise each fold give nearly the same result	

		% modify here to make it covering(TrainPE,TrainNE,[],FinalT).
		cover(TrainPE,TrainNE,[],FinalTI), %multiCovering(TrainPE,TrainNE,[],FinalTI), 
		tInterpreter(FinalTI,FinalT),write('*** Learning Result is'), nl,print_list(FinalT),nl.

mapHypothesisPrediction(TestID,Hypothesis,Result):-	
		addT(Hypothesis,HRef),		
		unCoverRecord([TestID],CoverResult), % need to deal with evaluation Depth and e to b problem 
		ex(TestID,E,PosNegSign),
		trueCheck(CoverResult,PosNegSign,Result),
		write('--------------------Test'),write({[TestID],Result}),
		removeT(HRef).


trueCheck([],1,[1]).
trueCheck([E],0,[1]).
trueCheck([],0,[]).
trueCheck([E],1,[]).

checkCorrectPrediction(PredictedSign,EI,CorrectSign):-
	ex(EI,E,OriginalSign),
	(PredictedSign==OriginalSign->
		CorrectSign=[1];
		CorrectSign=[]
	).


percentage(Record,All,P):-
	length(Record,K),
	length(All,AllK),
	P is (K*100)/AllK. 

