Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
aa4fd943
Commit
aa4fd943
authored
14 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
Finished third exercise of assignment 3 (comp-algo).
parent
db5c09fd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
algo-comp/ass3/algo-comp-3-answers.tex
+25
-38
25 additions, 38 deletions
algo-comp/ass3/algo-comp-3-answers.tex
algo-comp/ass3/profit.py
+18
-8
18 additions, 8 deletions
algo-comp/ass3/profit.py
with
43 additions
and
46 deletions
algo-comp/ass3/algo-comp-3-answers.tex
+
25
−
38
View file @
aa4fd943
...
@@ -34,37 +34,15 @@
...
@@ -34,37 +34,15 @@
\section
{
Chemische stof
}
\section
{
Chemische stof
}
Een bepaalde chemische stof kan maximaal
$
n
$
keer door een veredelingsproces
gehaald worden, en elke keer dat dat gebeurt gaat er wat van die stof verloren.
De stof kan ook na een willekeurige stap in dat proces verkocht worden. Als een
hoeveelheid
$
x
$
van de stof na stap
$
r
$
verkocht wordt levert dat
$
\phi
_
r
(
x
)
$
op. Als een hoeveelheid
$
y
$
van de stof door het veredelingsproces gehaald
wordt, heb je daarna nog maar
$
a
*
y
$
van de stof over (hier is
$
a <
1
$
de
factor). Bedenk een dynamic programming algoritme die uitrekent hoeveel van de
stof je in welke stap moet verkopen om de maximale winst te scoren. Wat is de
complexiteit van uw algoritme?
Na elke veredeling stijgt de waarde van de stof (er geldt dus
Na elke veredeling stijgt de waarde van de stof (er geldt dus
$
\phi
_{
r
+
1
}
(
x
)
>
\phi
_{
r
}
(
x
)
$
, als
$
x
$
niet verandert). De hoeveelheid
$
x
$
van
$
\phi
_{
r
+
1
}
(
x
)
>
\phi
_{
r
}
(
x
)
$
, als
$
x
$
niet verandert). De hoeveelheid
$
x
$
van
de stof neemt af bij elke veredeling (
$
x
_{
i
+
1
}
=
x
_
i
*
a
$
met
$
a <
1
$
). De
de stof neemt af bij elke veredeling (
$
x
_{
i
+
1
}
=
x
_
i
*
a
$
met
$
a <
1
$
). De
maximale winst van de chemische stof wordt behaald als
$
\phi
_
r
(
x
)
$
maximaal is.
maximale winst van de chemische stof wordt behaald als
$
\phi
_
r
(
x
)
$
maximaal is.
%\begin{lstlisting}[language=python,backgroundcolor=\color{darkgray}]
%i = 0
%
%while i < n:
% phi(r, x)
%
% i += 1
%\end{lstlisting}
\noindent
Stel:
$
x
_
0
$
= 6 en
$
\phi
_
0
(
x
)
=
\frac
{
1
}{
2
}
x
$
. In dit voorbeeld nemen we aan dat
Stel:
$
x
_
0
$
= 6 en
$
\phi
_
0
(
x
)
=
\frac
{
1
}{
2
}
x
$
. In dit voorbeeld nemen we aan dat
$
\phi
_{
r
+
1
}
(
x
)
=
\phi
_
r
(
x
)
*
c
$
met
$
c
=
1
.
4
$
. Tot slot geldt in dit voorbeeld
$
\phi
_{
r
+
1
}
(
x
)
=
\phi
_
r
(
x
)
*
c
$
met
$
c
=
1
.
4
$
. Tot slot geldt in dit voorbeeld
dat
$
a
=
0
.
8
$
. Dat levert de volgende tabel op:
\\
dat
$
a
=
0
.
8
$
. Dat levert de volgende tabel op:
\begin{table}
[H]
\hspace
{
-.45in
}
\begin{tabular}
{
|r|l|l|l|l|l|l|l|l|l|l|
}
\begin{tabular}
{
|r|l|l|l|l|l|l|l|l|l|l|
}
\hline
\hline
&
$
x
_
0
$
&
$
x
_
1
$
&
$
x
_
2
$
&
$
x
_
3
$
&
$
x
_
4
$
&
$
x
_
5
$
&
$
x
_
6
$
&
$
x
_
7
$
&
$
x
_
8
$
&
$
x
_
9
$
\\
&
$
x
_
0
$
&
$
x
_
1
$
&
$
x
_
2
$
&
$
x
_
3
$
&
$
x
_
4
$
&
$
x
_
5
$
&
$
x
_
6
$
&
$
x
_
7
$
&
$
x
_
8
$
&
$
x
_
9
$
\\
...
@@ -89,21 +67,30 @@ $\phi_8(x)$ & 44.27 & 35.42 & 28.34 & 22.67 & 18.13 & 14.51 & 11.61 & 9.28 & 7.
...
@@ -89,21 +67,30 @@ $\phi_8(x)$ & 44.27 & 35.42 & 28.34 & 22.67 & 18.13 & 14.51 & 11.61 & 9.28 & 7.
\hline
\hline
$
\phi
_
9
(
x
)
$
&
61.98
&
49.59
&
39.67
&
31.74
&
25.39
&
20.31
&
16.25
&
13.00
&
10.40
&
8.32
\\
$
\phi
_
9
(
x
)
$
&
61.98
&
49.59
&
39.67
&
31.74
&
25.39
&
20.31
&
16.25
&
13.00
&
10.40
&
8.32
\\
\hline
\hline
%\hline
% & $x_0$ & $x_1$ & $x_2$ & $x_3$ & $x_4$ \\
%\hline
%$\phi_0(x)$ & 8.000 & 6.400 & 5.120 & 4.096 & 3.277 \\
%\hline
%$\phi_1(x)$ & 11.20 & 8.960 & 7.168 & 5.734 & 4.588 \\
%\hline
%$\phi_2(x)$ & 15.68 & 12.54 & 10.04 & 8.028 & 6.423 \\
%\hline
%$\phi_3(x)$ & 21.95 & 17.56 & 14.05 & 11.24 & 8.992 \\
%\hline
%$\phi_4(x)$ & 30.73 & 24.59 & 19.67 & 15.74 & 12.59 \\
%\hline
\end{tabular}
\end{tabular}
\end{table}
\noindent
Uit de tabel valt op te maken dat de cel
$
\phi
_
9
(
x
_
9
)
$
het meest voordelig is.
Daardoor is het volgende algoritme bedacht:
\begin{lstlisting}
[language=python,backgroundcolor=
\color
{
darkgray
}
]
def phi(i, x):
if i == 0:
return 0.5 * x
else:
return phi(i-1, x * 1.4)
def x
_
i(i, x):
if i == 0:
return x
else:
return x
_
i(i-1, x * 0.8)
max = phi(n, x
_
i(x, x0))
\end{lstlisting}
\noindent
De worse-case tijdcomplexiteit van dit algoritme is van de orde
$
\mathcal
{
O
}
(
n
)
$
, omdat het zichzelf recursief aanroept (n keer).
\section
{}
\section
{}
...
...
This diff is collapsed.
Click to expand it.
algo-comp/ass3/profit.py
+
18
−
8
View file @
aa4fd943
...
@@ -29,19 +29,24 @@ for i in range(0,max_i):
...
@@ -29,19 +29,24 @@ for i in range(0,max_i):
lookup
.
append
(
r
)
lookup
.
append
(
r
)
r
=
[]
r
=
[]
print
"
=== calculations ===
"
n
=
10
n
=
10
x_step
=
float
(
x0
)
/
n
x_step
=
float
(
x0
)
/
n
s
=
[]
s
=
[]
left
=
float
(
x0
)
for
i
in
range
(
0
,
n
):
for
i
in
range
(
0
,
n
):
s
.
append
(
phi
(
i
,
x_step
))
left
-=
x_step
s
.
append
(
phi
(
i
,
x_i
(
i
,
x_step
)))
print
s
,
len
(
s
),
sum
(
s
)
print
left
,
s
,
len
(
s
),
sum
(
s
)
print
"
x_n = x0 / n = %.3f =>
"
%
x_step
,
sum
([
phi
(
i
,
x_step
)
for
i
in
range
(
0
,
n
)])
print
"
x_n = x0 / n = %.3f =>
"
%
x_step
,
sum
([
phi
(
i
,
x_i
(
i
,
x_step
)
)
for
i
in
range
(
0
,
n
)])
for
f
in
range
(
2
,
10
):
for
f
in
range
(
2
,
10
,
1
):
x
=
float
(
x0
)
x
=
float
(
x0
)
left
=
x
for
d
in
range
(
0
,
n
):
for
d
in
range
(
0
,
n
):
x
/=
f
x
/=
f
...
@@ -49,10 +54,15 @@ for f in range(2, 10):
...
@@ -49,10 +54,15 @@ for f in range(2, 10):
s
=
[]
s
=
[]
for
i
in
range
(
0
,
n
-
1
):
for
i
in
range
(
0
,
n
-
1
):
print
x
x
*=
f
x
*=
f
s
.
append
(
phi
(
i
,
x
))
left
-=
x
s
.
append
(
phi
(
i
,
x_i
(
i
,
x
)))
s
.
append
(
phi
(
n
,
x
))
print
s
,
len
(
s
)
s
.
append
(
phi
(
n
,
x_i
(
n
,
left
)))
print
left
,
s
,
len
(
s
)
print
"
x_n = x * %d =>
"
%
f
,
sum
(
s
)
print
"
x_n = x * %d =>
"
%
f
,
sum
(
s
)
print
"
all x in phi():
"
,
phi
(
n
,
x_i
(
n
,
float
(
x0
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment