final delivery, flake OK

This commit is contained in:
yann 2025-04-01 17:52:33 +02:00
parent fabf2cf56d
commit bfcbfa356a
6 changed files with 2125 additions and 86 deletions

View File

@ -0,0 +1,21 @@
Actions #,Coût par action (en euros),Bénéfice (après 2 ans)
Action-1,20,5%
Action-2,30,10%
Action-3,50,15%
Action-4,70,20%
Action-5,60,17%
Action-6,80,25%
Action-7,22,7%
Action-8,26,11%
Action-9,48,13%
Action-10,34,27%
Action-11,42,17%
Action-12,110,9%
Action-13,38,23%
Action-14,14,1%
Action-15,18,3%
Action-16,8,8%
Action-17,4,12%
Action-18,10,14%
Action-19,24,21%
Action-20,114,18%
1 Actions # Coût par action (en euros) Bénéfice (après 2 ans)
2 Action-1 20 5%
3 Action-2 30 10%
4 Action-3 50 15%
5 Action-4 70 20%
6 Action-5 60 17%
7 Action-6 80 25%
8 Action-7 22 7%
9 Action-8 26 11%
10 Action-9 48 13%
11 Action-10 34 27%
12 Action-11 42 17%
13 Action-12 110 9%
14 Action-13 38 23%
15 Action-14 14 1%
16 Action-15 18 3%
17 Action-16 8 8%
18 Action-17 4 12%
19 Action-18 10 14%
20 Action-19 24 21%
21 Action-20 114 18%

View File

@ -1,6 +1,6 @@
# AlgoInvest&Trade # AlgoInvest&Trade
Choix optimal parmi une liste d'actions Déterminer un choix optimal d'actions caractérisées par un coût et un rendement, en fonction d'un coût maximum pour un profit maximal
## Introduction ## Introduction
@ -15,66 +15,56 @@ Ces instructions vous permettent de :
``` ```
paquets : python 3.11, python3.11-venv, git paquets : python 3.11, python3.11-venv, git
modules : python requests, BeautifulSoup, csv, os modules : csv
``` ```
### Installation ### Installation
Voici les étapes à suivre pour avoir un environnement d'exécution opérationnel : 1. créer l'environnement virtuel :
créer l'environnement virtuel
``` ```
python3.11 -m venv env python3.11 -m venv env
source env/bin/activate source env/bin/activate
``` ```
cloner le dépôt, aller dans le bon dossier 2. cloner le dépôt :
``` ```
git clone https://mcstn.fr/gitea/Yann/Projet2.git git clone https://mcstn.fr/gitea/Yann/Projet7.git
cd Projet2/rendu
```
installer les modules
```
pip install -r requirements.txt
``` ```
## Exécution ## Exécution
Pour l'algorithme bruteforce sur le dataset0,
exécuter la commande : exécuter la commande :
``` ```
python3 main.py python3 bruteforce.py
``` ```
Pour l'algorithme de DP, executer la commande :
```
python3 optimized.py
```
## Résultat ## Résultat
Les fichiers sont placés dans un répertoire "resultat" Optimized traite par défaut les datasets 1 et 2;
Décommenter la ligne du dataset0 dans le main() si besoin
Le programme récupère les catégories sur la page d'accueil de l'URL, puis, pour chaque catégorie :
1. affiche la catégorie traitée, le nombre de catégories restantes, de livres présents, traités au total et restants
2. crée un dossier du nom de la catégorie, y enregistre les images des livres nommées en fonction du titre
3. crée un fichier csv au nom de la catégorie, avec :
- product_page_url
- universal_ product_code (upc)
- title
- price_including_tax
- price_excluding_tax
- number_available
- product_description
- category
- review_rating
- image_url
``` ```
$ time python3.11 main.py $ time python optimized.py
1000 à traiter répartis en 50 catégories.
[ ... ] DATASET 1
Cost: 499.43 €
Profit: 196.84 €
Shares : ['Share-HITN', 'Share-GRUT']
Traitement terminé. DATASET 2
Cost: 497.67 €
Profit: 194.90 €
Shares : ['Share-GEBJ', 'Share-LFXB', 'Share-FWBE', 'Share-PLLK', 'Share-ZKSN', 'Share-ZOFA', 'Share-PATS', 'Share-DWSK', 'Share-ALIY', 'Share-ECAQ', 'Share-FAPS', 'Share-JGTW', 'Share-QLWT', 'Share-OPBR', 'Share-ANFX', 'Share-IJFT', 'Share-JWGF']
real 20m17,783s real 0m0,852s
user 4m30,695s user 0m0,832s
sys 0m3,172s sys 0m0,018s
``` ```
## Auteur ## Auteur

