Skip to content

Instantly share code, notes, and snippets.

@64lines
Last active May 30, 2022 00:22
Show Gist options
  • Save 64lines/fcbd7c8187301355411d8bf2484f05e9 to your computer and use it in GitHub Desktop.
Save 64lines/fcbd7c8187301355411d8bf2484f05e9 to your computer and use it in GitHub Desktop.
Simulador de compra y venta de acciones auto adaptables.
/*
* Stock buy and sell sensor simulation
*
* Author: Julian Murillo
* Date: May 2022
*
* simul(File, Format, What, StockPrice, InvestmentHorizon, LC, TotC): simulation predicate
*
* File = 'console' or a filename (suffix '.csv' or '.txt' is added if none present)
* Format = 'txt' or 'csv' (format of the output)
* What = 'all' or 'best' (either list all products for a context or only the best one)
* StockPrice, StockPrice: context (can be given or not, if not all are generated)
* LC, TotC: Claims (can be given or not, if not will TotC will be maximized)
*
* Example: show on the console all products for StockPrice = 0 (low) and InvestmentHorizon = 1 (high)
* simul(console, txt, all, 1, 0, _, _).
*
* Example: create a file data.csv with the best product for each context
* simul(data, csv, best, _, _, _, _).
*
*/
simul(File, Format, What, StockPrice, InvestmentHorizon, LC, TotC) :-
findall(Product, one_product(What, StockPrice, InvestmentHorizon, LC, TotC, Product), LProduct),
sort(LProduct, LProduct1),
write_to_file(File, Format, What, LProduct1).
write_to_file(File, Format, What, LProduct) :-
g_assign(cxt, void),
( File = console ->
Stm = user_output
;
(sub_atom(File, _, _, _, '.') -> File1 = File ; format_to_atom(File1, '%s.%s', [File, Format])),
format('output file: %s\n', [File]),
open(File1, write, Stm)
),
( write_header(What, best, Stm, Format),
member(Product, LProduct),
write_one(Format, What, Stm, Product),
fail
;
close(Stm)
).
write_header(What, What, Stm, Format) :-
!,
( Format = txt ->
NameLSD = 'SD1...SD4',
NameC = 'C1,C2,...C4'
;
NameLSD = ['SD1','SD2','SD3','SD4'],
NameC = ['C1','C2','C3','C4']
),
write_line(Format, Stm, ['BuyStocks':9, 'SellStocks':10,
'STP', 'LTP', 'ML', 'TotNFR',
NameLSD, 'TotSD',
NameC, 'TotC', 'ObjValue']), nl(Stm).
write_header(_, _, _, _).
write_one(Format, What, Stm, Product) :-
Product = p(StockPrice, InvestmentHorizon, LRC, LC, TotC, LNFR, TotNFR, LSD, TotSD, ObjValue),
( g_read(cxt, [StockPrice, InvestmentHorizon]) ->
true
;
g_assign(cxt, [StockPrice, InvestmentHorizon]),
name_of(StockPrice, [low, high], NameStateOfTheRiver),
name_of(InvestmentHorizon, [short, long], NameHealthOfBattery),
write_line(Format, Stm, []),
write_line(Format, Stm, ['Context:', NameStateOfTheRiver, NameHealthOfBattery]),
write_line(Format, Stm, []),
write_header(What, all, Stm, Format)
),
LRC = [YearlyHistory, _MonthlyHistory, StopLoss, _TimeSell],
LNFR = [ShortTermProfits, LongTermProfits, MitigateLoss],
name_of(YearlyHistory, ['YearHist', 'MonthHist'], NameTD),
name_of(StopLoss, ['StopLoss', 'TimeSell'], NameON),
name_of(ShortTermProfits, [--, -, =, +, ++], LevelSTP),
name_of(LongTermProfits, [--, -, =, +, ++], LevelLTP),
name_of(MitigateLoss, [--, -, =, +, ++], LevelML),
write_line(Format, Stm, [NameTD:9, NameON:10, LevelSTP:2, LevelLTP:2, LevelML:2, TotNFR:6, LSD, TotSD:5, LC, TotC:4, ObjValue:6]).
write_line(txt, Stm, LElem) :-
member(X, LElem),
write_txt(Stm, X),
write(Stm, ' '),
fail.
write_line(csv, Stm, LElem) :-
member(X, LElem),
( list(X) ->
member(Y, X),
write_csv(Stm, Y)
;
write_csv(Stm, X)
),
write(Stm, ';'),
fail.
write_line(_, Stm, _) :-
nl(Stm).
write_csv(Stm, X:_) :- % ignore Width
!,
write_csv(Stm, X).
write_csv(Stm, X) :-
atom(X), !,
format(Stm, '"~a"', [X]).
write_csv(Stm, X) :-
write(Stm, X).
write_txt(Stm, X:Width) :-
( atom(X),
format(Stm, '%-*s', [Width, X])
;
integer(X),
format(Stm, '%*d', [Width, X])
;
write(Stm, X)
), !.
write_txt(Stm, X) :-
write(Stm, X).
name_of(I, L, X) :- % act ast nth0/3, could be replaced by it
I1 is I + 1,
nth(I1, L, X).
/* Constraint Program */
one_product(What, StockPrice, InvestmentHorizon, LC, TotC, Product) :-
set_constraint(StockPrice, InvestmentHorizon, LC, TotC, LRC, LNFR, LSubNFR, TotNFR, LSD, TotSD, ObjValue),
fd_labeling([StockPrice, InvestmentHorizon]), % if not given, try all possibilites
( What = all ->
fd_labeling(LRC) % label LRC before maximization to try all possibilites
;
true
),
fd_maximize(enumerate(LC, LRC, LNFR, LSubNFR, LSD), ObjValue),
Product = p(StockPrice, InvestmentHorizon, LRC, LC, TotC, LNFR, TotNFR, LSD, TotSD, ObjValue).
enumerate(LC, LRC, LNFR, LSubNFR, LSD) :-
fd_labeling(LC),
fd_labeling(LRC),
fd_labeling(LNFR),
fd_labeling(LSubNFR),
fd_labeling(LSD).
set_constraint(StockPrice, InvestmentHorizon, LC, TotC, LRC, LNFR, LSubNFR, TotNFR, LSD, TotSD, ObjValue):-
fd_domain(StockPrice, 0, 1), % Low, High
fd_domain(InvestmentHorizon, 0, 1), % Short, Long
% Hard Goals
LGoals = [PredictStockFluctuation, BuyStock, SellStock],
fd_domain_bool(LGoals),
% Reusable Components (operationalization of the Hard Goals)
LRC = [MonthlyHistory, YearlyHistory, StopLoss, TimeSell],
fd_domain_bool(LRC),
% Non Functional Requirements
LNFR = [ShortTermProfits, LongTermProfits, MitigateLoss],
fd_domain(LNFR, 0, 4), % {0: "--"", 1: "-", 2: "=", 3: "+", 4: "++"}
LSubNFR = [C2ShortTermProfits, C1LongTermProfits, C3LongTermProfits, C4MitigateLoss],
fd_domain(LSubNFR, 0, 4),
% Claims
LC = [C1, C2, C3, C4],
fd_domain_bool(LC),
% Soft Dependencies
LSD = [SD1, SD2, SD3, SD4],
fd_domain_bool(LSD),
% Constants on Hard Goals
PredictStockFluctuation #= 1,
PredictStockFluctuation * 2 #= BuyStock + SellStock,
BuyStock #= MonthlyHistory + YearlyHistory,
SellStock #= StopLoss + TimeSell,
% Monthly and Yearly History are mutually exclusive.
MonthlyHistory + YearlyHistory #=< 1,
% StopLoss and TimeSell are mutually exclusive.
StopLoss + TimeSell #=< 1,
% Constraints on Claims
% C1: YearlyHistory -, MonthlyHistory++ => ShortTermProfits
C1 #<=> (YearlyHistory #==> C2ShortTermProfits #=< 1) #/\ (MonthlyHistory #==> C2ShortTermProfits #>= 4),
% C2: YearlyHistory ++, MonthlyHistory - => LongTermProfits
C2 #<=> (YearlyHistory #==> C1LongTermProfits #>= 4) #/\ (MonthlyHistory #==> C1LongTermProfits #=< 1),
% C3: StopLoss- TimeSell++ => LongTermProfits
C3 #<=> (StopLoss #==> C3LongTermProfits #=< 1) #/\ (TimeSell #==> C3LongTermProfits #>= 4),
% C4: StopLoss- TimeSell++ => MitigateLoss
C4 #<=> (StopLoss #==> C4MitigateLoss #=< 1) #/\ (TimeSell #==> C4MitigateLoss #>= 4),
TotC #= C1 + C2 + C3+ C4,
% NFR as mean of SubNFR
ShortTermProfits #= C2ShortTermProfits,
LongTermProfits #= (C1LongTermProfits + C3LongTermProfits) / 2,
MitigateLoss #= C4MitigateLoss,
TotNFR #= ShortTermProfits * ShortTermProfits + LongTermProfits * LongTermProfits + MitigateLoss * MitigateLoss,
% Soft Dependencies
% SD1: StockPrice: High -> ShortTermProfits ++, LongTermProfits ++, MitigateLoss ++
SD1 #<=> (StockPrice #= 1 #==> ShortTermProfits #= 4 #/\ LongTermProfits #= 4 #/\ MitigateLoss #= 4),
% SD2: StockPrice: Low -> ShortTermProfits --, LongTermProfits --, MitigateLoss --
SD2 #<=> (StockPrice #= 0 #==> ShortTermProfits #= 0 #/\ LongTermProfits #= 0 #/\ MitigateLoss #= 0),
% SD3: InvestmentHorizon: Short -> ShortTermProfits ++, LongTermProfits --
SD3 #<=> (InvestmentHorizon #= 0 #==> ShortTermProfits #= 4 #/\ LongTermProfits #= 0),
% SD4: InvestmentHorizon: Long -> ShortTermProfits --, LongTermProfits ++
SD4 #<=> (InvestmentHorizon #= 1 #==> ShortTermProfits #= 0 #/\ LongTermProfits #= 1),
TotSD #= SD1 + SD2 + SD3 + SD4,
ObjValue #= 1000 * TotC + 100 * TotSD + TotNFR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment