| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import random
- from datetime import datetime, timedelta
- import math
- from Global import *
- from datetime import datetime, timedelta
- import ast
- import os
- PortefeuilleInit = 80000
- liste_fichiers = []
- """
- qui genere un tableau de 30 nombre random (-1,1) appeler generatePoids
- """
- def generatePoids(nbCouche):
- poids = [random.uniform(-1, 1) for _ in range(nbCouche)]
- return poids
- """
- un tableau de nb*30 nombre random de (0, nbinf) appel genereateCouche
- """
- def generateCouche(nb, nbinf):
- couche = [random.randint(0, nbinf-1) for _ in range(nb*30)]
- return couche
- def sigmoid(valeurs, poids):
- # Vérification si les listes ont la même longueur
- if len(valeurs) != len(poids):
- raise ValueError("Les listes de valeurs et de poids doivent avoir la même longueur")
-
- # Calcul de la somme pondérée
- somme_ponderee = sum([valeur * poids for valeur, poids in zip(valeurs, poids)])
-
- # Calcul de la sigmoid
- resultat = 1 / (1 + math.exp(-somme_ponderee))
-
- return resultat
-
- def pondere(liste):
- valeurs_ponderees = []
- min_value = min(liste)
- max_value = max(liste)
- for valeur in liste:
- if min_value == max_value:
- valeur_ponderee = 0.0
- else:
- valeur_ponderee = -1 + 2 * (valeur - min_value) / (max_value - min_value)
- valeurs_ponderees.append(valeur_ponderee)
- return valeurs_ponderees
-
- def lire_valeurs_precedentes(nom_fichier, date):
- valeurs_precedentes = []
- with open(nom_fichier, 'r') as f:
-
- lignes = f.readlines()
- index_date = -1
- for i in range(len(lignes)):
- ligne_date, valeurs = lignes[i].strip().split(':')
- if ligne_date == date:
- index_date = i+1
- break
- if index_date != -1:
- debut = max(index_date - 30, 0)
- for j in range(debut, index_date):
- valeurs = [float(v) for v in lignes[j].strip().split(':')[1].split(',')]
- valeurs_precedentes.extend(valeurs)
- return valeurs_precedentes
- def lirefichier(date):
- valeurs_ponderees = []
- valeurs_date = []
- with open("valeur.txt", 'r') as fichier:
- lignes = fichier.readlines()
- for ligne in lignes:
- FichierVal,index=ligne.split(':')
- val = lire_valeurs_precedentes("../Donnee/" + FichierVal + ".txt",date)
- # ~ print(FichierVal)
- valeurs_ponderees+=pondere(val)
- valeurs_date.append(val[29])
- return valeurs_ponderees,valeurs_date
-
- def processingCouche(nbcouche,coucheinf,poids,couche):
- res = []
- for i in range(0,nbcouche) :
- r = 0
- listevaleur= []
- listepoids = []
- for y in range(0,30):
- listevaleur.append(coucheinf[couche[i*30+y]])
- listepoids.append(poids[i])
- r=sigmoid(listevaleur,listepoids)
- res.append(r)
- return pondere(res)
-
- def processing(val,weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4):
- R1=processingCouche(nbCouche1,val,weight1,couche1)
- R2=processingCouche(nbCouche2,R1,weight2,couche2)
- R3=processingCouche(nbCouche3,R2,weight3,couche3)
- return processingCouche(nbCouche4,R3,weight4,couche4)
-
-
- """
- fonction Evaluation en parametre un nombre (res), un nombre (valeur), un nombre (action), un nombre (portefeuille)
- si res >0.90 & portefeuille > 2003.9 => retourn action += 2000/valeur et Portefeuille -2003.9
- si res <0.90 et > 0.75 & portefeuille > 1003.9 => retourn action += 1000/valeur et Portefeuille -1003.9
- si res >0.5 et < 0.75 & portefeuille > 503.9 => retourn action += 500/valeur et Portefeuille -503.9
- Inferieur a -0.5 retourn portefeuille += action*valeur -3.9 et action = 0
- """
- def evaluation(res, valeur, action, portefeu):
- if res > 0.95 and portefeu > 2003.9:
- action += 2000 / valeur
- portefeu -= 2003.9
- elif res < 0.95 and res > 0.90 and portefeu > 1003.9:
- action += 1000 / valeur
- portefeu -= 1003.9
- elif res > 0.90 and res < 0.80 and portefeu > 503.9:
- action += 500 / valeur
- portefeu -= 503.9
- elif res < -0.5:
- portefeu += action * valeur -3.9
- action = 0
- return action, portefeu
-
-
- def verification(valeurs_date, Action, portefeuille):
- for i in range(len(Action)):
- portefeuille += Action[i] * valeurs_date[i]
-
- pourc = portefeuille/PortefeuilleInit
- if(verb):
- print("Total : ",portefeuille)
- print(" soit" ,pourc)
- return pourc
- """
- Fonction enregistre qui enregistre dans un fichier dont le nom est composé de la varible nbtour - la variable pourcent:
- les tableau (nouvelle ligne a chaque tableau) weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4
- """
- def enregistre(nbtour, pourcent,weight1,weight2,weight3,weight4,couche1,couche2,couche3,couche4):
- nom_fichier = str(pourcent)
-
- if not os.path.exists('res'):
- os.makedirs('res')
- with open("res/"+nom_fichier, 'w') as fichier:
- fichier.write("weight1: " + str(weight1) + "\n")
- fichier.write("weight2: " + str(weight2) + "\n")
- fichier.write("weight3: " + str(weight3) + "\n")
- fichier.write("weight4: " + str(weight4) + "\n")
- fichier.write("couche1: " + str(couche1) + "\n")
- fichier.write("couche2: " + str(couche2) + "\n")
- fichier.write("couche3: " + str(couche3) + "\n")
- fichier.write("couche4: " + str(couche4) + "\n")
-
- def arret(pourcentage, date_depart, date_actuelle):
- date_depart = datetime.strptime(date_depart, "%d-%m-%Y")
- date_actuelle = datetime.strptime(date_actuelle, "%d-%m-%Y")
- if date_actuelle >= date_depart + timedelta(days=30) and pourcentage < 1.05:
- return 1
- elif date_actuelle >= date_depart + timedelta(days=30) and pourcentage < 1.10:
- return 1
- elif date_actuelle >= date_depart + timedelta(days=60) and pourcentage < 1.20:
- return 1
- else:
- return 0
|