View File

@ -1,5 +1,8 @@
import csv import csv
MAX_COST = 500
FILE = "Liste+dactions+-+P7+Python+-+Feuille+1.csv"
def listFromFile(csv_file): def listFromFile(csv_file):
""" """
get data from a csv file and : get data from a csv file and :
@ -17,6 +20,7 @@ def listFromFile(csv_file):
item[2] = item[1] * float(item[2].strip("%")) / 100 item[2] = item[1] * float(item[2].strip("%")) / 100
return liste return liste
def powerset(itemList): def powerset(itemList):
""" """
Generate every subset (combination) for a given list Generate every subset (combination) for a given list
@ -29,6 +33,7 @@ def powerset(itemList):
result.extend(newsubsets) result.extend(newsubsets)
return result return result
def transformData(dataset): def transformData(dataset):
""" """
Transform in a list of dict with computed values as gain, ratio Transform in a list of dict with computed values as gain, ratio
@ -41,15 +46,17 @@ def transformData(dataset):
'gain': x[1] * x[2] / 100, 'gain': x[1] * x[2] / 100,
'ratio1': x[2] / x[1], 'ratio1': x[2] / x[1],
'ratio2': (x[1] * x[2] / 100) / x[1]} 'ratio2': (x[1] * x[2] / 100) / x[1]}
for x in dataset if for x in dataset if x[1] > 0.0 and x[2] > 0.0]
x[1] > 0.0 and x[2] > 0.0]
return sorted(tmpset, key=lambda x: x['gain'], reverse=True) return sorted(tmpset, key=lambda x: x['gain'], reverse=True)
def selectActions(actionList, maximal_cost): def selectActions(actionList, maximal_cost):
""" """
:param actionList: takes a list of combinations and a max select combination corresponding to max cost
:return: a list of selected combinations where cost is under max :param actionList: list of combinations
:param maximal_cost: maximal cost
:return: a list of selected items
""" """
best = [] best = []
for i in actionList: for i in actionList:
@ -66,17 +73,15 @@ def selectActions(actionList, maximal_cost):
return sortedBest.pop(0) return sortedBest.pop(0)
def main():
actions = listFromFile(FILE)
power_actions = powerset(actions)
selected_actions = selectActions(power_actions, MAX_COST)
# tri des actions sur le rendement
print("Cost:", selected_actions[1], "")
print("Profit: %.2f" % selected_actions[0])
print(f"Shares: {[x[0] for x in selected_actions[2]]}")
actions = listFromFile("/home/b/Documents/OCR/projet7/actions.csv")
power_actions = powerset(actions)
selected_actions = selectActions(power_actions, 500)
print("Nombre d'actions:", len(actions))
print("Nb de combinaisons:", len(power_actions))
#tri des actions sur le rendement
print("Gain: %.2f" % selected_actions[0])
print("Cout:", selected_actions[1], "")
print("Actions sélectionnées:", selected_actions[2:])
if __name__ == '__main__':
main()

1002
dataset1_Python+P7.csv Normal file

File diff suppressed because it is too large Load Diff

