-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtango_solver.py
More file actions
143 lines (118 loc) · 5.27 KB
/
tango_solver.py
File metadata and controls
143 lines (118 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# DEPRECATED
from bs4 import BeautifulSoup
import numpy as np
import requests
import pyomo.environ as pyo
import pyautogui
import time
# TODO: AÑADIR BIEN LOS DATOS QUE LEE CON EL request, EL PROBLEMA ES QUE LEE OTRO JUEGO DE TANGO DIFERENTE AL DEL DIA ACTUAL
https = "https://www.linkedin.com/games/tango/"
response = requests.get(https)
soup = BeautifulSoup(response.text, features="html.parser")
# with open("output1.html", "w") as file:
# file.write(str(soup))
model = pyo.ConcreteModel()
model.N = pyo.RangeSet(6)
model.cells = pyo.Var(model.N, model.N, domain=pyo.Binary)
def row_rule(model, r):
return sum(model.cells[r,:]) == 3
model.row_constraint = pyo.Constraint(model.N, rule = row_rule)
def col_rule(model, c):
return sum(model.cells[:, c]) == 3
model.col_constraint = pyo.Constraint(model.N, rule = col_rule)
for i in range(4):
# Add constraint that 2 consecutive suns or moons cannot be placed in rows
def row_two_consec_rule(model, r):
return sum(model.cells[r, j + 1] for j in range(i, i + 3)) <= 2
def row_two_consec2_rule(model, r):
return sum(model.cells[r, j + 1] for j in range(i, i + 3)) >= 1
# Add constraint that 2 consecutive suns or moons cannot be placed in columns
def col_two_consec_rule(model, c):
return sum(model.cells[j + 1, c] for j in range(i, i + 3)) <= 2
def col_two_consec2_rule(model, c):
return sum(model.cells[j + 1, c] for j in range(i, i + 3)) >= 1
# Add constraints to the model
model.add_component(f"row_two_consec_{i}", pyo.Constraint(model.N, rule=row_two_consec_rule))
model.add_component(f"row_two_consec2_{i}", pyo.Constraint(model.N, rule=row_two_consec2_rule))
model.add_component(f"col_two_consec_{i}", pyo.Constraint(model.N, rule=col_two_consec_rule))
model.add_component(f"col_two_consec2_{i}", pyo.Constraint(model.N, rule=col_two_consec2_rule))
# Check the information of the game
for cell_number, cell in enumerate(soup.find_all("div", {"class": "lotka-cell"})):
divs = cell.find_all("div")
# convert cell number into row and column numbers
r = cell_number//6
c = cell_number % 6
for div in divs:
# Check the icon of the cell
if "lotka-cell-content" in div["class"]:
icons = div.find_all("svg")
for icon in icons:
if icon["aria-label"] == "Luna":
# If cell has a moon, 0
model.cells[r,c].fix(0)
elif icon["aria-label"] == "Sol":
# If cell has a sun, 1
model.cells[r,c].fix(1)
elif "lotka-cell-edge--right" in div["class"]:
icons = div.find_all("svg")
for icon in icons:
if icon["aria-label"] == "Cruz":
# Add constraint: cell must be different from the cell to the right
model.add_component(
f"diff_constraint_{r}_{c}",
pyo.Constraint(expr=model.cells[r, c] != model.cells[r, c + 1])
)
elif icon["aria-label"] == "Igual":
# Add constraint: cell must be the same as the cell to the right
model.add_component(
f"equal_constraint_{r}_{c}",
pyo.Constraint(expr=model.cells[r, c] == model.cells[r, c + 1])
)
elif "lotka-cell-edge--bottom" in div["class"]:
for icon in icons:
if icon["aria-label"] == "Cruz":
# Add constraint: cell must be different from the cell to the right
model.add_component(
f"diff_constraint_{r}_{c}",
pyo.Constraint(expr=model.cells[r, c] != model.cells[r+1, c])
)
elif icon["aria-label"] == "Igual":
# Add constraint: cell must be the same as the cell to the right
model.add_component(
f"equal_constraint_{r}_{c}",
pyo.Constraint(expr=model.cells[r, c] == model.cells[r+1, c])
)
# Add a symbolic objective function
model.obj = pyo.Objective(expr = 0)
solver = pyo.SolverFactory("gurobi")
results = solver.solve(model)
result_matrix = np.zeros((6,6))
for r in model.N:
for c in model.N:
result_matrix[r-1, c-1] = model.cells[r,c]()
print(result_matrix)
x_start = 1334
y_start = 870
pyautogui.click(x_start, y_start)
time.sleep(0.7)
top_left_x = 709
top_left_y = 343
bottom_right_x = 1191
bottom_right_y = 826
box_witdh = bottom_right_x - top_left_x
cell_witdh = box_witdh // 6
middle_box = cell_witdh // 2
for r in range(6):
for c in range(6):
if result_matrix[r,c] == 0:
pyautogui.click(
top_left_x + middle_box + cell_witdh * r,
top_left_y + middle_box + cell_witdh * c,
clicks = 2
)
elif result_matrix[r,c] == 1:
pyautogui.click(
top_left_x + middle_box + cell_witdh * r,
top_left_y + middle_box + cell_witdh * c,
clicks = 1
)