Commit d3cbeb17 authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Emptied Makefile.

Merge branch 'master' of ssh://vo20.nl/home/git/repos/uva

Conflicts:
	os/ass4/Makefile
parents 53506716 1715f304
*.log
*.aux
*.swp
*.toc
compilerbouw/
[submodule "compilerbouw"]
path = compilerbouw
url = ssh://sander@vo20.nl/git/compilerbouw.git
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{enumerate}
\usepackage{listings}
\usepackage{url}
\usepackage{float}
\title{Algoritme en complexiteit: opgaves deel 3}
\author{Sander van Veen \& Richard Torenvliet \\ Tadde\"us Kroes \& Jayke Meijer}
% dot graphs
\usepackage{dot2texi}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}
\definecolor{darkgray}{rgb}{0.95,0.95,0.95}
\maketitle
\tableofcontents
\pagebreak
\section{}
\section{}
\section{Chemische stof}
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
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.
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
dat $a = 0.8$. Dat levert de volgende tabel op:
\begin{table}[H]
\begin{tabular}{|r|l|l|l|l|l|l|l|l|l|l|}
\hline
& $x_0$ & $x_1$ & $x_2$ & $x_3$ & $x_4$ & $x_5$ & $x_6$ & $x_7$ & $x_8$ & $x_9$ \\
\hline
$\phi_0(x)$ & 3.00 & 2.40 & 1.92 & 1.54 & 1.23 & 0.98 & 0.79 & 0.63 & 0.50 & 0.40 \\
\hline
$\phi_1(x)$ & 4.20 & 3.36 & 2.69 & 2.15 & 1.72 & 1.38 & 1.10 & 0.88 & 0.70 & 0.56 \\
\hline
$\phi_2(x)$ & 5.88 & 4.70 & 3.76 & 3.01 & 2.41 & 1.93 & 1.54 & 1.23 & 0.99 & 0.79 \\
\hline
$\phi_3(x)$ & 8.23 & 6.59 & 5.27 & 4.21 & 3.37 & 2.70 & 2.16 & 1.73 & 1.38 & 1.10 \\
\hline
$\phi_4(x)$ & 11.52 & 9.22 & 7.38 & 5.90 & 4.72 & 3.78 & 3.02 & 2.42 & 1.93 & 1.55 \\
\hline
$\phi_5(x)$ & 16.13 & 12.91 & 10.33 & 8.26 & 6.61 & 5.29 & 4.23 & 3.38 & 2.71 & 2.17 \\
\hline
$\phi_6(x)$ & 22.59 & 18.07 & 14.46 & 11.57 & 9.25 & 7.40 & 5.92 & 4.74 & 3.79 & 3.03 \\
\hline
$\phi_7(x)$ & 31.62 & 25.30 & 20.24 & 16.19 & 12.95 & 10.36 & 8.29 & 6.63 & 5.31 & 4.24 \\
\hline
$\phi_8(x)$ & 44.27 & 35.42 & 28.34 & 22.67 & 18.13 & 14.51 & 11.61 & 9.28 & 7.43 & 5.94 \\
\hline
$\phi_9(x)$ & 61.98 & 49.59 & 39.67 & 31.74 & 25.39 & 20.31 & 16.25 & 13.00 & 10.40 & 8.32 \\
\hline
\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{}
\end{document}
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)
x0 = 6
max_j = max_i = 10
lookup = []
print r'\hline'
print ' &', ' & '.join(map(lambda x: "$x_%d$" % x, range(0, max_i))), r'\\'
print r'\hline'
for i in range(0,max_i):
r = []
for j in range(0,max_j):
r.append("%.2f" % phi(i, x_i(j, x0)))
print '$\phi_%d(x)$ & ' % i, ' & '.join(r), r'\\'
print r'\hline'
lookup.append(r)
r = []
print " === calculations ==="
n = 10
x_step = float(x0) / n
s = []
left = float(x0)
for i in range(0, n):
left -= x_step
s.append(phi(i, x_i(i, x_step)))
print left, s, len(s), sum(s)
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, 1):
x = float(x0)
left = x
for d in range(0, n):
x /= f
s = []
for i in range(0, n-1):
print x
x *= f
left -= x
s.append(phi(i, x_i(i, x)))
s.append(phi(n, x_i(n, left)))
print left, s, len(s)
print "x_n = x * %d =>" % f, sum(s)
print "all x in phi():", phi(n, x_i(n, float(x0)))
compilerbouw @ 4911d79e
Subproject commit 4911d79e41b41212a4b120bb69ee4a6a492d29e8
function [onb] = norma( v )
onb = v ./ sqrt(dot(v,v));
end
function [Q,R] = obn(V)
[m,n] = size(V)
A = V
Q = zeros(n,n)
for j = 1:n
% take column j of V
%v = V(:,j)
for i = 1:j-1
%a = V(:,i)
%b = v'
%c = a * b
%v = v - dot(v, V(:,i))*v
% v = v - dot(V(:,i), v)*V(:,i)
% v = v - V(:,i)*v' % proj (V(:,i), V(:,j))
% \mathbf{v}_j \leftarrow \mathbf{v}_j - \mathrm{proj}_{\mathbf{v}_{i}}
% \, (\mathbf{v}_j) (remove component in direction vi)
m = (V(:, j)'*V(:, i)) / (V(:, i)'*V(:, i));
V(:, j) = V(:, j) - m * V(:, i);
end
Q(:, j) = V(:, j) / norm(V(:, j));
%v = norma(v)
%V(:,j) = v
end
R = Q' * A
end
%obn([1,1,1;
% 0,1,1;
% 0,0,1])
%function [span, printv2, printw1] = onb2(w1, v2)
% w1 = norma(w1);
% v2 = norma(v2);
% printv2 = v2;
% printw1 = w1;
%
% v2 = cross(w1, v2);
%
% span(:, 1) = w1;
% span(:, 2) = v2;
%end
function span = onb2(w1, v2)
span = [w1, norma(v2 - dot(w1,v2)*w1)];
end
function [onb] = norma( v )
onb = v ./ sqrt(dot(v,v));
end
function [Q,R] = obn(V)
[m,n] = size(V)
A = V
Q = zeros(n,n)
for j = 1:n
% take column j of V
%v = V(:,j)
for i = 1:j-1
%a = V(:,i)
%b = v'
%c = a * b
%v = v - dot(v, V(:,i))*v
% v = v - dot(V(:,i), v)*V(:,i)
% v = v - V(:,i)*v' % proj (V(:,i), V(:,j))
% \mathbf{v}_j \leftarrow \mathbf{v}_j - \mathrm{proj}_{\mathbf{v}_{i}}
% \, (\mathbf{v}_j) (remove component in direction vi)
m = (V(:, j)'*V(:, i)) / (V(:, i)'*V(:, i));
V(:, j) = V(:, j) - m * V(:, i);
end
Q(:, j) = V(:, j) / norm(V(:, j));
%v = norma(v)
%V(:,j) = v
end
R = Q' * A
end
%obn([1,1,1;
% 0,1,1;
% 0,0,1])
%function [span, printv2, printw1] = onb2(w1, v2)
% w1 = norma(w1);
% v2 = norma(v2);
% printv2 = v2;
% printw1 = w1;
%
% v2 = cross(w1, v2);
%
% span(:, 1) = w1;
% span(:, 2) = v2;
%end
function span = onb2(w1, v2)
span = [w1, norma(v2 - dot(w1,v2)*w1)];
end
ass*/ass*
*.o
PROG = philosophers
CC = gcc
CFLAGS = -ansi -Wall -Wextra -O3 -pthread
OFILES = host.o
RM = rm -f
$(PROG): $(OFILES)
$(CC) -o $(PROG) $(OFILES) $(CFLAGS)
%.o: %.c
$(CC) -c $< $(CFLAGS)
clean:
$(RM) *.o $(PROG)
/*
* Assignment 4 of Operating Systems: PThread simulation.
*
* Sander van Veen (6167969) and Taddeus Kroes (6054129).
* <sandervv@gmail.com> and <taddeuskroes@hotmail.com>.
*
* Submission date: 21 november 2010.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include "main.h"
int forks_len;
pthread_mutex_t *forks;
pthread_cond_t wait_threshold;
pthread_barrier_t wait_barrier;
int diner_finished = 0;
/*
* Release fork taken by a philosopher.
*/
static inline void release_fork(diner_stats *stats, int f) {
if(diner_finished)
return;
// Unlock left or right fork.
stats->locked ^= 1 << (f % 2);
pthread_mutex_unlock(forks+f);
}
/*
* Try to claim a fork, but do not wait if the fork cannot be claimed.
* This call is therefore non-blocking.
*/
static inline int try_take_fork(diner_stats *stats, int f) {
// Try to lock left or right fork.
if(diner_finished || !pthread_mutex_trylock(forks+f))
return 1;
stats->locked |= 1 << (f % 2);
stats->forks++;
return 0;
}
/*
* Claim a fork, and wait (blocking) if the fork is already taken.
*/
static inline void take_fork(diner_stats *stats, int f) {
if(diner_finished)
return;
// Lock left or right fork.
pthread_mutex_lock(forks+f);
stats->locked |= 1 << (f % 2);
stats->forks++;
}
#define PHILO_EATING \
if(diner_finished) \
return 1; \
\
stats->meals++;
static const char *philo_type_names[] = {"left", "right", "optimistic", "shy",
"random"};
static const philo_t philo_types[] = {&philo_left, &philo_right, &philo_shy, \
&philo_optimistic, &philo_random};
static const int philo_type_count = sizeof(philo_types) / sizeof(philo_t);
/*
* Philosopher favoring the left fork above the right fork. He will wait for
* both forks, and wouldn't release the first while he is waiting on the second.
*/
int philo_left(diner_stats *stats, int id) {
// Let the philosopher take two forks (first left, then right).
take_fork(stats, id);
take_fork(stats, (id+1) % forks_len);
PHILO_EATING;
// Release both forks.
release_fork(stats, (id+1) % forks_len);
release_fork(stats, id);
return 0;
}
/*
* Philosopher favoring the right fork above the left fork. He will wait for
* both forks, and wouldn't release the first while he is waiting on the second.
*/
int philo_right(diner_stats *stats, int id) {
// Let the philosopher take two forks (first right, then left).
take_fork(stats, (id+1) % forks_len);
take_fork(stats, id);
PHILO_EATING;
// Release both forks.
release_fork(stats, id);
release_fork(stats, (id+1) % forks_len);
return 0;
}
/*
* Philosopher with optimistic behaviour: try to take the left or right fork,
* and if one fork is taken succesfully, wait till the second fork is released.
*/
int philo_optimistic(diner_stats *stats, int id) {
// Let the philosopher take the left fork of the two forks.
if(!try_take_fork(stats, id)) {
if(diner_finished)
return 1;
// Left fork is already taken, so try to take the right fork.
if(!try_take_fork(stats, (id+1) % forks_len)) {
if(diner_finished)
return 1;
// No forks available, be faster next time!
sched_yield();
}
else {
// Grabbed the right fork successfully.
if(diner_finished)
return 1;
take_fork(stats, id);
PHILO_EATING;
// Release both forks.
release_fork(stats, id);
release_fork(stats, (id+1) % forks_len);
}
}
else {
// Grabbed the left fork successfully.
take_fork(stats, (id+1) % forks_len);
PHILO_EATING;
// Release both forks.
release_fork(stats, id);
release_fork(stats, (id+1) % forks_len);
}
return 0;
}
/*
* Philosopher with shy behaviour: try again next time, when taking one of the
* forks fails.
*/
int philo_shy(diner_stats *stats, int id) {
// Try to take the right fork.
if(!try_take_fork(stats, (id+1) % forks_len)) {
if(diner_finished)
return 1;
// Failed to take fork, so try again next time.
sched_yield();
}
// Let the philosopher try to take the left fork.
else if(!try_take_fork(stats, id)) {
if(diner_finished)
return 1;
// Release the right fork and try again next time.
release_fork(stats, (id+1) % forks_len);
sched_yield();
}
else {
// Grabbed the left and right fork successfully.
PHILO_EATING;
// Release both forks.
release_fork(stats, id);
release_fork(stats, (id+1) % forks_len);
}
return 0;
}
/*
* Philosopher with random behaviour: change each iteration to one of the other
* philosopher behaviours.
*/
int philo_random(diner_stats *stats, int id) {
// Pick a behaviour randomly (except for the "random" behaviour, since that
// would be recursive and thus useless to simulate).
return (*philo_types[rand() % (philo_type_count - 1)])(stats, id);
}
void *philo_start(void *raw_stats) {
diner_stats *stats = (diner_stats *) raw_stats;
int id = stats->id,
type = rand() % philo_type_count;
printf("P #%u: hello everybody, i'm %s!\n", id, philo_type_names[type]);
// Philosophers wait for each other, before they can start eating diner.
pthread_barrier_wait(&wait_barrier);
while(!diner_finished && !(*philo_types[type])(stats, id));
printf("P #%u: i'm leaving, after paying the bill.\n", id);
pthread_exit(NULL);
}
void host_start(int philos) {
if( philos < 2 ) {
fprintf(stderr, "Invalid philosopher count.\n");
exit(EXIT_FAILURE);
}
pthread_attr_t attr;
pthread_t *threads = malloc(philos * sizeof(pthread_t));
forks = malloc(philos * sizeof(pthread_mutex_t));
diner_stats *stats = malloc(philos * sizeof(diner_stats));
forks_len = philos;
void *status;
int rc;
// POSIX standard specifies that threads are joinable by default, but
// unfortunately, not all pthread implementations set threads as joinable by
// default. Therefore, set the thread's detach state.
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Let all philosophers wait before they can eat diner.
pthread_barrier_init(&wait_barrier, NULL, philos);
// Create the cutlery (mutexes) and initialise the statistics.
for(int i = 0; i < philos; i++) {
pthread_mutex_init(&forks[i], NULL);
stats[i].meals = 0;
stats[i].forks = 0;
stats[i].id = i;
stats[i].locked = 0;
}
// Invite the philosophers.
for(int i = 0; i < philos; i++) {
printf("Host: inviting philo #%d\n", i);
rc = pthread_create(threads+i, &attr, philo_start, (void*)&stats[i]);
if(rc) {
fprintf(stderr, "pthread_create() returned: %d\n", rc);
exit(-1);
}
}
puts("Host: invitations done.");
struct timeval wct;
gettimeofday(&wct, NULL);
double start_time = wct.tv_sec + wct.tv_usec / 1e6;
// Duration of the diner party is 10 seconds (see main.h). This loop will
// prevent the philosphers from exceeding this time limit.
do {
// Check each 10 ms for deadlock.
usleep(10000);
// If an deadlock occured (all philosophers are waiting on their left or
// right fork), finish the diner party immidiately. It is unclear in the
// assignment, if the philosophers should leave the diner party, or if
// they only had to release their claimed forks. I choose the first
// assumption: leave the party. If they start arguing which fork is
// theirs, there's no party anymore.
int p;
for(p = 0; p < philos && (stats[p].locked & 1
|| stats[p].locked & 2); p++);
if( p == philos ) {
puts("Host: deadlock occured.");
diner_finished = 1;
break;
}
gettimeofday(&wct, NULL);
} while(wct.tv_sec + wct.tv_usec / 1e6 - start_time < TIME_LIMIT);
diner_finished = 1;
puts("Host: diner is finished.");
for(int i = 0; i < philos; i++) {
// Reclaim all cutlery (mutexes) in reverse order.
for(int p = philos; p >= i; p--)
pthread_mutex_unlock(&forks[p]);
// Wait on the other philosophers.
if( (rc = pthread_join(threads[i], &status)) ) {
fprintf(stderr, "return code from pthread_join() is %d\n", rc);
exit(EXIT_FAILURE);
}
printf("Philo #%d ate %d meal(s) and grabbed %d fork(s).\n",
i, stats[i].meals, stats[i].forks);
pthread_mutex_destroy(&forks[i]);
}
puts("Host: philosophers are done.");
pthread_barrier_destroy(&wait_barrier);
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
/*
* Start the philosopher diner problem simulation. By default two philosophers
* are simulated. The first commandline argument changes the total philosophers
* being simulated. If the argument is less than two, the philosopher count is
* set to the default count of two.
*/
int main(int argc, const char **argv) {
// Get system clock's milliseconds and set it as seed to ensure a
// more "pseudo-random" simulation.
struct timeval wtc;
gettimeofday(&wtc, NULL);
srand(wtc.tv_usec);
host_start( argc > 1 ? atoi(argv[1]) : 2 );
}
/*
* Assignment 4 of Operating Systems: PThread simulation.
*
* Sander van Veen (6167969) and Taddeus Kroes (6054129).
* <sandervv@gmail.com> and <taddeuskroes@hotmail.com>.
*
* Submission date: 21 november 2010.
*/
typedef struct diner_stats {
unsigned int meals;
unsigned int forks;
unsigned int id;
unsigned int locked;
} diner_stats;
/*
* Duration of the diner party (in seconds). When this time limit is reached,
* all philosophers are requested to pay their bill and leave the party.
*/
#define TIME_LIMIT 10.f
int philo_left(diner_stats *stats, int id);
int philo_right(diner_stats *stats, int id);
int philo_optimistic(diner_stats *stats, int id);
int philo_shy(diner_stats *stats, int id);
int philo_random(diner_stats *stats, int id);
typedef int(*philo_t)(diner_stats *, int);
TT\documentclass[twocolumn]{article}
\documentclass[twocolumn]{article}
\usepackage{url}
\title{Web-based digital examination for specific learning areas}
......
......@@ -22,7 +22,7 @@ if version > 580
endif
let g:colors_name="darkspectrum"
hi Normal guifg=#efefef guibg=#2A2A2A
hi Normal guifg=#efefef guibg=#171717
" highlight groups
hi Cursor guibg=#ffffff guifg=#000000
......@@ -123,8 +123,8 @@ hi link schemeFunc Statement
"hi link bashSpecialVariables Constant
" tabs (non gui)
hi TabLine guifg=#A3A3A3 guibg=#202020 gui=none
hi TabLineFill guifg=#535353 guibg=#202020 gui=none
hi TabLine guifg=#A3A3A3 guibg=#171717 gui=none
hi TabLineFill guifg=#535353 guibg=#171717 gui=none
hi TabLineSel guifg=#FFFFFF gui=bold
"hi TabLineSel guifg=#FFFFFF guibg=#000000 gui=bold
" vim: sw=4 ts=4
......@@ -31,6 +31,34 @@ augroup vimrc_autocmds
autocmd BufEnter * match OverLength /\%81v.*/
augroup END
" configure tags - add additional tags here or comment out not-used ones
" cd /usr/include
" ctags -R --sort=yes --fields=+iaS -f ~/.vim/tags/stdc stdio.h stdlib.h \
" pthread.h math.h assert.h errno.h malloc.h strings.h fcntl.h unistd.h
" ctags -R --sort=yes --fields=+iaS -f ~/.vim/tags/gl GL/
set tags+=~/.vim/tags/stdc
set tags+=~/.vim/tags/gl
" build tags of your own project with CTRL+F12
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
"noremap <F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>
"inoremap <F12> <Esc>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>
" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_MayCompleteDot = 1
let OmniCpp_MayCompleteArrow = 1
let OmniCpp_MayCompleteScope = 1
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
"set completeopt=menuone,menu,longest,preview
set completeopt=menuone,menu " ,longest,preview
set nocp
filetype plugin on
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment