You selected memory1.pl

headline:-
   write('% -------------------------------------------------- %'),nl,
   write('%  simulating shopping behavior                      %'),nl,
   write('%  --- optimal use of memory for search decisions(1) %'),nl,
   write('% -------------------------------------------------- %'),nl,
   h0.
h0:-
   write('%  state(time(T),at(Store),price(P),act(A)):-  a state,'),nl,
   write('%   or state(T,L,P,A):-  in short.'),nl,
   write('%  frame(time(T),at(Store),act(Act)):- scripts of purchase decision,'),nl,
   write('%   and simulate it via backtrack.'),nl,
   write('%  display_actual_choices:- to review simulation results.'),nl,
   write('%  frame_x(T,L,A,X):- version with variable threshold.'),nl,
   write('%  otm(X,LU,Q,Vq,V):- optimal threshold.'),nl,
   write('%  h0.   this.'),nl.
me:-
   write('% file: memory1.pl.'),nl,
   write('% created: 14-17 Mar 2003.'),nl,
   write('% previous: search.pl(Sep 2002). '),nl,
   write('% previous: search10.pl(Nov 2002). '),nl,
   write('% author: Kenryo INDO (Kanto Gakuen University) '),nl,
   write('% url: http://www.us.kanto-gakuen.ac.jp/indo/front.html'),
   nl.

references:-
   write('% inspired by:'),nl,
   write('%  %   J. Dow(1991). '),nl,
   write('%  %   search decisions with limited memory.  '),nl,
   write('%  %   Review of Economic Studies 58: 1-14.'),nl,
   nl.

:- headline.

:- dynamic memory_threshold /2.


%  basic objects in this model
%-----------------------------------------------

%agent(J):-member(J,[1,2,3]).

store(S):-
   member(S,[1,2]).

price(P):-
   length(L,101),
   nth0(P,L,P).

time(T):-
   member(T,[0,1,2,3]).


%  act frame for purchase behavior of the agent
%-----------------------------------------------
act(A):-
   act(A,nominal); act(A,substantive).

act(A,nominal):-
   member(A,[see,buy,consume]).

act(A,substantive):-
   member(A,[memorize,decide]).


%  state description
%-----------------------------------------------
state(time(T),at(Store),price(P),act(A)):-
   time(T),
   Store = store(S),
   Store,
   price_of_store(S,P),
   act(A).

price_of_store(1,0).
price_of_store(2,1).



%-----------------------------------------------
%  limited memory
%-----------------------------------------------
%  limited but significant memory means the 
%   delay of purchase is vluable for the agent, 
%   potentially, via updating the threshold of stopping rule. 

memorize(C -> M):-
   C = state(time(T),at(Store),price(P),act(see)),
   memory_threshold(time(T),X),
   (
    P < X
     ->
      M = (Store,less_than(X))
     ;
      M = (Store,no_less_than(X))
   ).

memory_threshold(time(1),1/2).


%-----------------------------------------------
%  decision rule under lmited memory
%-----------------------------------------------
decide(C,M,D):-
   C = state(time(_T),at(Store2),price(P),act(see)),
   M = (Store1,less_than(X)),
   Y is (0 + X) / 2,
   (
    P > Y
     ->
      D = buy(at(Store1))
     ;
      D = buy(at(Store2))
   ).

decide(C,M,D):-
   C = state(time(_T),at(Store2),price(P),act(see)),
   M = (Store1,no_less_than(X)),
   Y is (X + 1) / 2,
   (
    P =< Y
     ->
      D = buy(at(Store2))
     ;
      D = buy(at(Store1))
   ).



%-----------------------------------------------
%  act frame  (script for search)
%-----------------------------------------------
%  a sort of production system for search decisions:
%  agent process simulation under act frame

% t = 1 
%-----------
frame(time(1),at(store(1)),act(see(C))):-
   C = state(time(1),at(store(1)),price(_P),act(see)),
   C.

frame(time(1),at(store(1)),act(memorize(C->M))):-
   frame(time(1),at(store(1)),act(see(C))),
   memorize(C->M).

