doubly array from a stream of integers - spring-boot

how to convert Flux<List> into Flux<int[][]>.
I have a Flux<List> -> {1,2,3,.....100} I want to group them by 30 numbers -> [[1,2,3,.....30], [31,32....60],[61.....100]]
I have tried the below approach but was not successful. elements are getting grouped in batches of 5 [ [1,2,3,4,5], [6,7,8,9,10],.....]
Flux<int[][]> groupedData = fluxData.map(x -> {
int outerArraySize = (int) Math.ceil(x.size() / 30) +1;
System.out.println(outerArraySize);
int[][] boxedData = new int[30][outerArraySize];
AtomicInteger innerArray = new AtomicInteger(0);
AtomicInteger outerArray = new AtomicInteger(0);
x.forEach(ids -> {
boxedData[innerArray.get()][outerArray.get()] = ids;
innerArray.getAndIncrement();
if (innerArray.get() == 30) {
innerArray.set(0);
outerArray.getAndIncrement();
}
});

Flux has a useful operator called 'buffer', we can use it to batch the List of Integers.
I have created my 'fluxData' like this, just so I can test my code:
List<Integer> list1 = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toList());
List<Integer> list2 = IntStream.rangeClosed(1, 40).boxed().collect(Collectors.toList());
List<Integer> list3 = IntStream.rangeClosed(1, 70).boxed().collect(Collectors.toList());
Flux<List<Integer>> fluxData = Flux.just(list1, list2, list3);
Now, we can do the following:
fluxData.map(integersList -> {
List<List<Integer>> batchesList = Flux.fromStream(integersList.stream())
.buffer(30) // This the magic.
.collectList()
.block();
// List<List<Integer>> --> int[][]
int[][] batchesArray = new int[batchesList.size()][];
for(int i = 0;i < batchesArray.length;i++){
batchesArray[i] = new int[batchesList.get(i).size()];
for (int j = 0; j < batchesArray[i].length; j++) {
batchesArray[i][j] = batchesList.get(i).get(j);
}
}
return batchesArray;
})
.subscribe(batchesArray -> System.out.println(Arrays.deepToString(batchesArray)));
Output:
[[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, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [91, 92, 93, 94, 95, 96, 97, 98, 99, 100]]
[[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]]
[[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, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70]]

Related

How CBOR Parse Task in Standard mode works?

I wonder how cborparse in mode="standard" works cause I can't make it work.
Resources
Chainlink CBOR Parse Task - docs
Chainlink CBOR Prase Task - implementation
Chainlink CBOR Parse Task - implementation tests
Online CBOR encoder
Online uft8 to bytes converter
Environment
Chainlink Node v1.7.0, and v1.8.1.
Case OK: cborparse in mode="diet" (default)
Test Case
Input
{"path":["recent","usd"],"url":"https://etherprice.com/api"}
Input CBOR encoded
A264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069
Webhook TOML spec
type = "webhook"
schemaVersion = 1
name = "Test decode CBOR diet"
observationSource = """
merge [type="merge" left=<{"input": "0x"}> right=<{"input": "0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"}>]
decode_cbor [type="cborparse" data="$(merge.input)"]
merge -> decode_cbor
"""
JSON job run
{
"__typename": "JobRun",
"id": "240",
"allErrors": [
],
"createdAt": "2022-10-14T12:06:56.646544Z",
"fatalErrors": [
],
"finishedAt": "2022-10-14T12:06:56.64718Z",
"job": {
"__typename": "Job",
"id": "225",
"name": "Test decode CBOR diet",
"observationSource": " merge [type="merge" left=<{"input": "0x"}> right=<{"input": "0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"}>] decode_cbor [type="cborparse" data="$(merge.input)"] "
},
"status": "COMPLETED",
"inputs": {
"decode_cbor": {
"path": [
"recent",
"usd"
],
"url": "https://etherprice.com/api"
},
"jobRun": {
"meta": null
},
"merge": {
"input": "0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"
}
},
"outputs": [
"{"path":["recent","usd"],"url":"https://etherprice.com/api"}"
],
"taskRuns": [
]
}
Case KO: cborparse in mode="standard"
Test Case
Input
{"path":["recent","usd"],"url":"https://etherprice.com/api"}
Input CBOR encoded
A264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069
Input CBOR encoded as array of bytes
[41, 32, 36, 34, 37, 30, 36, 31, 37, 34, 36, 38, 38, 32, 36, 36, 37, 32, 36, 35, 36, 33, 36, 35, 36, 45, 37, 34, 36, 33, 37, 35, 37, 33, 36, 34, 36, 33, 37, 35, 37, 32, 36, 43, 37, 38, 31, 41, 36, 38, 37, 34, 37, 34, 37, 30, 37, 33, 33, 41, 32, 46, 32, 46, 36, 35, 37, 34, 36, 38, 36, 35, 37, 32, 37, 30, 37, 32, 36, 39, 36, 33, 36, 35, 32, 45, 36, 33, 36, 46, 36, 44, 32, 46, 36, 31, 37, 30, 36, 39]
Below few TOML specs attempted but none of them work. They always fail with data: parameter is empty (common part of the JSON job run shared below):
{
"__typename": "JobRun",
"id": "246",
"allErrors": [
"data: parameter is empty"
],
"createdAt": "2022-10-14T12:31:42.234027Z",
"fatalErrors": [
"data: parameter is empty"
],
"finishedAt": "2022-10-14T12:31:42.236091Z",
"job": {
"__typename": "Job",
"id": "232",
"name": "Test decode CBOR standard",
"observationSource": " decode_cbor [type="cborparse" mode="standard" data=<it_does_not_matter>] "
},
"status": "ERRORED",
"inputs": {
"decode_cbor": {
},
"jobRun": {
"meta": null
}
},
"outputs": [
null
],
"taskRuns": [
{
"__typename": "TaskRun",
"id": "9c374ffb-cf80-41c9-a3b7-08391abd48fa",
"createdAt": "2022-10-14T12:31:42.234993Z",
"dotID": "decode_cbor",
"error": "data: parameter is empty",
"finishedAt": "2022-10-14T12:31:42.236021Z",
"output": "null",
"type": "cborparse"
}
]
}
Test 1: webhook TOML spec where data has an hexStr
data has is a hexStr
type = "webhook"
schemaVersion = 1
name = "Test decode CBOR diet 1"
observationSource = """
decode_cbor [type="cborparse" mode="standard" data="0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"]
"""
I've tried also multiple syntax combinations, adding and removing [ ], 0x, "". For instance:
type = "webhook"
schemaVersion = 1
name = "Test decode CBOR standard 3"
observationSource = """
decode_cbor [type="cborparse" mode="standard" data=<{["0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"]}>]
"""
Test 2: webhook TOML spec where data does not have hexStr
data is <{ ... }>. I've also tried this with scaped quotes (old syntax)
type = "webhook"
schemaVersion = 1
name = "Test decode CBOR standard 1"
observationSource = """
decode_cbor [type="cborparse" mode="standard" data=<{41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 34, 36, 33, 36, 31, 36, 43, 36, 43, 36, 36, 36, 39, 36, 45, 37, 30, 37, 35, 37, 34, 37, 33, 38, 32, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 39, 37, 32, 36, 35, 37, 31, 37, 35, 36, 35, 37, 33, 37, 34, 34, 39, 36, 34, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 37, 36, 32, 37, 39, 37, 34, 36, 35, 37, 33, 33, 33, 33, 32, 41, 34, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 37, 37, 32, 36, 35, 37, 33, 37, 35, 36, 43, 37, 34, 37, 33, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 37, 36, 32, 37, 39, 37, 34, 36, 35, 37, 33, 35, 42, 35, 44, 36, 36, 36, 35, 36, 45, 36, 33, 36, 46, 36, 34, 36, 35, 41, 32, 36, 36, 36, 44, 36, 35, 37, 34, 36, 38, 36, 46, 36, 34, 36, 36, 37, 30, 36, 31, 36, 33, 36, 42, 36, 35, 36, 34, 36, 34, 36, 34, 36, 31, 37, 34, 36, 31, 36, 41, 37, 32, 36, 35, 37, 33, 37, 35, 36, 43, 37, 34, 37, 33, 35, 32, 36, 31, 37, 37, 36, 41, 36, 33, 36, 46, 36, 44, 37, 30, 36, 46, 36, 45, 36, 35, 36, 45, 37, 34, 37, 33, 38, 35, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 36, 36, 37, 36, 31, 36, 44, 36, 35, 34, 39, 36, 34, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 35, 36, 39, 36, 45, 37, 34, 33, 33, 33, 32, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 39, 37, 33, 37, 34, 36, 31, 37, 32, 37, 34, 35, 34, 36, 39, 36, 44, 36, 35, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 35, 36, 39, 36, 45, 37, 34, 33, 34, 33, 30, 41, 34, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 45, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 34, 43, 36, 35, 36, 45, 36, 37, 37, 34, 36, 38, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 35, 37, 35, 36, 39, 36, 45, 37, 34, 33, 38, 36, 36, 36, 44, 36, 35, 37, 34, 36, 38, 36, 46, 36, 34, 36, 36, 36, 43, 36, 35, 36, 45, 36, 37, 37, 34, 36, 38, 36, 34, 36, 34, 36, 31, 37, 34, 36, 31, 36, 38, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 38, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 33, 37, 34, 37, 32, 36, 39, 36}>]
"""
data is <{[ ... ]}>. I've also tried this with scaped quotes (old syntax)
type = "webhook"
schemaVersion = 1
name = "Test decode CBOR standard 2"
observationSource = """
decode_cbor [type="cborparse" mode="standard" data=<{[41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 34, 36, 33, 36, 31, 36, 43, 36, 43, 36, 36, 36, 39, 36, 45, 37, 30, 37, 35, 37, 34, 37, 33, 38, 32, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 39, 37, 32, 36, 35, 37, 31, 37, 35, 36, 35, 37, 33, 37, 34, 34, 39, 36, 34, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 37, 36, 32, 37, 39, 37, 34, 36, 35, 37, 33, 33, 33, 33, 32, 41, 34, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 37, 37, 32, 36, 35, 37, 33, 37, 35, 36, 43, 37, 34, 37, 33, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 37, 36, 32, 37, 39, 37, 34, 36, 35, 37, 33, 35, 42, 35, 44, 36, 36, 36, 35, 36, 45, 36, 33, 36, 46, 36, 34, 36, 35, 41, 32, 36, 36, 36, 44, 36, 35, 37, 34, 36, 38, 36, 46, 36, 34, 36, 36, 37, 30, 36, 31, 36, 33, 36, 42, 36, 35, 36, 34, 36, 34, 36, 34, 36, 31, 37, 34, 36, 31, 36, 41, 37, 32, 36, 35, 37, 33, 37, 35, 36, 43, 37, 34, 37, 33, 35, 32, 36, 31, 37, 37, 36, 41, 36, 33, 36, 46, 36, 44, 37, 30, 36, 46, 36, 45, 36, 35, 36, 45, 37, 34, 37, 33, 38, 35, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 36, 36, 37, 36, 31, 36, 44, 36, 35, 34, 39, 36, 34, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 35, 36, 39, 36, 45, 37, 34, 33, 33, 33, 32, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 39, 37, 33, 37, 34, 36, 31, 37, 32, 37, 34, 35, 34, 36, 39, 36, 44, 36, 35, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 35, 36, 39, 36, 45, 37, 34, 33, 34, 33, 30, 41, 34, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 45, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 34, 43, 36, 35, 36, 45, 36, 37, 37, 34, 36, 38, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 35, 37, 35, 36, 39, 36, 45, 37, 34, 33, 38, 36, 36, 36, 44, 36, 35, 37, 34, 36, 38, 36, 46, 36, 34, 36, 36, 36, 43, 36, 35, 36, 45, 36, 37, 37, 34, 36, 38, 36, 34, 36, 34, 36, 31, 37, 34, 36, 31, 36, 38, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 41, 32, 36, 34, 36, 45, 36, 31, 36, 44, 36, 35, 36, 38, 36, 38, 36, 46, 36, 44, 36, 35, 35, 34, 36, 35, 36, 31, 36, 44, 36, 34, 37, 34, 37, 39, 37, 30, 36, 35, 36, 36, 37, 33, 37, 34, 37, 32, 36, 39, 36]}>]
""
Summary
Did I get wrong how this task is used and its purpose?
May be the standard mode just return straight data?, but why I'm always getting data parameters is empty?
One alternative for me to understand better what's going on would be implementing a unit test in task.cborparse_test.go, but may be someone has the answer!
Thanks!
When using mode=standard, can actually skip the character <{[.
So you can specify the value of data in the TOML as the hex string
decode_cbor [type="cborparse" mode="standard" data="0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"]
or
reference it from a variable like $(foo).
Give that a shot and let me know if you've had trouble.
Also, not sure you need the merge task :
merge [ type="merge" left=<{"input": "0x"}> right=<{"input": "0xA264706174688266726563656E74637573646375726C781A68747470733A2F2F657468657270726963652E636F6D2F617069"}>]
Hope this helps.

Print ASCII table without loop

I just had an interview and I have been asked a question: How would you print all the ASCII table characters without using a loop. The language doesn't matter.
The only method for doing so that comes to my mind is by using recursion instead of loops. An algorithm for doing this will be something like:
void printASCII(int i){
if(i == 128)
return;
print(i + " " + ((char)i) + "\n");
printASCII(i + 1);
}
You should call the previous function using:
printASCII(0);
This will print the complete ASCII table, where each line contains the index followed by a space and the actual ASCII character.
I don't think you can find any other way to do so, specially that it clearly says:
The language doesn't matter
This usually means that the question is about an algorithmic idea, rather than being specific for any language.
Two other approaches that weren't mentioned:
The obvious:
#include <stdio.h>
int main() {
printf("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c", 0, 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, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127);
}
The power of two tree (probably intended by the interviewer):
#include <stdio.h>
int c;
#define a128 a64; a64;
#define a64 a32; a32;
#define a32 a16; a16;
#define a16 a8; a8;
#define a8 a4; a4;
#define a4 a2; a2;
#define a2 a; a;
#define a printf("%c", c++);
int main() {
c = 0;
a128
}

Quicksort partitioning

I am sorry to be dumb, but I have been struggling with my own quicksort implementation for quiet a while. To be more specific, I can't get my partition procedure to work properly. Ridiculously enough, but I've also tried to almost directly copy an implementation from Sedjewick's book, with no success, however.
Here is my code:
void partition(int a[], int size)
{
int i, j = size - 1;
int t, pivot = a[j / 2];
i = 0;
for (;;)
{
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i >= j)
break;
t = a[i];
a[i++] = a[j];
a[j] = t;
}
}
Here is the example of input:
82, 65, 59, 10, 35, 51, 81, 47, 25, 64, 34, 38, 12, 38, 58, 74, 37, 42, 63, 18,
75, 67, 36, 77, 47, 48, 13, 91, 94, 52
The pivot here is 58, but I get wrong output:
52, 13, 48, 10, 35, 51, 47, 47, 25, 36, 34, 38, 12, 38, 18, 58, 37, 42, 63, 74,
75, 67, 64, 77, 81, 59, 65, 91, 94, 82
It looks almost correct, with a little exception of 37 and 42 going right after 58. I've tried a lot of variations of partitioning procedure, but they all get me similar results.
EDIT
My previous answer seemed to fix the issue but wasn't correct.
Your output is fine. Let me visually indicate the partition with vertical bars:
52, 13, 48, 10, 35, 51, 47, 47, 25, 36, 34, 38, 12, 38, 18, 58, 37, 42 || 63, 74, 75, 67, 64, 77, 81, 59, 65, 91, 94, 82
Everything to the left of the vertical bars is <= 58, and everything to the right is >= 58. This is what's expected from the partition step in a quicksort.
You do, however, need to decrement j in addition to incrementing i:
t = a[i];
a[i++] = a[j];
a[j--] = t; // added a decrement here
Other than that, the only thing you're missing is returning the partition index. Simply return the value of i at the end of the function and use that as the array boundary in your recursive step.
Replace a[j] = t; with a[j--] = t

Issue with random mutation hill climbing

Hi I'm trying to write some simple code to use random mutation hill climbing for the travelling salesman problem. I have created a Tour class as such:-
import java.util.ArrayList;
import java.util.Collections;
public class Tour
{
private ArrayList<Integer> tour;
// Specified tour
public Tour(ArrayList<Integer> tour) { this.tour = tour; }
// Random tour
public Tour(int size)
{
// Initalize tour with size
tour = new ArrayList<Integer>(size);
// Add integers up to size into ArrayList
for (int i=0; i<size; ++i) {tour.add(i);}
// Shuffle ArrayList
Collections.shuffle(tour);
}
ArrayList<Integer> getTour() {return tour;}
void printTour()
{
for (int i=0; i<tour.size(); ++i)
{
System.out.print(tour.get(i) + ", ");
}
}
// Get the distance between all tour stops using a set of distances
double getFitness (double[][] distances)
{
double s = 0;
for (int i=0; i<tour.size()-1; ++i)
{
int a = tour.get(i);
int b = tour.get(i+1);
s += distances[a][b];
}
int start_city = tour.get(0);
int end_city = tour.get(tour.size()-1);
s += distances[end_city][start_city];
return s;
}
// Makes a small change to the tour
void smallChange()
{
// Change random index values to swap
int indexfirst = CS2004.UI(0, tour.size()-1);
int indexsecond = CS2004.UI(0, tour.size()-1);
// Checks to make sure index values are not the same
while (indexsecond == indexfirst)
{
indexsecond = CS2004.UI(0, tour.size()-1);
}
// Store city value in temp variable
int indexTemp = tour.get(indexfirst);
// Swap values
tour.set(indexfirst, tour.get(indexsecond));
tour.set(indexsecond, indexTemp);
}
}
My RMHC method looks like this:-
public static Tour RMHC(double[][] distances, int iter)
{
Tour sol = new Tour(distances.length);
double oldFitness;
double fitness = 0;
Tour oldSol=null;
for (int i=0;i<iter;i++)
{
oldSol = null;
// Make old solution equal to solution before change
oldSol = new Tour(sol.getTour());
System.out.println(oldSol.getTour());
// Calculate old fitness for comparison
oldFitness = sol.getFitness(distances);
// Change solution slightly
sol.smallChange();
// Calculate new fitness
fitness = sol.getFitness(distances);
/* Compare new fitness to old fitness
* set solution back to old solution and fitness to old fitness if
* new solution is not better */
System.out.println(oldFitness + " " + fitness);
if (fitness > oldFitness) {System.out.println(oldSol.getTour()); System.out.println(sol.getTour()); sol = null; sol = new Tour(oldSol.getTour()); fitness = oldFitness;}
// Print iteration number and new fitness
System.out.println("Iteration " + (i+1) + ", fitness: " + sol.getFitness(distances));
}
return(sol);
}
The problem I'm having is that when I call my smallChange method in the RMHC it seems to change the Tour for both the old solution and the new solution. I ran this for a few iterations on a 48 size dataset and got the following output:-
[11, 6, 13, 37, 23, 45, 34, 25, 16, 39, 5, 35, 31, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 22, 44, 7, 24, 43, 14, 41, 1, 38, 40]
155843.9387676824 159088.1701641078
[31, 6, 13, 37, 23, 45, 34, 25, 16, 39, 5, 35, 11, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 22, 44, 7, 24, 43, 14, 41, 1, 38, 40]
[31, 6, 13, 37, 23, 45, 34, 25, 16, 39, 5, 35, 11, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 22, 44, 7, 24, 43, 14, 41, 1, 38, 40]
Iteration 1, fitness: 159088.1701641078
[31, 6, 13, 37, 23, 45, 34, 25, 16, 39, 5, 35, 11, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 22, 44, 7, 24, 43, 14, 41, 1, 38, 40]
159088.1701641078 144709.1336957683
Iteration 2, fitness: 144709.1336957683
[31, 6, 13, 37, 7, 45, 34, 25, 16, 39, 5, 35, 11, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 22, 44, 23, 24, 43, 14, 41, 1, 38, 40]
144709.1336957683 143387.5110957744
Iteration 3, fitness: 143387.5110957744
[31, 6, 13, 37, 7, 45, 22, 25, 16, 39, 5, 35, 11, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 34, 44, 23, 24, 43, 14, 41, 1, 38, 40]
143387.5110957744 143565.3842060348
[31, 6, 13, 37, 7, 45, 22, 25, 16, 39, 5, 35, 14, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 34, 44, 23, 24, 43, 11, 41, 1, 38, 40]
[31, 6, 13, 37, 7, 45, 22, 25, 16, 39, 5, 35, 14, 9, 27, 0, 10, 42, 30, 28, 4, 12, 33, 36, 2, 21, 17, 29, 18, 20, 32, 3, 15, 47, 26, 19, 46, 8, 34, 44, 23, 24, 43, 11, 41, 1, 38, 40]

Windows phone 7 hebrew support

I have just downloaded windows phone 7.1 sdk for windows phone 7.5.
I have two problems:
1. When i create a new project I doesn't see the option to choose windows phone 7 or 7.5
2. Windows phone 7.5 should be based on Silver light 4 but when I create label at my application and write hebrew words it's backwords.
Can I solve the problem at this version?
If you have installed the Mango tools, once you select to create a WP7 application you are prompted on a secondary screen to select whether to target 7.0 or 7.1.
Note that the version number of the SDK and tools is 7.1 but phones running Mango are marketed as version 7.5.
Note that WP7[.1|5] does not yet currently provide native support for RTL languages. You may find a workaround at http://www.danielmoth.com/Blog/RTL-Arabic-And-Hebrew-Support-For-Windows-Phone-7.aspx
For Windows Phone 7 i wrote a mapping function which maps the Arabic encoding[windows-1256] into default WP7 encoding.
public static string ConvertToArabic(string s) { short[] mapping = { 0, 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, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 8364, 1662, 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 1657, 8249, 338, 1670, 1688, 1672, 1711, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 1705, 8482, 1681, 8250, 339, 8204, 8205, 1722, 160, 1548, 162, 163, 164, 165, 166, 167, 168, 169, 1726, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 1563, 187, 188, 189, 190, 1567, 1729, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579, 1580, 1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1589, 1590, 215, 1591, 1592, 1593, 1594, 1600, 1601, 1602, 1603, 224, 1604, 226, 1605, 1606, 1607, 1608, 231, 232, 233, 234, 235, 1609, 1610, 238, 239, 1611, 1612, 1613, 1614, 244, 1615, 1616, 247, 1617, 249, 1618, 251, 252, 8206, 8207, 1746 };
string str = string.Empty;
for (int ix = 0; ix < s.Length; ++ix)
{
str = str + (char)mapping[s[ix]];
}
return str;
}
This worked for me well.

Resources