Skip to content

Commit b41df23

Browse files
Merge pull request #33 from LearningToOptimize/copilot/fix-32
Fix sudoku exercise answer checking and variable naming conflicts in optimization_basics.jl
2 parents 6ea67e6 + 99dd777 commit b41df23

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

class01/background_materials/math_basics.html

Lines changed: 6 additions & 8 deletions
Large diffs are not rendered by default.

class01/background_materials/math_basics.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ md"
3939
| Lecturer | : | Rosemberg, Andrew |
4040
| Date | : | 28 of July, 2025 |
4141
42+
Special thanks to **Guancheng Qiu** for helping fix some of the code!
43+
4244
# Background Math (_Welcome to Pluto!_)
4345
4446
This background material will use Pluto!

class01/background_materials/optimization_basics.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

class01/background_materials/optimization_basics.jl

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ md"""
3737
| Lecturer | : | Rosemberg, Andrew |
3838
| Date | : | 28 of July, 2025 |
3939
40+
Special thanks to **Guancheng Qiu** for helping fix some of the code!
41+
4042
"""
4143

4244
# ╔═╡ eeceb82e-abfb-4502-bcfb-6c9f76a0879d
@@ -436,21 +438,32 @@ begin
436438
[7 1 3 9 2 4 8 5 6];
437439
[9 6 1 5 3 7 2 8 4];
438440
[2 8 7 4 1 9 6 3 5];
439-
[3 4 5 2 8 6 1 7 9]])
440-
441-
anss = missing
442-
try
443-
anss = (
444-
x_ss = haskey(sudoku, :x_s) ? JuMP.value.(sudoku[:x_s]) : missing,
445-
)
446-
catch
447-
anss = missing
441+
[3 4 5 2 8 6 1 7 9]],)
442+
443+
anss = (;
444+
x_ss = haskey(sudoku, :x_s) && JuMP.is_solved_and_feasible(sudoku) ? JuMP.value.(sudoku[:x_s]) : missing
445+
)
446+
447+
# Convert 3D binary matrix to 2D solution matrix
448+
function convert_3d_to_solution(x_3d)
449+
if ismissing(x_3d)
450+
return missing
451+
end
452+
solution = zeros(Int, 9, 9)
453+
for i in 1:9, j in 1:9, k in 1:9
454+
if x_3d[i, j, k] 1.0
455+
solution[i, j] = k
456+
end
457+
end
458+
return solution
448459
end
449460

450-
goods = !ismissing(anss) &&
451-
all(isapprox.(anss.x_ss, ground_truth_s.x_ss; atol=1e-3))
461+
solution_matrix = ismissing(anss) ? missing : convert_3d_to_solution(anss.x_ss)
462+
463+
goods = !ismissing(anss) && !ismissing(solution_matrix) &&
464+
all(isapprox.(solution_matrix, ground_truth_s.x_ss; atol=1e-3))
452465

453-
if ismissing(anss)
466+
if ismissing(anss.x_ss)
454467
still_missing()
455468
elseif goods
456469
correct()
@@ -578,8 +591,8 @@ begin
578591
model_nlp = Model(Ipopt.Optimizer)
579592

580593
# Required named variables
581-
@variable(model_nlp, x)
582-
@variable(model_nlp, y)
594+
@variable(model_nlp, x_nlp)
595+
@variable(model_nlp, y_nlp)
583596

584597
# --- YOUR CODE HERE ---
585598

@@ -707,7 +720,7 @@ begin
707720
# Decide which badge to show
708721
if ismissing(ansd) # nothing yet
709722
still_missing()
710-
elseif x == 25.0
723+
elseif ansd == 25.0
711724
correct()
712725
else
713726
keep_working()
@@ -721,8 +734,8 @@ begin
721734
ans3=missing
722735
try
723736
ans3 = (
724-
x = safeval(model_nlp, :x),
725-
y = safeval(model_nlp, :y),
737+
x = safeval(model_nlp, :x_nlp),
738+
y = safeval(model_nlp, :y_nlp),
726739
obj = objective_value(model_nlp),
727740
)
728741
catch
@@ -799,7 +812,7 @@ end
799812
# ╟─bca712e4-3f1c-467e-9209-e535aed5ab0a
800813
# ╟─3997d993-0a31-435e-86cd-50242746c305
801814
# ╠═3f56ec63-1fa6-403c-8d2a-1990382b97ae
802-
# ╟─0e8ed625-df85-4bd2-8b16-b475a72df566
815+
# ╠═0e8ed625-df85-4bd2-8b16-b475a72df566
803816
# ╟─fa5785a1-7274-4524-9e54-895d46e83861
804817
# ╟─5e3444d0-8333-4f51-9146-d3d9625fe2e9
805818
# ╠═0e190de3-da60-41e9-9da5-5a0c7fefd1d7

0 commit comments

Comments
 (0)