% t = 2 
%-----------
frame(time(2),at(store(2)),act(decide(C,M,D))):-
   frame(time(1),_,act(memorize(_->M))),
   C = state(time(2),at(store(2)),price(_P),act(see)),
   C,
   decide(C,M,D),
   D = buy(at(store(_S))).

frame(time(2),at(store(2)),act(buy(S,price(P)))):-
   S = store(2),
   D = buy(at(S)),
   C = state(time(2),at(S),price(P),act(see)),
   frame(time(2),at(S),act(decide(C,_M,D))).

% t = 3 
%-----------
frame(time(3),at(store(1)),act(buy(S,price(P)))):-
   S = store(1),
   D = buy(at(S)),
   frame(time(2),_,act(decide(_,_M,D))),
   C = state(time(3),at(S),price(P),act(see)),
   C.

frame(time(3),at(home),act(consume(store(2),_P))):-
   S = store(2),
   frame(time(2),at(S),act(buy(S,_P))).

frame(time(3),at(home),act(consume(store(1),_P))):-
   S = store(1),
   frame(time(3),at(S),act(buy(S,_P))).



%-----------------------------------------------
%  decision process with variable memory threshold
%-----------------------------------------------

frame_x(time(T),at(S),act(A), X):-
   F = frame(time(T),at(S),act(A)),
   clause(F,_B),
   % note:  it does not good job, 
   %  if the head of frame is not unique.
   update_memory_threshold(time(T),X,A),
   F.

update_memory_threshold(_T,_X,A):-
   \+ A = memorize(_).

update_memory_threshold(T, X,memorize(_)):-
   \+ var(X),
   retractall(memory_threshold(T,_)),
   assert(memory_threshold(T,X)).

update_memory_threshold(T, X,memorize(_)):-
   var(X),
   user_input_of_threshold(T,X),
   retractall(memory_threshold(T,_)),
   assert(memory_threshold(T,X)).

user_input_of_threshold(T,X):-
   nl,
   write(' $$$ Please input the new threshold value in [0,1]. $$$'),
   nl,
   read(Y),
   (
    (Y>=0,Y=<1)
     ->
       X = Y
     ;
       update_memory_threshold(T,X,memorize)
   ),
   display_expected_price(X,_,_,_,_).

display_expected_price(X,[L,L1,U,U1],[Q1,Q2,Q3,Q4],Vq,V):-
   expected_price(X,[L,L1,U,U1],[Q1,Q2,Q3,Q4],Vq,V),
   display_expected_price(less_than,X,(L,Q1),(L1,Q2)),
   display_expected_price(no_less_than,X,(U,Q3),(U1,Q4)),
   nl,
   write('%------------------------------------------------%'),
   nl,
   write(' The total expected price is '),
   nl,
   tab(3),
   write(Vq),
   nl,
   tab(2),
   write('= '),
   write(V),
   nl,
   nl,
   write(' Go ahead? (y) '),
   read(y).

display_expected_price(less_than,X,(L,Q1),(L/2,Q2)):-
   nl,
   write(' When the price of store 1, P1, is less than '),
   write(X),
   write(', '),
   nl,
   write(' with the probability same as it, '),
   write(X),
   write(', '),
   nl,
   write(' the expected minimum price is '),
   write(L),
   write(', the mean of P1, '),
   nl,
   write(' if the price of store 2 is no less than of it '),
   nl,
   write(' with the probability '),
   write((X * (1-L) = Q1)),
   write(', '),
   nl,
   write(' and is '),
   write((L/2)),
   write(', '),
   nl,
   write(' if the price of store 2 is less than '),
   write(L),
   write(', '),
   nl,
   write(' with the probability '),
   write((X * L = Q2)),
   write('.'),
   nl.