1001
dataset2_Python+P7.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,10 @@
import csv import csv
MAX_COST = 500
DATASET1 = "dataset1_Python+P7.csv"
DATASET2 = "dataset2_Python+P7.csv"
DATASET0 = "Liste+dactions+-+P7+Python+-+Feuille+1.csv"
def listFromFile(csv_file): def listFromFile(csv_file):
""" """
@ -15,9 +20,12 @@ def listFromFile(csv_file):
liste.pop(0) liste.pop(0)
for item in liste: for item in liste:
item[1] = float(item[1]) item[1] = float(item[1])
if item[2][-1] == "%":
item[2] = item[2].strip("%")
item[2] = float(item[2]) item[2] = float(item[2])
return liste return liste
def transformData(dataset): def transformData(dataset):
""" """
Transform in a list of dict with computed values as gain, ratio Transform in a list of dict with computed values as gain, ratio
@ -29,11 +37,30 @@ def transformData(dataset):
'rendement': x[2], 'rendement': x[2],
'gain': x[1] * x[2] / 100, 'gain': x[1] * x[2] / 100,
'ratio1': x[2] / x[1], 'ratio1': x[2] / x[1],
'ratio2': (x[1] * x[2] / 100) / x[1]} for x in dataset if 'ratio2': (x[1] * x[2] / 100) / x[1]}
x[1] > 0.0 and x[2] > 0.0] for x in dataset if x[1] > 0.0 and x[2] > 0.0]
return sorted(tmpset, key=lambda x: x['gain'], reverse=True) return sorted(tmpset, key=lambda x: x['gain'], reverse=True)
def get_results(filepath, maximum, nbr):
"""
load, transform data then run the algorithm and print results
:param filepath: full path to csv
:param maximum: maximum cost
:param nbr: set number
:return: print results
"""
action_list = transformData(listFromFile(filepath))
maximum_gain, selection = sacADosFloat(action_list, maximum)
print("\nDATASET", nbr)
print(f"Cost: {sum(x['cout'] for x in selection):.2f}")
print("Profit: %.2f" % maximum_gain)
print(f"Shares : {[x['nom'] for x in selection]}")
def sacADosFloat(actions, maximum_cost): def sacADosFloat(actions, maximum_cost):
""" """
Use dynamic approach Use dynamic approach
@ -50,11 +77,18 @@ def sacADosFloat(actions, maximum_cost):
if i == 0 or w == 0: if i == 0 or w == 0:
table[i][w] = 0.0 table[i][w] = 0.0
elif actions[i-1]['cout'] <= w: elif actions[i-1]['cout'] <= w:
table[i][w] = max(actions[i-1]['gain'] + table[i-1][int(w-actions[i-1]['cout'])], table[i-1][w]) table[i][w] = (
max(
actions[i-1]['gain'] +
table[i-1][int(w-actions[i-1]['cout'])],
table[i-1][w]
)
)
else: else:
table[i][w] = table[i-1][w] table[i][w] = table[i-1][w]
# Select # Selection
w = maximum_cost w = maximum_cost
selected_actions = [] selected_actions = []
for i in range(n, 0, -1): for i in range(n, 0, -1):
@ -65,25 +99,11 @@ def sacADosFloat(actions, maximum_cost):
return table[n][int(maximum_cost)], selected_actions return table[n][int(maximum_cost)], selected_actions
def main():
actions = transformData(listFromFile("/home/b/Documents/OCR/projet7/ph3/dataset1_Python+P7.csv")) # get_results(DATASET0, MAX_COST, 0)
actions2 = transformData(listFromFile("/home/b/Documents/OCR/projet7/ph3/dataset2_Python+P7.csv")) get_results(DATASET1, MAX_COST, 1)
get_results(DATASET2, MAX_COST, 2)
if __name__ == '__main__':
maximum_cost = 500 main()
maximum_gain1, selection1 = sacADosFloat(actions, maximum_cost)
maximum_gain2, selection2 = sacADosFloat(actions2, maximum_cost)
print("\nDATASET 1")
print(f"Cout: {sum(x['cout'] for x in selection1):.2f}")
#print(f"Rendement: {sum((x['cout']*x['rendement']/100)for x in actions_selectionnees):.2f}")
print("Gain: %.2f" % maximum_gain1)
print(f"Actions sélectionnées: {[x['nom'] for x in selection1]}")
print("\nDATASET 2")
print(f"Cout: {sum(x['cout'] for x in selection2):.2f}")
#print(f"Rendement: {sum((x['cout']*x['rendement']/100)for x in actions_selectionnees2):.2f}")
print("Gain: %.2f" % maximum_gain2)
print(f"Actions sélectionnées: {[x['nom'] for x in selection2]}")