## Dynamic Programming – Select objects to maximize the value under multiple conditions

I think it's best to first explain the problem and then explain what I think:

I have a record similar to the following:

``````Fruits, calories, costs
Apple, 100,1
Apple, 200, 1.5
Apple, 150.2
Pear, 300.3
Pear, 100, 0.5
Pear, 250.2
orange, 100.1
Orange, 120.1
Orange, 400.2
``````

And I try to maximize my calories while keeping my costs in a specific area. At this point, it's just a backpack problem, but what if I have to have 1 apple, 1 pear, and 2 oranges or some other arbitrary set of fruit numbers?

I can not really care how this works, given the additional limitations. I've been thinking about putting my costs and calories together in 1 metric, but I'm pretty sure it will not work as it loses.

My last thought may be to log the number of fruits in a list. When the right amount of fruit is reached, the next fruit will continue. I think about it, similar to a backpack with 3 limitations, weight, value and size, but the size is essentially just the number of fruits.

If I hope someone is here, can that tell me if I am heading in the right direction or if there is an algorithm that does this that I can investigate.

Many Thanks!

## Dynamic Programming – Why is my iterative solution slower than a recursive solution?

I'm trying to find a solution to this question: https://www.spoj.com/problems/COINS/.

But strangely, my iterative solution:

``````#include
using namespace std;

int main () {
int n;
while (cin >> n) {
long long int dp[n+2];
dp[0]= 0;
for (long long int i = 1; i <= n; i ++)
dp[i]= max (dp[i/2]+ dp[i/3]+ dp[i/4], me);
cout << dp[n] << endl;
}
return 0;
}
``````

gets a TLE, while the recursive solution for it (not mine) is accepted in no time:

``````#include
#include
#include

using namespace std;

map dp;

long long f (int n) {
if (n == 0) returns 0;

if (dp[n]! = 0) return dp[n];

long aux = f (n / 2) + f (n / 3) + f (n / 4);

if (aux> n) dp[n]= aux;
otherwise dp[n]= n;

return dp[n];
}

int main () {
int n;

while (scanf ("% d", & n) == 1) printf ("% lld  n", f (n));

return 0;
}
``````

Should not it be the opposite? I am really confused.
Please tell me how to improve the first code.

## Merging two ordered sequences in linear programming

Given two sequences of integers $$a_1, dots, a_t$$ and $$b_1, dots, b_m$$ everyone $$> 0$$ from where $$a_i and $$b_i Is there a linear program to merge into an ordered sequence without integer variables?

Related: Sort as a linear program

## Programming Challenge – Project Euler, Issue # 9, Pythagorean Triplet

A Pythagorean triplet is a set of three natural numbers a <b <c, for which a ** 2 + b ** 2 = c ** 2

For example 3 ** 2 + 4 ** 2 = 9 + 16 = 25 = 52.

There is exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.

Here is my implementation in Python waiting for your feedback …

``````def get_triplet ():
for c in the range (2, 1000):
for b in the range (2, c):
a = 1000 - c - b
if a ** 2 + b ** 2 == c ** 2:
return # n1 =% s  n2 =
% s  n n3 =% s  n product =% s & # 39;% (a, b, c, a * b * c)

if __name__ == & # 39; __ main __ & # 39 ;:
print (get_triplet ())
``````

## Genetic Programming – Tournament Selection and Elite

I write a thesis about automatic GUI tests. To find the best strategy, I use GP. I use tournament selection to select next-generation chromosomes and elitism to ensure that the strongest elites move into the next round.

Basically, I have 2 questions:

a) Many papers use 7 as tournament size, as this is the default setting in one of Koza's papers. As far as I know, we do not have the point of Koza. I think 7 could have been a good fit for his research, which can be based on his population. Should we not use the probability and adjust the tournament size accordingly? As you may have noticed, I assume. Are there any papers that I may have missed and are writing about choosing the right tournament size? If not, should experiments be conducted to find the right tournament size for a particular research setup?

For example, Following results in a 7% chance that a chromosome is part of the tournament.

Population: 100
Elites: 5
Tournament selection: 7

An increase in tournament size (selection pressure) will increase the likelihood that a chromosome will be selected for the tournament.

b) If I have a population of 100, a tournament size of 10 and 5 elites, if I understand it correctly, 6 chromosomes make it to the next generation, right?

## linear programming – Why does not my code properly fill in the extended matrix?

I try to enter data from a training file. The first row is completely skipped.