display_expected_price(no_less_than,X,(U,Q3),(U/2,Q4)):-
   nl,
   write(' When the price of store 1, P1, is no less than '),
   write(X),
   write(', '),
   nl,
   write(' with the probability '),
   write(1-X),
   write(', '),
   nl,
   write(' the expected minimum price is '),
   write(U),
   write(', the mean of P1, '),
   nl,
   write(' if the price of store 2 is no less than of it '),
   nl,
   write(' with the probability '),
   write(((1-X) * (1-U) = Q3)),
   write(', '),
   nl,
   write(' and is '),
   write((U/2)),
   write(', '),
   nl,
   write(' if the price of store 2 is less than '),
   write(U),
   write(', '),
   nl,
   write(' with the probability '),
   write(((1-X) * U = Q4)),
   write('.'),
   nl.


% -----------------------------------------------------------  %
% expected price and the optimal threshold of memory
% -----------------------------------------------------------  %
expected_price(X,[L,L1,U,U1],[Q1,Q2,Q3,Q4],Vq,V):-
   L = X/2,
   U = (1+X)/2,
   L1 = L/2,
   U1 = U/2,
   Q1 is X * (1-L),
   Q2 is X * L,
   Q3 is (1-X) * (1-U),
   Q4 is (1-X) * U,
   sum([Q1,Q2,Q3,Q4],1),
   product_sum_eq([Q1,Q2,Q3,Q4],[L,L1,U,U1],_,Vq,V).

otm(X,LU,Q,Vq,V):-
   length(L,101),
   nth0(Xper,L,_), 
   X is (100-Xper) /100,
   expected_price(X,LU,Q,Vq,V),
   \+ (
     nth0(Yper,L,_), 
     Y is (100-Yper) /100,
     expected_price(Y,_LU1,_Q1,_Vq1,V1),
     V1 < V
   ).




%-----------------------------------------------
%  arithmetic utilities
%-----------------------------------------------
% excerptions from math1.pl

% max,min
% -----------------------------------------------------------  %
max_of(X,[X]).
max_of(Z,[X|Y]):-
   max_of(Z1,Y),
   (X > Z1 -> Z=X; Z=Z1).
min_of(X,[X]).
min_of(Z,[X|Y]):-
   min_of(Z1,Y),
   (X < Z1 -> Z=X; Z=Z1).


% count frequency of occurence of the specified value of variable, M.
% -----------------------------------------------------------  %
% note: Both of M and L have to be specified.

counter(N,M,L):-
    length(L,_),
    findall(M,member(M,L),Mx),
    length(Mx,N).

% sum
% -----------------------------------------------------------  %
sum([],0).
sum([X|Members],Sum):-
   sum(Members,Sum1),
  %number(X),
   Sum is Sum1 + X.

% added: 27 feb 2003.
sum_eq([],0,0).
sum_eq([X],X,X).
sum_eq([X|Members],Eq,Sum):-
   Members \= [],
   sum_eq(Members,Eq1,Sum1),
   Eq = Eq1 + X,
   Sum is Sum1 + X.

%
% product
% -----------------------------------------------------------  %
product([],1).
product([X|Members],Z):-
   product(Members,Z1),
  %number(X),
   Z is Z1 * X.
%
% weighted sum
% -----------------------------------------------------------  %
product_sum([],[],[],0).
product_sum([P|Q],[A|B],[E|F],V):-
    length(Q,N),
    length(B,N),
    product_sum(Q,B,F,V1),
    E is P * A,
    V is V1 + E.
%
% product sum value with equational
% -----------------------------------------------------------  %
product_sum_eq([],[],[],0,0).
product_sum_eq([P|Q],[A|B],[E|F],V,Vq):-
    length(Q,N),
    length(B,N),
    product_sum_eq(Q,B,F,V1,Vq1),
    Eq = (P) * A,
    E is Eq,
    (Vq1=0 -> Vq = Eq; Vq = Vq1 + Eq),
    V is V1 + E.
%
% allocation
% -----------------------------------------------------------  %
allocation(N,A,[X|Y]):-
    allocation(N,A,A,[X|Y]).
allocation(0,_,0,[]).
allocation(N,A,B,[X|Y]):-
    integer(A),
    length([X|Y],N),
    allocation(_N1,A,B1,Y),
    % N1 is N - 1,
    % sum(Y,B1),
    K is A - B1 + 1,
    length(L,K),
    nth0(X,L,X),
    B is B1 + X.

%end



return to front page.