Linear Programming Improve Overlap - linear-programming

Is there a way to introduce an overlap objective to this linear programming problem?
Simplified Example:
In my problem, each variable represents a "task" that needs to be completed a certain number of times. When we need to do a "task" it works better if we can have multiple people do that "task" at the same time.
Person 1 Constraint 1: a1 + b1 >= 5
Person 1 Constraint 2: c1 + d1 >= 2
...
Person 2 Constraint 1: a2 + e2 >= 2
Person 2 Constraint 2: f2 + g2 >= 1
...
Person N
Objective 1: Minimize Overall Cost
Objective 2: Maximize Overlap
So in the above example choosing a1 instead of b1 would be ideal since it would overlap with a2).
I'm currently using Google OR Tools which only supports 1 objective (as far as I can tell), so I'm hoping I can somehow translate the 2nd objective into a sub-objective of #1.
i.e (Minimize Cost Equation * 100000000) + (-1 * Maximize Overlap Equation)

Related

LP / MILP If-Then Statements

I have the following statements for a MILP:
Variables: c (can be 1 or 0), αj (real numbers with 0 <= αj <= 1)
I have a linear inequality system for aj:
∑vj * αj = 0 (with vj = constants)
∑αj = c
and I have the following logic:
If there exists a solution for c = 1, the formulation should be infeasible
If there exists the only one solution c = 0 (each αj must be 0) the formulation should be feasible
I need some more equations or changes so that the logic above holds.
First idea:
When I use an additional constraint c = 1 the MILP finds a solution for c = 1 und no solution for c = 0. This helps to identify if c can be 1 but this flips the feasible solution space since the solver breaks when c = 0 which should be the feasible one. Adding the constraint c = 0 will not help, since it is not enough that c = 0 is one potential solution, it must be the only one valid solution.
Second idea:
When I use the objective function max(c) i can conclude that
IF max(c) = 1 THEN not feasible (or IF max(c) = 0 THEN feasible)
However I don't want to use c in the objective function.
Is there any other possibility to change the formulation so that the logic above holds?

Removing Cyclotomic Factors from a Polynomial - Pari

