I am working on chicken swarm optimization when the algorithm return the best solution the broker seem to have a different ordering
best solution
Cloudlet Vm
-------- ---------
99 1
52 2
62 7
63 13
64 3
68 14
71 6
94 0
97 8
13 9
53 10
91 11
16 12
1 4
61 5
15 15
33 16
34 17
35 18
43 19
44 1
45 2
46 7
47 13
49 3
42 14
48 6
50 0
51 8
37 9
40 10
36 11
39 12
41 4
38 5
6 15
31 16
19 17
65 18
88 19
89 1
66 2
9 7
92 13
93 3
18 14
98 6
14 0
30 8
12 9
2 10
96 11
11 12
29 4
77 5
87 15
70 16
76 17
78 18
81 19
82 1
69 2
56 7
57 13
32 3
85 14
75 6
80 0
83 8
60 9
86 10
79 11
74 12
84 4
67 5
28 15
90 16
27 17
21 18
72 19
95 1
73 2
26 7
23 13
25 3
24 14
3 6
20 0
59 8
58 9
0 10
55 11
54 12
4 4
22 5
17 15
8 16
10 17
7 18
5 19
printing the result after receiving from broker
Cloudlet ID STATUS Data center ID VM ID
99 SUCCESS 2 1
52 SUCCESS 2 2
62 SUCCESS 2 7
64 SUCCESS 2 3
71 SUCCESS 2 6
97 SUCCESS 2 8
63 SUCCESS 2 13
68 SUCCESS 2 14
94 SUCCESS 2 0
53 SUCCESS 2 10
13 SUCCESS 2 9
91 SUCCESS 2 11
16 SUCCESS 2 12
1 SUCCESS 2 4
61 SUCCESS 2 5
15 SUCCESS 2 15
35 SUCCESS 2 18
44 SUCCESS 2 1
33 SUCCESS 2 16
34 SUCCESS 2 17
45 SUCCESS 2 2
46 SUCCESS 2 7
49 SUCCESS 2 3
48 SUCCESS 2 6
51 SUCCESS 2 8
47 SUCCESS 2 13
42 SUCCESS 2 14
50 SUCCESS 2 0
43 SUCCESS 2 19
40 SUCCESS 2 10
37 SUCCESS 2 9
36 SUCCESS 2 11
39 SUCCESS 2 12
41 SUCCESS 2 4
38 SUCCESS 2 5
6 SUCCESS 2 15
31 SUCCESS 2 16
19 SUCCESS 2 17
65 SUCCESS 2 18
89 SUCCESS 2 1
88 SUCCESS 2 19
66 SUCCESS 2 2
9 SUCCESS 2 7
93 SUCCESS 2 3
92 SUCCESS 2 13
18 SUCCESS 2 14
98 SUCCESS 2 6
14 SUCCESS 2 0
30 SUCCESS 2 8
2 SUCCESS 2 10
12 SUCCESS 2 9
96 SUCCESS 2 11
any ideas why this this happened ?
(Edited 2021-09-06 13:30 CEST after I found out that the problem is caused at a different place of my code already)
I am working on some code (not my own) where they use a custom type for dynamic integer arrays. One instance of this type is passed via a common block between routines. It looks like this is not working properly or as expected, since the arrays take different values after being passed to a new routine. A minimal example looks like this:
implicit none
type intdynarr2
sequence
integer, pointer::i(:,:)
end type intdynarr2
type(intdynarr2):: my_array
integer j,k
common/carrays/my_array
allocate(my_array%i(1:10,1:10))
do j=1,10
do k=1,10
my_array%i(j,k)=j*k
enddo
enddo
write(*,*) my_array%i
call printarr
end
subroutine printarr
implicit none
type intdynarr2
sequence
integer, pointer::i(:,:)
end type intdynarr2
type(intdynarr2):: my_array
common/carrays/my_array
write(*,*) my_array%i
end subroutine
The output of this after compiling with gfortran in version 8.2.0 looks like this:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
while I expected it to be:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Can anyone tell me what is going wrong here and how to fix this?
So I wrote a function in C which gets input until EOF
while (scanf(" %c", &input) != EOF) {
// do stuff
}
I tried to use the following test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
10 20 30 40 50 _66_ 15 66bb
As an input to the program. It actually runs the program but at the end it finishes with the following error: Illegal instruction (core dumped).
Other tests work fine, and after a quick check I found out that the problem begins after the last 50 (if I use only the numbers until 50 - it works fine). How to solve this problem?
This question already has answers here:
C-equivalent of the 'setw' function
(4 answers)
Closed 6 years ago.
With
fprintf(f,"%d %d %d\n",m,l,k);
I got
8 0 98
8 1 98
8 2 98
9 0 98
9 1 98
9 2 98
10 0 98
10 1 98
10 2 98
11 0 98
11 1 98
11 2 98
12 0 98
12 1 98
12 2 98
13 0 98
13 1 98
What I really want is that each line has same format,like this
9 2 98
10 0 98
This code
fprintf(f,"%2d %d %2d\n",m,l,k);
yields:
9 2 98
10 0 98
I have this vector:
a = [ 7 8 9 7 8 9];
and I would like to obtain the following vector:
b= [ 7 8 9 7 8 9;
17 18 19 17 18 19;
27 28 29 27 28 29;
37 38 39 37 38 39 ...]
I am replicating the vector and then summing 10 for each line (for n lines). I would like to do this without using loop iterations. How can I do it?
Thank you so much.
My first bsxfun solution.
a = [ 7 8 9 7 8 9];
b = 0:10:(10+10*n);
c = bsxfun(#plus,a,b.');
c =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
47 48 49 47 48 49
57 58 59 57 58 59
67 68 69 67 68 69
77 78 79 77 78 79
87 88 89 87 88 89
97 98 99 97 98 99
107 108 109 107 108 109
Bit of explanation, though a full and complete introduction to bsxfun can be found in this answer by Divakar.
What happens is that your row array a gets piece-wise added to the column vector b. Thus the first element of a, being 7 in this case, gets added to the column vector b=[10;20;30;...] and becomes the first column of your output matrix c. The second entry of a is summed with the same column vector b and becomes the second column of c. This gets repeated to fill the entire matrix c to a size of numel(b) x numel(a).
This is becoming quite the coding fest. I ran some bench test, running a loop with n=1000 a hundred times and averaged the results. Windows 7, MATLAB R2012a, i5-750 CPU. Actually, the for loop is not even the worst in terms of timing:
bsxfun: 0.00003556 s.
repmat: 0.00048514 s.
cumsum: 0.00015726 s.
for : 0.00033096 s.
Timing revisited on the same system, but with MATLAB R2015a. for is now the slowest, with the others edging towards one another, but bsxfun prevails!
bsxfun: 0.00002030 s.
repmat: 0.00005213 s.
cumsum: 0.00002180 s.
for : 0.00019560 s.
Where I used this for loop implementation:
base = [7 8 9 7 8 9];
A = zeros(1e3,length(base));
for n = 1:1e3;
A(n,:) = base+10*n;
end
For completeness, you could also obtain the desired result through the use of the repmat command:
n = 3;
a = [7 8 9 7 8 9];
addingMatrix = repmat([0:10:10*n+1]',1,size(a,2));
b = addingMatrix + repmat(a,n+1,1);
Result:
b =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
Since repmat and bsxfun are already taken, another approach is to create a temporary array where [7 8 9 7 8 9] is the first row, followed by a matrix completely full of 10s where the number of rows is the desired number you want and the number of columns is the total number of elements in the base vector ([7 8 9 7 8 9]). You'd then apply a cumulative sum via cumsum along the rows:
n = 11; %// Number of rows - including the first row of [7 8 9 7 8 9]
base = [7 8 9 7 8 9];
A = [base; 10*ones(n-1, numel(base))];
B = cumsum(A, 1);
We get:
>> B
B =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
47 48 49 47 48 49
57 58 59 57 58 59
67 68 69 67 68 69
77 78 79 77 78 79
87 88 89 87 88 89
97 98 99 97 98 99
107 108 109 107 108 109