``````                double ** augmentMatrix (double ** matrix, int dim) {
double ** identityMatrix = CreateIdentityMatrix (dim, dim); // Creates an identity matrix

double ** augmentedMatrix = (double **) malloc (sizeof (double *) * dim);
for (int row = 0; row <dim; row ++) {
advanced matrix[row] = (double *) malloc (sizeof (double) * dim * 2);
} // attributes spaces

for (int row = 0; row <dim; row ++) {
for (int col = 0; col <dim; col ++) {
advanced matrix[row][col]    = Matrix[row][col]; // Sets the first part of the extended matrix to the points of the matrix parameter
}
for (int col = dim; col <2 * dim; col ++) {
advanced matrix[row][col]    = identity matrix[row][col - dim]; // separates the identity matrix
}
}
printMatrix (augmentedMatrix, dim, dim * 2);
return augmentedMatrix;
}
``````

The following is displayed:

``````                0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
26.2, 84.8, 61.9, 58900.0, 51789.8, 0.0, 1.0, 0.0, 0.0, 0.0,
6493.8, 19487.8, 12220.3, 9864262.5, 12861241.2, 0.0, 0.0, 1.0, 0.0, 0.0,
21384.0, 70762.0, 55533.2, 57206075.0, 42298973.0, 0.0, 0.0, 0.0, 1.0, 0.0,
19713.0, 61172.0, 41552.0, 39621035.0, 38865313.0, 0.0, 0.0, 0.0, 0.0, 1.0,
``````

Here is the training file:

``````                18.0, 55.0, 37.0, 33025.0, 35598.0,
26.2, 84.8, 61.9, 58900.0, 51789.8,
6493.8, 19487.8, 12220.3, 9864262.5, 12861241.2,
21384.0, 70762.0, 55533.2, 57206075.0, 42298973.0,
19713.0, 61172.0, 41552.0, 39621035.0, 38865313.0,
``````

## Design – Are there limits to programming languages ​​that others call?

This mechanism is called a foreign function call. Virtually all major programming languages ​​can do this, although the usability of a foreign function call is different for each pair of language implementations.

Python itself has, for example `ctypes` in the standard library and a foreign library `CFFI` This allows the invocation of C code and other languages ​​that have a C-like foreign function interface (for which there are quite a few). Java has JNI (Java Native Interface).

.NET CLR and Java differ a bit because they provide an interface that allows all languages ​​running in the same VM to be called to a different language in the VM. Therefore, invoking foreign functions from the same runtime is generally quite simple. This is possible because these languages ​​already have to be mapped into the construct of the VM anyway.

## design – How can some programming languages ​​call others?

This mechanism is called a foreign function call. Virtually all major programming languages ​​can do this, although the usability of a foreign function call is different for each pair of language implementations.

Python itself has, for example `ctypes` in the standard library and a foreign library `CFFI` This allows the invocation of C code and other languages ​​that have a C-like foreign function interface (for which there are quite a few). Java has JNI (Java Native Interface).

.NET CLR and Java differ a bit because they provide an interface that allows all languages ​​running in the same VM to be called to a different language in the VM. Therefore, invoking foreign functions from the same runtime is generally quite simple. This is possible because these languages ​​already have to be mapped into the construct of the VM anyway.

## Functional Programming – Refactoring an if statement within a higher-order function in Swift

With Swift, I sort some objects by the first date order and then by the title, if the date is the same, as shown in the following excerpt:

``````.sorted {
if \$ 0.date! = \$ 1.date {
return \$ 0.date <\$ 1.date // date object
} else {
return \$ 0.title <\$ 1.title // String
}
}
``````

This short if / else is used several times. So I want to extract that functionality and make it a reusable component. Any ideas or suggestions on how to do this, assuming the return value could be a date or a string?

## Computability – Questions about Seth Lloyd's Programming the Universe?

I am now interested in the cosmological model of Seth Lloyd (which states that the universe is a computer: https://en.wikipedia.org/wiki/Programming_the_Universe, https://arxiv.org/abs/quant-ph/ 0501135) a long time ago. While researching on this topic, I found an interesting article written by Hungarian author Zoltán Galántai (https://arxiv.org/pdf/1501.01754.pdf)

I was wondering if you would answer a few questions about the article. It would be very helpful if one of you could find some time to answer them.

1. In Zoltán's article he compiled a table listing the possible types of calculations. In type "C3" he says:
"Computing based on different constants – in some cases it is impossible to interpret Lloyd's equations for them"
What does he mean exactly? Would Lloyd's cosmological model be compatible with that? Would his model be applicable to a universe with this kind of calculation?

2. He also created a table that shows the possible types of computational universe based on the speed of light. In type 3 (U3) he says that calculation is impossible because the speed of light would be zero. However, if this universe had a structure that resembled a wormhole or some other type of instant communication (perhaps a kind of teleportation system or another) a kind of quantum entanglement that would be superluminal) could light not transmit information without having to move at all? Could this not allow a calculation? Could Lloyd's model be applied to a static (c = 0) universe? I mean, would this model be able to "produce" this kind of universe?