I want to take some polynomial f and remove all of it's cyclotomic factors, and then look at the resulting polynomial (say g). I'm aware of polcyclofactors and the current code I have tried is:
c(f)=polcyclofactors(f)
p(f)=prod(i=1,#c(f),c(f)[i])
g(f)=f/p(f)
The issue I have is that polcyclofactors doesn't take into account multiplicity of the cyclotomic factors. For example:
f=3*x^4 + 8*x^3 + 6*x^2 - 1
g(f)
= 3*x^3 + 5*x^2 + x - 1
But
factor(f)
=
[ x + 1 3]
[3*x - 1 1]
Is there any way to be able to nicely include multiple cyclotomic factors of f to divide by? Or will I have to look at factorising f and try and removing cyclotomic factors that way?
The following two suggestions are based on repeating the divisions until no more can be done (they are both very similar).
Suggestion 1:
r(f)={my(c); while(c=polcyclofactors(f); #c, f=f/vecprod(c)); f}
Suggestion 2:
r(f)={my(g=vecprod(polcyclofactors(f))); while(poldegree(g), f=f/g; g=gcd(f,g)); f}
Another suggestion without a loop:
r(f)={my(g=vecprod(polcyclofactors(f))); numerator(f/g^(poldegree(f)))}
Now a version that is probably superior: For each factor valuation can be used to get the required power.
r(f)={f/vecprod([t^valuation(f,t) | t<-polcyclofactors(f)])}

Not understanding mathematical solution to implementation task

The problem sounds like that:
Kevin has N friends and one building. He wants to organize a
party in that building and he invites exacly 1 friend / day. Kevin
unfortunately has grumpy neighbors which aren't too happy with the
noise that Kevin's party does. For that, Kevin wants to minimize the
noise level of his party. (the noise level is equal to the number of
friends in the building) In order to do that, after some day he can
clear the building by asking his friends to leave (he can do that K times, for K different days). (ex: he clears the
building after day 2 so at day 3 when he invites another friend, for
day 3 the noise level will be 1 because the past day he cleared the
building; if he didn't clear the building at day 2, the noise level
for day 3 will have been 3 (1 from day 2, 1 from day 1 and the new invited friend) and the total noise level for day 1, 2 and 3 would have been 1+2+3=6).
For better understanding the task:
Input:
5 2 (N, K)
Output:
7
Explanation:
In the input example, N friends will be invited at the party, in the following order:
1 (day 1) 1 (day 1)
1 (day 2) so in the building for each day will be present 2 (day 2)
1 (day 3) -------------------------------------------------> 3 (day 3)
1 (day 4) (without clearing the building) 4 (day 4)
1 (day 5) 5 (day 5)
-----(+)
15
So, clearing the building 0 times (K=0), the noise level will be 15.
Clearing the building 1 time (K=1), the sum will be:
(0 times)
1 1
__ 2 __ clearing the building after day 2 2
3 -----------------------------------> 1
4 2
5 3
----(+)
9
At this case (K=1), another solution could be to clear after day 3, same sum.
Clearing 2 times (K=2):
(0 times)
1 1
__ 2 __ clearing the building after day 2 & 3 2
__ 3 __ --------------------------------------> 1
4 1
5 2
----(+)
7
I have the solution but I don't understand it!
I tried, took some other cases but still nothing, maybe you guys can explain me why the solution is the way it is.
This is how sum(N, K) is calculated:
sum(N, K) = minimum noise level clearing the building K times (K>=0)
C++ code:
int sum(int n)
{
return n * (n + 1) / 2;
}
int solve(int n, int k)
{
int p = k + 1;
int mp = n/p;
int bp = (n+p-1)/p;
int nmaj = n%p;
int nmic = p-nmaj;
return nmic * sum(mp) + nmaj*sum(bp);
}
What is the purpose of each variable?
What does nmic * sum(mp) and nmaj*sum(bp) computes?
!!!!
For the example above (at the beginning - N=5, K=2), I debugged the code and wrote for N=5, K=0..2 the value for each variable, hoping to get the idea but no succes. I am going to post them for you, maybe you'll get the idea and then explain to me (I am a novice in algorithmic problems).
Here is it:
variable values for each case
I repeat, I tried for some hours to understand but no succes. And it's not a homework. Thank you!
The code goes like this:
int p = k+1;
p is the number of "parts" in which you divide the days (Ex: Day1 + Day2 = part1, Day 3 + Day4 = part2, Day 5 = part3)
int mp = n/p;
mp is the amount of days the minor part has, in the example is 1 (Day5). It's calculated as the floor of the number of days over the number of parts.
int bp = (n+p-1)/p;
bp is the amount of days in the bigger part, in the example is 2 (Day1 + Day2 or Day3 + Day4). I don't really get the exact math behind but that's what it calculates
int nmaj = n%p;
int nmic = p-nmaj;
nmaj is the number of major parts, in the example is 2 (Day1 + Day2 and Day3 + Day4), the nmic is the number of minor parts (1 for Day5).
nmic * sum(mp) + nmaj*sum(bp);
This just returns the number of minor parts * the accumulated value in minor parts + number of major parts * the accumulated value in major parts
The sum function is just the formula for arithmetic series (Or summation of arithmetic progressions) for the special case of initial element = 1

Adding constraints for a known solution causes out of bounds exception

I have a linear optimization goal to Maximize EE+FF, where EE and FF each consist of some C and D.
With code I've written, I can get solver to find:
EE_quantity: 0, FF_quantity: 7
...but I know there to be another solution:
EE_quantity: 1, FF_quantity: 6
In order to validate user input for other valid solutions, I added a constraint for both EE and FF. So I added the EE_quantity == 0, FF_quantity == 7 in the code below, which is a runnable example:
SolverContext c2 = SolverContext.GetContext();
Model m2 = c2.CreateModel();
p.elements = elements_multilevel_productmix();
Decision C_quantity = new Decision(Domain.IntegerNonnegative, "C_quantity");
Decision D_quantity = new Decision(Domain.IntegerNonnegative, "D_quantity");
Decision EE_quantity = new Decision(Domain.IntegerNonnegative, "EE_quantity");
Decision FF_quantity = new Decision(Domain.IntegerNonnegative, "FF_quantity");
m2.AddDecisions(C_quantity, D_quantity, EE_quantity, FF_quantity);
m2.AddConstraints("production",
6 * C_quantity + 4 * D_quantity <= 100,
1 * C_quantity + 2 * D_quantity <= 200,
2 * EE_quantity + 1 * FF_quantity <= C_quantity,
1 * EE_quantity + 2 * FF_quantity <= D_quantity,
EE_quantity == 0,
FF_quantity == 7
);
m2.AddGoal("fixed_EE_FF", GoalKind.Maximize, "EE_quantity + FF_quantity");
Solution sol = c2.Solve(new SimplexDirective());
foreach (var item in sol.Decisions)
{
System.Diagnostics.Debug.WriteLine(
item.Name + ": " + item.GetDouble().ToString()
);
}
It seems that Solver Foundation really doesn't like this specific combination. Using EE_quantity == 1, FF_quantity == 6 is fine, as is using just EE_quantity == 0 or FF_quantity == 7. But using both, AND having one of them being zero, throws an exception:
Index was outside the bounds of the array.
What is going on under the hood, here? And how do I specify that I want to find "all" solutions for a specific problem?
(Note: no new releases of Solver Foundation are forthcoming - it's essentially been dropped by Microsoft.)
The stack trace indicates that this is a bug in the simplex solver's presolve routine. Unfortunately the SimplexDirective does not have a way to disable presolve (unlike InteriorPointDirective). Therefore the way to get around this problem is to specify the fixed variables differently.
Remove the last two constraints that set EE_quantity and FF_quantity, and instead set both the upper and lower bounds to be 0 and 7 respectively when you create the Decision objects. This is equivalent to what you wanted to express, but appears to avoid the MSF bug:
Decision EE_quantity = new Decision(Domain.IntegerRange(0, 0), "EE_quantity");
Decision FF_quantity = new Decision(Domain.IntegerRange(7, 7), "FF_quantity");
The MSF simplex solver, like many mixed integer solvers, only returns the optimal solution. If you want MSF to return all solutions, change to the constraint programming solver (ConstraintProgrammingDirective). If you review the documentation for Solution.GetNext() you should figure out how to do this.
Of course the CP solver is not guaranteed to produce the globally optimal solution immediately. But if you iterate through solutions long enough, you'll get there.

Solve an equation containing three unknown prime numbers [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I was recently asked a question in an interview & have been unable to crack it, after my own efforts have failed & Google not showing any results , I am posting it here so that anyone else may also try their hand on it .
Given the equation:
a (a + b) = c - 120
where a,b & c are unequal prime numbers, find a, b & c.
I know I must use some property of the prime numbers to reduce the problem to a simpler one, but I can't think of one. Any suggestions/solutions will be appreciated.
The best I could come up with is that :
There may be multiple solutions to it. My first approach was a brute force search for 3 prime numbers that solved this equations. (I know , totally useless)
The second approach was a refinement of the first, to modify the equation to a (a + b) - 120 = c. So now we reduce our brute force variables to just a & b & check if the LHS is prime no for the selected a & b. (If c were to be large, finding out whether the LHS is a prime would take away the advantage gained by reducing the variables from 3 to 2.)
So you see, I am not really going anywhere.
all primes are odd, except 2 - (1)
all primes are positive - (2)
odd - even = odd (3)
(1), (2) => c > 120 and c is odd - (4)
odd * odd = odd - (5)
(3), (4), (5) => c-120 is odd => a(a+b) is odd - (6)
even + odd = odd - (7)
(6) => a is odd, a+b is odd (8)
(7), (8) => b is even => b = 2
So, we have a^2 + 2a = c-120
I couldn't go any further
Let's stipulate that c > 120. That implies c != 2. So the RHS is odd.
Therefore the LHS has to be odd, so a (a + b) has to be odd. So a is odd, and a+b is odd. This only works out if b is even, and b is prime, so b = 2.
So we have a(a+2) = c - 120.
So a^2 + 2a + (120-c) = 0
Using the quadratic formula, solving for a, we get
[-2 +- sqrt(2^2 - 4 * 1 * (120 - c))] / 2
= -1 +- sqrt(1 - (120-c))
= -1 + sqrt(c - 119)
So we need a prime number c, so that c - 119 is a perfect square.
This is a quick calculation with a table of primes.
The smallest one I can find is c = 263, so a = 11, b = 2
It looks like c=443, a=17, b=2 also works.
There don't appear to be any other c values below 1000.
Possibly there are many, many others.