:- ensure_loaded( '../run.pl' ).

rrun :- run([runs_file('runs/mixture_concept_learning_run'),
	     results_dir('out/mixture_concept_learning_out')]).

exs.   %do nothing with the data, except load it from data/exs.pl

lhood_canonical(Mst,_Mi,LLst,LLi,Ratio) :-
	model_llhood(Mst,LLst),  % compute log-likelihood of Mst
	Ratio is exp(LLst - LLi).

model_llhood(Model,LL) :-
	findall(dat(XX,XY,C),datum((XX,XY),C),Data),
	plod_thru_data(Data,Model,0,LL).

plod_thru_data([],_Model,OutLL,OutLL).
plod_thru_data([dat(XX,XY,C)|T],Model,InLL,OutLL) :-
	(
	  inconsistent(C,XX,XY,Model)
	->
	  MidLL is log(0.2) + InLL
	;
	  MidLL is log(0.8) + InLL
	),
	plod_thru_data(T,Model,MidLL,OutLL).


inconsistent(pos,XX,XY,circle((X,Y),R)) :-
	SqD is (XX-X)**2 + (XY-Y)**2,
	R2 is R**2,
	R2 < SqD.

inconsistent(neg,XX,XY,circle((X,Y),R)) :-
	SqD is (XX-X)**2 + (XY-Y)**2,
	R2 is R**2,
	R2 >= SqD.

inconsistent(pos,XX,XY,rectangle(_Name,X1,Y1,X2,Y2)) :-
	(XX<X1 ; XX>X2 ; XY<Y1 ; XY>Y2).       % pos outside the rectangle

inconsistent(neg,XX,XY,rectangle(_Name,X1,Y1,X2,Y2)) :-
	XX>=X1 , XX=<X2 , XY>=Y1 , XY=<Y2.     % neg inside the rectangle

:- multifile( exec_extension/2 ).
:- dynamic( exec_extension/2 ).

exec_extension(counts,File) :-
	flatoms(['sort ', File, '| uniq -c >', File, '.counts'],Com),
	shell(Com).