-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adicionando projeto final de programação linear
- Loading branch information
1 parent
ed59691
commit e832d4c
Showing
11 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
venv/ |
73 changes: 73 additions & 0 deletions
73
programacao-linear-2024-02/trabalho-final/MastermindSolver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pulp | ||
|
||
class MastermindSolver: | ||
problem: pulp.LpProblem | ||
x: list[list[pulp.LpVariable]] | ||
tips: int = 0 | ||
positions: range # Posições da senha | ||
colors: range # Cores possíveis | ||
colorMap: dict[int, str] = { | ||
0: "Vermelho", | ||
1: "Azul", | ||
2: "Amarelo", | ||
3: "Verde", | ||
4: "Rosa" | ||
} | ||
|
||
def __init__(self, positionsLen: int = 4, colorsLen: int = 5) -> None: | ||
self.problem = pulp.LpProblem("Mastermind_Solver", pulp.LpMinimize) | ||
self.positions = range(positionsLen) | ||
self.colors = range(colorsLen) | ||
|
||
# Variáveis de decisão: x[i][j] = 1 se a posição i tem a cor j, 0 caso contrário | ||
self.x = [[pulp.LpVariable(f"x_{i}_{j}", cat="Binary") for j in self.colors] for i in self.positions] | ||
|
||
# Restrição 1: Cada posição tem exatamente uma cor | ||
for i in self.positions: | ||
self.problem += pulp.lpSum(self.x[i][j] for j in self.colors) == 1, f"One_Color_Per_Position_{i}" | ||
|
||
# Restrição 2: Cada cor aparece no máximo uma vez na senha | ||
for j in self.colors: | ||
self.problem += pulp.lpSum(self.x[i][j] for i in self.positions) <= 1, f"Unique_Color_{j}" | ||
|
||
def NewTip(self, guess: list[int], rightColors: int, blacks: int, whites: int) -> None: | ||
self.problem +=( | ||
pulp.lpSum( | ||
self.x[i][j] | ||
for i in self.positions | ||
for j in guess | ||
) == rightColors | ||
) | ||
|
||
self.problem += ( | ||
pulp.lpSum(self.x[i][guess[i]] for i in self.positions) == blacks, | ||
f"Feedback_Black_{self.tips}", | ||
) | ||
|
||
self.problem += ( | ||
pulp.lpSum( | ||
self.x[i][j] | ||
for i in self.positions | ||
for j in self.colors | ||
if j in guess and j != guess[i] | ||
) | ||
== whites, | ||
f"Feedback_White_{self.tips}", | ||
) | ||
|
||
self.tips += 1 | ||
|
||
def Run(self) -> None: | ||
# Função objetivo arbitrária (queremos apenas satisfazer as restrições) | ||
self.problem += 0, "Objective" | ||
|
||
# Resolver o problema | ||
self.problem.solve() | ||
|
||
# Mostrar os resultados | ||
if pulp.LpStatus[self.problem.status] == "Optimal": | ||
solution = [[int(self.x[i][j].varValue) for j in self.colors] for i in self.positions] | ||
senha = [self.colorMap[self.colors.index(row.index(1))] for row in solution] | ||
print("Solução encontrada (índices das cores):", senha) | ||
else: | ||
print("Nenhuma solução encontrada.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from MastermindSolver import MastermindSolver | ||
|
||
solver = MastermindSolver(colorsLen=5) | ||
solver.NewTip([0, 1, 2, 3], 3, 2, 1) | ||
solver.NewTip([0, 1, 3, 4], 4, 1, 3) | ||
solver.NewTip([0, 3, 1, 4], 4, 0, 4) | ||
solver.Run() |
Binary file added
BIN
+44.1 KB
programacao-linear-2024-02/trabalho-final/images/exemplo-modelagem.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Trabalho final - Programação Linear | ||
|
||
Programação de um solver para o jogo Senha (do ingles, Mastermind) usandno Programação Linear Inteira | ||
|
||
## Introdução | ||
|
||
## Modelagem | ||
|
||
## Como rodar o projeto | ||
|
||
Considerando que o python3 esteja instalado: | ||
|
||
Passo 1: Crie e inicialize um ambiente virtual | ||
|
||
``` | ||
pythom -m venv venv | ||
.\venv\Scripts\activate.bat | ||
``` | ||
|
||
Passo 2: Instale as dependencias | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
Passo 3: Configure o arquivo app.py com o estado (dicas) de seu jogo | ||
|
||
Passo 4: Execute o arquivo app.py para executar o solver e receber a melhor jogada a ser feita | ||
|
||
## Referencias |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- https://www.ufrgs.br/espmat/disciplinas/novas_abordagens/2009/modulo_II/proposta_IIc.htm | ||
- https://magisterrex.wordpress.com/wp-content/uploads/2014/07/mastermindrules.pdf | ||
- https://en.wikipedia.org/wiki/Mastermind_(board_game) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pulp==2.9.0 |