Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jirka Fink
large_scale_optimization
Commits
8ad62126
Commit
8ad62126
authored
Nov 11, 2021
by
Jirka Fink
Browse files
Correction of 5th assignment
parent
2ee0ea48
Changes
2
Hide whitespace changes
Inline
Side-by-side
05-lagrange_assignment/assignment.py
View file @
8ad62126
...
...
@@ -31,25 +31,32 @@ def solve_knapsack(weight, price, capacity):
assert
len
(
weight
)
==
len
(
price
)
# Lists for dynamic programming
max_cost
=
[
0
]
*
(
capacity
+
1
)
# Maximal possible cost reaching every total weight
last_index
=
[
-
1
]
*
(
capacity
+
1
)
# Index of the last added item
max_cost
=
[
-
1
]
*
(
capacity
+
1
)
# Maximal possible cost reaching every total weight
max_cost
[
0
]
=
0
used
=
[
[
False
]
*
(
capacity
+
1
)
for
_
in
range
(
len
(
weight
))
]
for
j
,
w
,
p
in
zip
(
range
(
len
(
weight
)),
weight
,
price
):
# Index, weight and price of each item
if
p
>
0
:
# Ignore items of non-positive items
for
i
in
reversed
(
range
(
capacity
-
w
+
1
)):
# Try to insert the item j to all total weights
if
max_cost
[
i
+
w
]
<
max_cost
[
i
]
+
p
:
if
max_cost
[
i
+
w
]
<
max_cost
[
i
]
+
p
and
max_cost
[
i
]
>=
0
:
# Adding item i increases the cost
max_cost
[
i
+
w
]
=
max_cost
[
i
]
+
p
last_index
[
i
+
w
]
=
j
used
[
j
]
[
i
+
w
]
=
True
solution
=
[]
m
=
numpy
.
argmax
(
max_cost
)
# Total weight with maximal cost
objective
=
max_cost
[
m
]
# Using last_index reconstruct an optimal solution
while
last_index
[
m
]
>=
0
:
solution
.
append
(
last_index
[
m
])
m
-=
weight
[
last_index
[
m
]]
for
j
in
reversed
(
range
(
len
(
weight
))):
if
used
[
j
][
m
]:
solution
.
append
(
j
)
m
-=
weight
[
j
]
# Test at least feasibility
assert
len
(
solution
)
==
len
(
set
(
solution
))
assert
abs
(
objective
-
sum
(
price
[
a
]
for
a
in
solution
))
<
0.001
assert
capacity
>=
sum
(
weight
[
a
]
for
a
in
solution
)
return
(
objective
,
solution
)
...
...
@@ -63,3 +70,5 @@ if __name__ == "__main__":
assert
solve_knapsack
([
4
,
6
,
10
],
[
2
,
5
,
9
],
12
)
==
(
9
,
[
2
])
assert
solve_knapsack
([
4
,
6
,
10
],
[
2
,
5
,
6
],
12
)
==
(
7
,
[
1
,
0
])
assert
solve_knapsack
([
4
,
6
,
10
],
[
2
,
5
,
6
],
15
)
==
(
8
,
[
2
,
0
])
assert
solve_knapsack
([
3
,
3
,
3
,
3
],
[
1
,
2
,
4
,
10
],
100
)
==
(
17
,
[
3
,
2
,
1
,
0
])
print
(
"All tests passed"
)
05-lagrange_assignment/assignment_tests.py
View file @
8ad62126
...
...
@@ -38,16 +38,16 @@ def run_test(filename, bound):
def
main
():
tests
=
{
"first"
:
(
"gap1.txt"
,
2
58.5
,
1
),
"second"
:
(
"gap2.txt"
,
27
4.3878787878788
,
1
),
"third"
:
(
"gap3.txt"
,
43
4.99189237716774
,
1
),
"fourth"
:
(
"gap4.txt"
,
4
17.7556653611704
,
2
),
"fifth"
:
(
"gap5.txt"
,
40
2.8431003822761
,
1
),
"sixth"
:
(
"gap6.txt"
,
52
2.2637096268518
,
2
),
"seventh"
:
(
"gap7.txt"
,
64
3.210566967029
,
3
),
"eigth"
:
(
"gap8.txt"
,
79
3.0298487147554
,
6
),
"ninth"
:
(
"gap9.txt"
,
48
0.81566820276504
,
2
),
"tenth"
:
(
"gap10.txt"
,
63
4.1139438624057
,
5
),
"first"
:
(
"gap1.txt"
,
2
60.0
,
.
5
),
"second"
:
(
"gap2.txt"
,
27
7.0
,
1
),
"third"
:
(
"gap3.txt"
,
43
7.6666666666667
,
1
),
"fourth"
:
(
"gap4.txt"
,
4
23.0
,
2
),
"fifth"
:
(
"gap5.txt"
,
40
3.0
,
1
),
"sixth"
:
(
"gap6.txt"
,
52
5.0
,
2
),
"seventh"
:
(
"gap7.txt"
,
64
6.0
,
5
),
"eigth"
:
(
"gap8.txt"
,
79
6.6
,
10
),
"ninth"
:
(
"gap9.txt"
,
48
2.0
,
2
),
"tenth"
:
(
"gap10.txt"
,
63
7.6666666666666
,
4
),
}
if
len
(
sys
.
argv
)
==
1
:
results
=
PrettyTable
([
"Instance name"
,
"Points"
,
"Filename"
,
"Jobs"
,
"Agents"
,
"Time limit [s]"
,
"Your time [s]"
,
"Evaluation"
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment