Skip to content

Commit

Permalink
Adicionando projeto final de programação linear
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomatos321 committed Nov 30, 2024
1 parent ed59691 commit e832d4c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions programacao-linear-2024-02/trabalho-final/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
venv/
73 changes: 73 additions & 0 deletions programacao-linear-2024-02/trabalho-final/MastermindSolver.py
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.")
7 changes: 7 additions & 0 deletions programacao-linear-2024-02/trabalho-final/app.py
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()
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.
30 changes: 30 additions & 0 deletions programacao-linear-2024-02/trabalho-final/readme.md
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
3 changes: 3 additions & 0 deletions programacao-linear-2024-02/trabalho-final/referencias.txt
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)
1 change: 1 addition & 0 deletions programacao-linear-2024-02/trabalho-final/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pulp==2.9.0

0 comments on commit e832d4c

Please sign in to comment.