Processing.py 5.4 KB

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