Skip to content

Commit 8ccc376

Browse files
committed
Update the documentation so that the examples are runnable by copy-pasting
1 parent 060e8e1 commit 8ccc376

File tree

11 files changed

+151
-201
lines changed

11 files changed

+151
-201
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### 0.2.10 (Upcoming Release)
22

33
- Update docs example in travelingsalesman.
4-
4+
- Update the documentation so that the examples are runnable by copy-pasting.
55

66
### 0.2.9
77

src/assignment.jl

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,15 @@ Solves an assignment problem given by an object of in type `AssignmentProblem`.
119119
# Example
120120
121121
```julia
122-
julia> mat = [
123-
4 8 1;
124-
3 1 9;
125-
1 6 7;
126-
];
122+
mat = [
123+
4 8 1;
124+
3 1 9;
125+
1 6 7;
126+
];
127127
128-
julia> problem = AssignmentProblem(mat);
128+
problem = AssignmentProblem(mat);
129129
130-
julia> result = solve(problem);
131-
132-
julia> result.solution
133-
134-
3×3 Matrix{Float64}:
135-
0.0 0.0 1.0
136-
0.0 1.0 0.0
137-
1.0 0.0 0.0
138-
139-
julia> result.cost
140-
141-
3.0
130+
result = solve(problem);
142131
```
143132
"""
144133
function solve(a::AssignmentProblem)::AssignmentResult

src/cpm.jl

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ The object that represents an activity in CPM (Critical Path Method).
2727
# Example
2828
2929
```julia
30-
julia> A = CpmActivity("A", 2, []);
30+
A = CpmActivity("A", 2, []);
3131
32-
julia> B = CpmActivity("B", 3, []);
32+
B = CpmActivity("B", 3, []);
3333
34-
julia> C = CpmActivity("C", 2, [A, B]);
34+
C = CpmActivity("C", 2, [A, B]);
3535
3636
```
3737
"""
@@ -96,9 +96,9 @@ The object that represents an activity in PERT (Program Evaluation and Review Te
9696
9797
# Example
9898
```julia
99-
julia> A = PertActivity("A", 1, 2, 3);
100-
julia> B = PertActivity("B", 3, 3, 4);
101-
julia> C = PertActivity("C", 5, 6, 7, [A, B]);
99+
A = PertActivity("A", 1, 2, 3);
100+
B = PertActivity("B", 3, 3, 4);
101+
C = PertActivity("C", 5, 6, 7, [A, B]);
102102
```
103103
"""
104104
struct PertActivity
@@ -218,32 +218,23 @@ Calculates CPM (Critical Path Method) and reports the critical path for a given
218218
# Example
219219
220220
```julia
221-
julia> A = CpmActivity("A", 2);
222-
julia> B = CpmActivity("B", 3);
223-
julia> C = CpmActivity("C", 2, [A]);
224-
julia> D = CpmActivity("D", 3, [B]);
225-
julia> E = CpmActivity("E", 2, [B]);
226-
julia> F = CpmActivity("F", 3, [C, D]);
227-
julia> G = CpmActivity("G", 7, [E]);
228-
julia> H = CpmActivity("H", 5, [E]);
229-
julia> I = CpmActivity("I", 6, [G, F]);
230-
julia> J = CpmActivity("J", 2, [C, D]);
231-
232-
julia> activities = [A, B, C, D, E, F, G, H, I, J];
233-
234-
julia> problem = CpmProblem(activities);
235-
236-
julia> result = solve(problem);
237-
238-
julia> result.pathstr
239-
4-element Vector{String}:
240-
"B"
241-
"E"
242-
"G"
243-
"I"
244-
245-
julia> result.path == [B, E, G, I]
246-
true
221+
A = CpmActivity("A", 2);
222+
B = CpmActivity("B", 3);
223+
C = CpmActivity("C", 2, [A]);
224+
D = CpmActivity("D", 3, [B]);
225+
E = CpmActivity("E", 2, [B]);
226+
F = CpmActivity("F", 3, [C, D]);
227+
G = CpmActivity("G", 7, [E]);
228+
H = CpmActivity("H", 5, [E]);
229+
I = CpmActivity("I", 6, [G, F]);
230+
J = CpmActivity("J", 2, [C, D]);
231+
232+
activities = [A, B, C, D, E, F, G, H, I, J];
233+
234+
problem = CpmProblem(activities);
235+
236+
result = solve(problem);
237+
247238
```
248239
"""
249240
function solve(problem::CpmProblem)::CpmResult
@@ -315,31 +306,20 @@ end
315306
# Example
316307
317308
```julia
318-
julia> A = PertActivity("A", 1, 2, 3)
319-
PertActivity("A", 1.0, 2.0, 3.0, PertActivity[])
320-
321-
julia> B = PertActivity("B", 3, 3, 3)
322-
PertActivity("B", 3.0, 3.0, 3.0, PertActivity[])
309+
A = PertActivity("A", 1, 2, 3)
323310
324-
julia> C = PertActivity("C", 5, 5, 5, [A, B])
325-
PertActivity("C", 5.0, 5.0, 5.0, PertActivity[PertActivity("A", 1.0, 2.0, 3.0, PertActivity[]), PertActivity("B", 3.0, 3.0, 3.0, PertActivity[])])
311+
B = PertActivity("B", 3, 3, 3)
326312
327-
julia> activities = [A, B, C]
328-
3-element Vector{PertActivity}:
329-
PertActivity("A", 1.0, 2.0, 3.0, PertActivity[])
330-
PertActivity("B", 3.0, 3.0, 3.0, PertActivity[])
331-
PertActivity("C", 5.0, 5.0, 5.0, PertActivity[PertActivity("A", 1.0, 2.0, 3.0, PertActivity[]), PertActivity("B", 3.0, 3.0, 3.0, PertActivity[])])
313+
C = PertActivity("C", 5, 5, 5, [A, B])
332314
333-
julia> problem = PertProblem(activities);
315+
activities = [A, B, C]
334316
335-
julia> result = pert(activities)
336-
PertResult(PertActivity[PertActivity("B", 3.0, 3.0, 3.0, PertActivity[]), PertActivity("C", 5.0, 5.0, 5.0, PertActivity[PertActivity("A", 1.0, 2.0, 3.0, PertActivity[]), PertActivity("B", 3.0, 3.0, 3.0, PertActivity[])])], 8.0, 0.0)
317+
problem = PertProblem(activities);
337318
338-
julia> result.mean
339-
8.0
319+
result = pert(activities)
340320
341-
julia> result.stddev
342-
0.0
321+
println(result.mean)
322+
println(result.stddev)
343323
```
344324
"""
345325
function solve(problem::PertProblem)::PertResult

src/johnsons.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ Given a problem containing a matrix of times and a permutation of the jobs, retu
250250
251251
```julia
252252
253-
julia> times = Float64[
253+
times = Float64[
254254
3 3 5;
255255
8 4 8;
256256
7 2 10;
257257
5 1 7;
258258
2 5 6
259259
]
260260
261-
julia> result = makespan(JohnsonProblem(times), [1, 4, 5, 3, 2])
261+
result = makespan(JohnsonProblem(times), [1, 4, 5, 3, 2])
262262
```
263263
"""
264264
function makespan(problem::JohnsonProblem, permutation::Vector{Int})::Float64

src/knapsack.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ Solves the knapsack problem.
7171
7272
# Example
7373
```julia
74-
julia> values = [10, 20, 30, 40, 50];
75-
julia> weights = [1, 2, 3, 4, 5];
76-
julia> capacity = 10;
77-
julia> solve(KnapsackProblem(values, weights, capacity));
74+
values = [10, 20, 30, 40, 50];
75+
weights = [1, 2, 3, 4, 5];
76+
capacity = 10;
77+
solve(KnapsackProblem(values, weights, capacity));
7878
```
7979
"""
8080
function solve(problem::KnapsackProblem)::KnapsackResult

src/maximumflow.jl

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,25 @@ Solves a maximum flow problem given by an object of in type `MaximumFlowProblem`
7272
# Example
7373
7474
```julia
75-
julia> conns = [
76-
Connection(1, 2, 3),
77-
Connection(1, 3, 2),
78-
Connection(1, 4, 4),
79-
Connection(2, 5, 3),
80-
Connection(3, 5, 1),
81-
Connection(3, 6, 1),
82-
Connection(4, 6, 2),
83-
Connection(5, 7, 6),
84-
Connection(6, 7, 5),
85-
];
86-
julia> problem = MaximumFlowProblem(conns)
87-
julia> result = solve(problem);
88-
89-
julia> result.path
90-
9-element Vector{Connection}:
91-
Connection(1, 2, 3.0)
92-
Connection(1, 3, 2.0)
93-
Connection(1, 4, 2.0)
94-
Connection(2, 5, 3.0)
95-
Connection(3, 5, 1.0)
96-
Connection(3, 6, 1.0)
97-
Connection(4, 6, 2.0)
98-
Connection(5, 7, 4.0)
99-
Connection(6, 7, 3.0)
100-
101-
julia> result.flow
102-
7.0
75+
conns = [
76+
Connection(1, 2, 3),
77+
Connection(1, 3, 2),
78+
Connection(1, 4, 4),
79+
Connection(2, 5, 3),
80+
Connection(3, 5, 1),
81+
Connection(3, 6, 1),
82+
Connection(4, 6, 2),
83+
Connection(5, 7, 6),
84+
Connection(6, 7, 5),
85+
];
86+
87+
problem = MaximumFlowProblem(conns)
88+
89+
result = solve(problem);
90+
91+
println(result.path)
92+
93+
println(result.flow)
10394
```
10495
10596
!!! info "Determining start and finish nodes"

src/mst.jl

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -142,30 +142,20 @@ Obtains the minimum spanning tree.
142142
# Examples
143143
144144
```julia
145-
julia> conns = Connection[
146-
Connection(1, 2, 10),
147-
Connection(2, 3, 10),
148-
Connection(3, 4, 10),
149-
Connection(1, 4, 10)
150-
]
151-
152-
4-element Vector{Connection}:
153-
Connection(1, 2, 10)
154-
Connection(2, 3, 10)
155-
Connection(3, 4, 10)
156-
Connection(1, 4, 10)
157-
158-
julia> result = solve(MstProblem(conns))
159-
MstResult(Connection[Connection(3, 4, 10), Connection(1, 4, 10), Connection(2, 3, 10)], 30.0)
160-
161-
julia> result.distance
162-
30.0
163-
164-
julia> result.connections
165-
3-element Vector{Connection}:
166-
Connection(3, 4, 10)
167-
Connection(1, 4, 10)
168-
Connection(2, 3, 10)
145+
conns = Connection[
146+
Connection(1, 2, 10),
147+
Connection(2, 3, 10),
148+
Connection(3, 4, 10),
149+
Connection(1, 4, 10)
150+
]
151+
152+
result = solve(MstProblem(conns))
153+
154+
MstResult(Connection[Connection(3, 4, 10), Connection(1, 4, 10), Connection(2, 3, 10)], 30.0)
155+
156+
println(result.distance)
157+
158+
println(result.connections)
169159
```
170160
"""
171161
function solve(problem::MstProblem)::MstResult

src/pmedian.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,25 @@ The function calculates Euclidean distances between all possible rows of the mat
5858
# Example
5959
6060
```julia
61-
julia> data1 = rand(10, 2);
61+
data1 = rand(10, 2)
6262
63-
julia> data2 = rand(10, 2) .+ 50;
63+
data2 = rand(10, 2) .+ 50
6464
65-
julia> data3 = rand(10, 2) .+ 100;
65+
data3 = rand(10, 2) .+ 100
6666
67-
julia> data = vcat(data1, data2, data3);
67+
data = vcat(data1, data2, data3)
6868
69-
julia> result = pmedian(data, 3);
69+
result = pmedian(data, 3);
7070
71-
julia> result.centers
72-
3-element Vector{Int64}:
73-
1
74-
16
75-
21
71+
println(result.centers)
72+
# 3-element Vector{Int64}:
73+
# 1
74+
# 16
75+
# 21
7676
77-
julia> result.objective
78-
11.531012240599605
77+
println(result.objective)
78+
# 11.531012240599605
7979
```
80-
8180
"""
8281
function pmedian(data::Matrix, ncenters::Int)::PMedianResult
8382

src/shortestpath.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,28 @@ Solves a shortest path problem given by an object of in type `ShortestPathProble
6868
# Example
6969
7070
```julia
71-
julia> conns = [
72-
Connection(1, 2, 3),
73-
Connection(1, 3, 2),
74-
Connection(1, 4, 4),
75-
Connection(2, 5, 3),
76-
Connection(3, 5, 1),
77-
Connection(3, 6, 1),
78-
Connection(4, 6, 2),
79-
Connection(5, 7, 6),
80-
Connection(6, 7, 5),
81-
];
82-
83-
julia> solve(ShortestPathProblem(conns));
84-
85-
julia> result.path
86-
3-element Vector{Connection}:
87-
Connection(1, 3, 2)
88-
Connection(3, 6, 1)
89-
Connection(6, 7, 5)
90-
91-
julia> result.cost
92-
8.0
71+
conns = [
72+
Connection(1, 2, 3),
73+
Connection(1, 3, 2),
74+
Connection(1, 4, 4),
75+
Connection(2, 5, 3),
76+
Connection(3, 5, 1),
77+
Connection(3, 6, 1),
78+
Connection(4, 6, 2),
79+
Connection(5, 7, 6),
80+
Connection(6, 7, 5),
81+
]
82+
83+
result = solve(ShortestPathProblem(conns))
84+
85+
println(result.path)
86+
# 3-element Vector{Connection}:
87+
# Connection(1, 3, 2)
88+
# Connection(3, 6, 1)
89+
# Connection(6, 7, 5)
90+
91+
println(result.cost)
92+
# 8.0
9393
```
9494
9595
!!! info "Determining start and finish nodes"

0 commit comments

Comments
 (0)