



%/*Compare the single and multi-Ex modes
com:-
	yap_flag(argv,[BK_File]),
	consult(BK_File),


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

	%permutateList(PosEIs,PosEIsLearning), write(PosEIsLearning),
PosEIsLearning=PosEIs,
	assert(setLearningMode(singleExs),Ref_single),

	write('/**********************SingleEx Learning**********************/'),

statistics(cputime,[Total1,Start]),
		multiCovering(PosEIsLearning,NegEIs,[],FinalTI_single), 	
statistics(cputime,[Total2,SingleTime0]),
SingleTime is SingleTime0-Start,
		tInterpreter(FinalTI_single,FinalT_single), 
		write('*** Learning Result by SINGLE example style is'), nl,print_list(FinalT_single),nl,

	erase(Ref_single),
	assert(setLearningMode(multiExs),Ref_multi),

	write('/********************* MultipleExs Learning**********************/'),
statistics(cputime,[Total3,MultiStart]),
		multiCovering(PosEIsLearning,NegEIs,[],FinalTI_multi), 	
statistics(cputime,[Total4,MultiTime0]),
MultiTime is MultiTime0-MultiStart,
		tInterpreter(FinalTI_multi,FinalT_multi), 
		write('*** Learning Result by Multiple example style is'), nl,print_list(FinalT_multi),nl,
	erase(Ref_multi), %write('problem here1'),nl,
	nl,nl,%write('problem here2'),nl,
	write('/----------------Running Time Comparison----------------/'),nl,
	write('Time for Single Mode is '), write(SingleTime),nl,
	write('Time for Multi Mode is '), write(MultiTime).

	
	
%*/



/* assume Pos and Neg are mixed in the index
get list of all Ex (both pos and neg)
reduce (otherwise they will increase PA -- it is not the learned theory that make it explain the test data---it already state even before learning)
compute leave one out 
		each iteration: one test, the others -> training (permutate before send for learning)
*/

lv_readFile:-
	yap_flag(argv,[InputFile]),
  	consult(InputFile), 
	%tell('grammarLeaveOneOut.txt'),	
	findall(EIp,ex(EIp,Ep,1),PosEI0s), % full size learning
	findall(EIn,ex(EIn,En,0),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,
	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,
	nl.%told.

/* every datasets have their own lv, due to the example -- but they all call the predicate of leave-one-out/3
lv:-
	tell('metaLog_LeaveOneOut.txt'),	
	%findall(EIp,ex(EIp,Ep,1),PosEI0s), % full size learning
	%findall(EIn,ex(EIn,En,0),NegEIs),
	%length(NegEIs,NK),
	%unCoverRecord(PosEI0s,PosEIs), % reduce = remove those already explained by the examples
	%length(PosEIs,PK),
	numbersList(1,100,PosEIs0),
	unCoverRecord(PosEIs0,PosEIs),write('Positive examples to be learned'),write(PosEIs),nl,
	NegEIs=[],

	append(PosEIs,NegEIs,AllEI),%write({PK,NK}),write('Totally '),nl,

	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,
	nl.%told.
*/

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


% only one test--either pos or neg
% no need for weight--just average finally.
leaveOneOut([],_,[]).
leaveOneOut([TestID|Folds],Data,Results):-
	leaveOneOut(Folds,Data,PreResults),

	append(Da,[TestID|Db],Data),append(Da,Db,Rest),%sepPN_ID(Rest,TrainPE,TrainNE),
	write('Training data are '), write(Rest),nl,%write(TrainPE), write(TrainNE),nl,
	leaveOneTrain(Rest,TestID,OneResult),
	Results= [OneResult|PreResults].	




/*leaveOneTrain(AllEIs,TestID,Result):-
		generateBayesianPredictionModel(AllEIs,FactsPs),
		insertHs(FactsPs),
		testBayePrediction(TestID,Result).
*/




leaveOneTrain(TrainPE0,TrainNE,TestID,Result):-
		permutateList(TrainPE0,TrainPE),write('(((( Positive Exs to learn '), write(TrainPE), 	% otherwise each fold give nearly the same result
		%learnAll(TrainPE,FinalTI),	
		
		
		trainANDcount(TrainPE,FinalTI),

% Covering Algorithm
%		%multiCovering(TrainPE,TrainNE,[],FinalTI), 
%		cover(TrainPE,TrainNE,[],FinalTI), 


		tInterpreter(FinalTI,FinalT),
		write('*** Learning Result is'), nl,print_list(FinalT),nl,

		addTI(FinalTI,[],Ref), %write(??????????),nl,
		%listing,
		asserta((enzyme_state(EC_Number,no_change):- (\+enzyme_state(EC_Number,inhibited);\+enzyme_state(EC_Number,activated))),Ref_defaultNoChangeStatement),
		%asserta((enzyme_state(EC_Number,State):- enz_concentration(EC_Number,Change),propotional(State,Change)),Ref_propotionalAssupmtion),
		%asserta((enzyme_state(EC_Number,State):- concentration_enzClass(EC_Number,Change),propotional(State,Change)),Ref_propotionalAssupmtion),
		predictTestData(TestID,Result),
		write('--------------------Test'),write({[TestID],Result}), ex(TestID,concentration(MID,Change),PosNegSign),write(MID),nl,nl,nl,
		erase(Ref_defaultNoChangeStatement),
		%erase(Ref_propotionalAssupmtion),
		removeRefT(Ref,Useless).



predictTestData(TestID,Result):-
		unCoverRecord([TestID],CoverResult), % need to deal with evaluation Depth and e to b problem 
		ex(TestID,E,PosNegSign),
		trueCheck(CoverResult,PosNegSign,Result).


trueCheck([],1,[1]).
trueCheck([E],0,[1]).
trueCheck([],0,[]).
trueCheck([E],1,[]).
/*
TruePositive, FalsePos,
TrueNeg, FalseNeg
*/

