Processing.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import random
  2. from datetime import datetime, timedelta
  3. import math
  4. from datetime import datetime, timedelta
  5. import ast
  6. import os
  7. import time
  8. liste_fichiers = []
  9. """
  10. qui genere un tableau de 30 nombre random (-1,1) appeler generatePoids
  11. """
  12. def generatePoids(nbCouche):
  13. poids = [random.uniform(-1, 1) for _ in range(nbCouche*30)]
  14. return poids
  15. """
  16. un tableau de nb*30 nombre random de (0, nbinf) appel genereateCouche
  17. """
  18. def generateCouche(nbCouche, nbinf):
  19. couche = [random.randint(0, nbinf-1) for _ in range(nbCouche*30)]
  20. return couche
  21. def sigmoid(valeurs, poids):
  22. # Vérification si les listes ont la même longueur
  23. if len(valeurs) != len(poids):
  24. raise ValueError("Les listes de valeurs et de poids doivent avoir la même longueur")
  25. # Calcul de la somme pondérée
  26. somme_ponderee = sum([valeur * poids for valeur, poids in zip(valeurs, poids)])
  27. # Calcul de la sigmoid
  28. resultat = 1 / (1 + math.exp(-somme_ponderee))
  29. return resultat
  30. def pondere(liste):
  31. valeurs_ponderees = []
  32. min_value = min(liste)
  33. max_value = max(liste)
  34. for valeur in liste:
  35. if min_value == max_value:
  36. valeur_ponderee = 0.0
  37. else:
  38. valeur_ponderee = -1 + 2 * (valeur - min_value) / (max_value - min_value)
  39. valeurs_ponderees.append(valeur_ponderee)
  40. return valeurs_ponderees
  41. def lire_valeurs_precedentes(nom_fichier, date):
  42. valeurs_precedentes = []
  43. with open(nom_fichier, 'r') as f:
  44. lignes = f.readlines()
  45. index_date = -1
  46. for i in range(len(lignes)):
  47. ligne_date, valeurs = lignes[i].strip().split(':')
  48. if ligne_date == date:
  49. index_date = i+1
  50. break
  51. if index_date != -1:
  52. debut = max(index_date - 30, 0)
  53. for j in range(debut, index_date):
  54. valeurs = [float(v) for v in lignes[j].strip().split(':')[1].split(',')]
  55. valeurs_precedentes.extend(valeurs)
  56. return valeurs_precedentes
  57. def lirefichier(date):
  58. valeurs_ponderees = []
  59. valeurs_date = []
  60. with open("valeur.txt", 'r') as fichier:
  61. lignes = fichier.readlines()
  62. for ligne in lignes:
  63. FichierVal,index=ligne.split(':')
  64. val = lire_valeurs_precedentes("../Donnee/" + FichierVal + ".txt",date)
  65. valeurs_ponderees+=pondere(val)
  66. valeurs_date.append(val[29])
  67. return valeurs_ponderees,valeurs_date
  68. def processingCouche(nbcouche,coucheinf,poids,couche):
  69. res = []
  70. for i in range(0,nbcouche) :
  71. r = 0
  72. listevaleur= []
  73. listepoids = []
  74. for y in range(0,30):
  75. listevaleur.append(coucheinf[couche[i*30+y]])
  76. listepoids.append(poids[i*30+y])
  77. r=sigmoid(listevaleur,listepoids)
  78. res.append(r)
  79. return pondere(res)
  80. def processing(val,weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4,nbCouche1,nbCouche2,nbCouche3,nbCouche4):
  81. R1=processingCouche(nbCouche1,val,weight1,couche1)
  82. R2=processingCouche(nbCouche2,R1,weight2,couche2)
  83. R3=processingCouche(nbCouche3,R2,weight3,couche3)
  84. return processingCouche(nbCouche4,R3,weight4,couche4)
  85. """
  86. fonction Evaluation en parametre un nombre (res), un nombre (valeur), un nombre (action), un nombre (portefeuille)
  87. si res >0.90 & portefeuille > 2003.9 => retourn action += 2000/valeur et Portefeuille -2003.9
  88. si res <0.90 et > 0.75 & portefeuille > 1003.9 => retourn action += 1000/valeur et Portefeuille -1003.9
  89. si res >0.5 et < 0.75 & portefeuille > 503.9 => retourn action += 500/valeur et Portefeuille -503.9
  90. Inferieur a -0.5 retourn portefeuille += action*valeur -3.9 et action = 0
  91. """
  92. def evaluation(res, valeur, action, portefeu,resAchat1,resAchat2,resAchat3,resVente,Achat1,Achat2,Achat3,Frais):
  93. if res > resAchat1 and portefeu > (Achat1 + Frais):
  94. action += Achat1 / valeur
  95. portefeu -= (Achat1 + Frais)
  96. elif res < resAchat1 and res > resAchat2 and portefeu > (Achat2 + Frais):
  97. action += Achat2 / valeur
  98. portefeu -= (Achat2 + Frais)
  99. elif res > resAchat2 and res < resAchat3 and portefeu > (Achat3 + Frais):
  100. action += Achat3 / valeur
  101. portefeu -= (Achat3 + Frais)
  102. elif res < resVente:
  103. portefeu += (action * valeur) - Frais
  104. action = 0
  105. return action, portefeu
  106. def verification(valeurs_date, Action, portefeuille,PortefeuilleInit):
  107. for i in range(len(Action)):
  108. portefeuille += Action[i] * valeurs_date[i]
  109. pourc = portefeuille/PortefeuilleInit
  110. """
  111. if(verb):
  112. print("Total : ",portefeuille)
  113. print(" soit" ,pourc)
  114. """
  115. return pourc
  116. """
  117. Fonction enregistre qui enregistre dans un fichier dont le nom est composé de la varible nbtour - la variable pourcent:
  118. les tableau (nouvelle ligne a chaque tableau) weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4
  119. """
  120. def enregistre(nbtour,parametre,var, pourcent,weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4):
  121. nom_fichier = str(pourcent)
  122. nom_dossier = "res_" + str(parametre)
  123. if not os.path.exists(nom_dossier):
  124. os.makedirs(nom_dossier)
  125. with open(nom_dossier+"/"+nom_fichier, 'w') as fichier:
  126. fichier.write("NB1: " + str(var[0]) + "\n")
  127. fichier.write("NB2: " + str(var[1]) + "\n")
  128. fichier.write("NB3: " + str(var[2]) + "\n")
  129. fichier.write("resAchat1: " + str(var[3]) + "\n")
  130. fichier.write("resAchat2: " + str(var[4]) + "\n")
  131. fichier.write("resAchat3: " + str(var[5]) + "\n")
  132. fichier.write("resVente: " + str(var[6]) + "\n")
  133. fichier.write("weight1: " + str(weight1) + "\n")
  134. fichier.write("weight2: " + str(weight2) + "\n")
  135. fichier.write("weight3: " + str(weight3) + "\n")
  136. fichier.write("weight4: " + str(weight4) + "\n")
  137. fichier.write("couche1: " + str(couche1) + "\n")
  138. fichier.write("couche2: " + str(couche2) + "\n")
  139. fichier.write("couche3: " + str(couche3) + "\n")
  140. fichier.write("couche4: " + str(couche4) + "\n")
  141. def arret(pourcentage, date_depart, date_actuelle):
  142. date_depart = datetime.strptime(date_depart, "%d-%m-%Y")
  143. date_actuelle = datetime.strptime(date_actuelle, "%d-%m-%Y")
  144. if date_actuelle >= date_depart + timedelta(days=30) and pourcentage < 1.05:
  145. return 1
  146. elif date_actuelle >= date_depart + timedelta(days=30) and pourcentage < 1.10:
  147. return 1
  148. elif date_actuelle >= date_depart + timedelta(days=60) and pourcentage < 1.20:
  149. return 1
  150. else:
  151. return 0