Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,13 +88,30 @@ def buscar_combinacoes(credito_desejado, entrada_desejada, parcela_desejada, adm
|
|
| 88 |
grupos = agrupar_por_administradora(df_filtrado)
|
| 89 |
resultados = []
|
| 90 |
|
|
|
|
| 91 |
for adm, cotas in grupos.items():
|
| 92 |
-
|
| 93 |
-
for
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
total_credito = sum(c['valor_credito'] for c in combinacao)
|
| 96 |
total_entrada_base = sum(c['valor_entrada'] for c in combinacao)
|
| 97 |
-
total_entrada = total_entrada_base + (total_credito * 0.085)
|
| 98 |
total_parcelas = sum(c['valor_parcela'] for c in combinacao)
|
| 99 |
|
| 100 |
if (credito_min <= total_credito <= credito_max and
|
|
@@ -103,9 +120,67 @@ def buscar_combinacoes(credito_desejado, entrada_desejada, parcela_desejada, adm
|
|
| 103 |
|
| 104 |
total_saldo = sum(c['saldo_devedor'] for c in combinacao)
|
| 105 |
valor_final = round(total_entrada + total_saldo, 2)
|
| 106 |
-
diferenca = abs(valor_final - total_credito)
|
| 107 |
|
| 108 |
resultados.append((adm, combinacao, diferenca))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if not resultados:
|
| 111 |
return "🚫 Nenhuma combinação adequada encontrada com os parâmetros fornecidos."
|
|
|
|
| 88 |
grupos = agrupar_por_administradora(df_filtrado)
|
| 89 |
resultados = []
|
| 90 |
|
| 91 |
+
# 🔥 OTIMIZAÇÃO 1: Pré-filtra cotas que individualmente já estão fora dos limites
|
| 92 |
for adm, cotas in grupos.items():
|
| 93 |
+
cotas_filtradas = []
|
| 94 |
+
for cota in cotas:
|
| 95 |
+
# Verifica se a cota individual está dentro dos limites básicos
|
| 96 |
+
if (credito_min <= cota['valor_credito'] <= credito_max and
|
| 97 |
+
parcela_min <= cota['valor_parcela'] <= parcela_max):
|
| 98 |
+
cotas_filtradas.append(cota)
|
| 99 |
+
|
| 100 |
+
if len(cotas_filtradas) < 1:
|
| 101 |
+
continue
|
| 102 |
+
|
| 103 |
+
# 🔥 OTIMIZAÇÃO 2: Limita para no máximo 15 cotas por administradora
|
| 104 |
+
if len(cotas_filtradas) > 15:
|
| 105 |
+
# Mantém as cotas mais próximas do valor desejado
|
| 106 |
+
cotas_filtradas.sort(key=lambda x: abs(x['valor_credito'] - credito_desejado/5))
|
| 107 |
+
cotas_filtradas = cotas_filtradas[:15]
|
| 108 |
+
|
| 109 |
+
# 🔥 OTIMIZAÇÃO 3: Busca primeiro combinações menores (mais rápidas)
|
| 110 |
+
for r in range(1, min(6, len(cotas_filtradas)) + 1): # Primeiro busca até 5 cotas
|
| 111 |
+
for combinacao in itertools.combinations(cotas_filtradas, r):
|
| 112 |
total_credito = sum(c['valor_credito'] for c in combinacao)
|
| 113 |
total_entrada_base = sum(c['valor_entrada'] for c in combinacao)
|
| 114 |
+
total_entrada = total_entrada_base + (total_credito * 0.085)
|
| 115 |
total_parcelas = sum(c['valor_parcela'] for c in combinacao)
|
| 116 |
|
| 117 |
if (credito_min <= total_credito <= credito_max and
|
|
|
|
| 120 |
|
| 121 |
total_saldo = sum(c['saldo_devedor'] for c in combinacao)
|
| 122 |
valor_final = round(total_entrada + total_saldo, 2)
|
| 123 |
+
diferenca = abs(valor_final - total_credito)
|
| 124 |
|
| 125 |
resultados.append((adm, combinacao, diferenca))
|
| 126 |
+
|
| 127 |
+
# 🔥 OTIMIZAÇÃO 4: Limita resultados por administradora
|
| 128 |
+
if len([r for r in resultados if r[0] == adm]) >= 10:
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
if len(resultados) >= 30: # Limite total de resultados
|
| 132 |
+
break
|
| 133 |
+
|
| 134 |
+
if len(resultados) >= 30:
|
| 135 |
+
break
|
| 136 |
+
|
| 137 |
+
# 🔥 Busca combinações maiores apenas se não encontrou muitas ainda
|
| 138 |
+
if len(resultados) < 20:
|
| 139 |
+
for adm, cotas in grupos.items():
|
| 140 |
+
cotas_filtradas = []
|
| 141 |
+
for cota in cotas:
|
| 142 |
+
if (credito_min <= cota['valor_credito'] <= credito_max and
|
| 143 |
+
parcela_min <= cota['valor_parcela'] <= parcela_max):
|
| 144 |
+
cotas_filtradas.append(cota)
|
| 145 |
+
|
| 146 |
+
if len(cotas_filtradas) < 6: # Só busca combinações grandes se tiver cotas suficientes
|
| 147 |
+
continue
|
| 148 |
+
|
| 149 |
+
# Busca combinações de 6 a 10 cotas
|
| 150 |
+
for r in range(6, min(10, len(cotas_filtradas)) + 1):
|
| 151 |
+
# Amostra aleatória para evitar combinações explosivas
|
| 152 |
+
for i in range(0, min(50, len(cotas_filtradas)), max(1, len(cotas_filtradas)//10)):
|
| 153 |
+
for j in range(i + r, min(i + 20, len(cotas_filtradas))):
|
| 154 |
+
combinacao = cotas_filtradas[i:j]
|
| 155 |
+
if len(combinacao) != r:
|
| 156 |
+
continue
|
| 157 |
+
|
| 158 |
+
total_credito = sum(c['valor_credito'] for c in combinacao)
|
| 159 |
+
total_entrada_base = sum(c['valor_entrada'] for c in combinacao)
|
| 160 |
+
total_entrada = total_entrada_base + (total_credito * 0.085)
|
| 161 |
+
total_parcelas = sum(c['valor_parcela'] for c in combinacao)
|
| 162 |
+
|
| 163 |
+
if (credito_min <= total_credito <= credito_max and
|
| 164 |
+
entrada_min <= total_entrada <= entrada_max and
|
| 165 |
+
parcela_min <= total_parcelas <= parcela_max):
|
| 166 |
+
|
| 167 |
+
total_saldo = sum(c['saldo_devedor'] for c in combinacao)
|
| 168 |
+
valor_final = round(total_entrada + total_saldo, 2)
|
| 169 |
+
diferenca = abs(valor_final - total_credito)
|
| 170 |
+
|
| 171 |
+
resultados.append((adm, combinacao, diferenca))
|
| 172 |
+
|
| 173 |
+
if len(resultados) >= 30:
|
| 174 |
+
break
|
| 175 |
+
|
| 176 |
+
if len(resultados) >= 30:
|
| 177 |
+
break
|
| 178 |
+
|
| 179 |
+
if len(resultados) >= 30:
|
| 180 |
+
break
|
| 181 |
+
|
| 182 |
+
if len(resultados) >= 30:
|
| 183 |
+
break
|
| 184 |
|
| 185 |
if not resultados:
|
| 186 |
return "🚫 Nenhuma combinação adequada encontrada com os parâmetros fornecidos."
|