Display all possible solutions in ojAlgo - constraint-programming

Question: Is it possible to adjust the above to produce all possible results instead of an optimized one.
Details: Given a data set of armor, I want to produce a set of combinations where my constraints are fulfilled.
Variable helm1 = model.addVariable("Helm 1").binary();
Variable helm2 = model.addVariable("Helm 2").binary();
Variable helm3 = model.addVariable("Helm 3").binary();
Variable arm1 = model.addVariable("Arm 1").binary();
Variable arm2 = model.addVariable("Arm 2").binary();
Variable arm3 = model.addVariable("Arm 3").binary();
Expression statA = model.addExpression().lower(0).weight(1);
Expression statB = model.addExpression().lower(0).weight(1);
Expression statC = model.addExpression().lower(0).weight(1);
//Lower Limit set for desired stat
Expression statD = model.addExpression().lower(2).weight(1);
// Limit number of helms you can equip
model.addExpression().upper(1).set(helm1,1).set(helm2,1).set(helm3,1);
model.addExpression().upper(1).set(arm1,1).set(arm2,1).set(arm3,1);
statA.set(arm1, 1);
statB.set(helm2, 1);
statB.set(helm3, 1);
statB.set(arm2, 1);
statC.set(helm1, 1);
statC.set(arm2, 1);
statC.set(arm3, 1);
statD.set(helm3, 3);
statD.set(arm1, 1);
Optimisation.Result result = model.maximise();
BasicLogger.debug(result);
Note: Before recommending libraries, please not that the library must be compatible with Android.

Answer: No - ojAlgo will output 1 (the optimal if it can find it) solution.

Related

Pyomo Constraint Block in Abstract Model

For a better structure of my Constraints, I want to summarize multiple constraints into a block, so that a don't have to scroll through a long list of separate functions representing my constraints.
My problem is that I'm using an Abstract model and don't know how to define that Block for a set that has not been initialized yet
M.s = pe.Set(dimen=1)
M.chp_minPower = pe.Param(within=pe.Reals,mutable=True)
M.chp_maxPower = pe.Param(within=pe.Reals,mutable=True)
M.chp_posGrad = pe.Param(within=pe.Reals,mutable=True)
M.chp_negGrad = pe.Param(within=pe.Reals,mutable=True)
M.chp_k = pe.Param(within=pe.Reals,mutable=True)
M.chp_c = pe.Param(within=pe.Reals,mutable=True)
M.chp_f1 = pe.Param(within=pe.Reals,mutable=True)
M.chp_f2 = pe.Param(within=pe.Reals,mutable=True)
M.gasCost = pe.Param(within=pe.Reals,mutable=True)
M.chpOn = pe.Var(M.s, within=pe.Binary)
M.chpSwitchON = pe.Var(M.s,within=pe.Binary)
M.chpPel = pe.Var(M.s,within=pe.NonNegativeReals)
M.chpPth = pe.Var(M.s, within=pe.NonNegativeReals)
M.chpQGas = pe.Var(M.s, within=pe.NonNegativeReals)
def chp_block_rule1(nb,i):
#Constraints
nb.chpPelMax = pe.Constraint(expr=M.chpPel[i] <= M.chp_maxPower * M.chpOn[i])
nb.chpPelMin = pe.Constraint(expr=M.chpPel[i] >= M.chp_minPower * M.chpOn[i])
#b.sellBin = pe.Constraint(expr=b.sell[i]/M.maxSell <= M.sellBin[i]
nb.chpCogen = pe.Constraint(expr=M.chpPth[i] == M.chp_f1 * M.chpPel[i] + M.chp_f2 * M.chpOn[i])
nb.chpConsumption = pe.Constraint(expr=M.chpQGas[i] == M.chp_c * M.chpOn[i] + M.chp_k + M.chpPel[i])
M.chp_block = pe.Block(M.s, rule=chp_block_rule1)
ValueError: Error retrieving component chpPel[1]: The component has
not been constructed.
Does anybody know how to work with blocks in Abstract models?
I'm not 100% sure but I guess expr tries to actually evaluate the expression, and because chpPel is a variable (thus has no value) it breaks.
In order to delay the evaluation of the expression (i.e. pass the expression into the solver as symbolic), you can use rule instead of expr.
As you probably know rule takes a function. If the expression is short enough, you can use a lambda function.
nb.chpPelMax = pe.Constraint(rule=lambda M: M.chpPel[i] <= M.chp_maxPower * M.chpOn[i])
Side note: you can just use list(range(...)) instead of the list comprehension.

How can I 0-pad a number by a variable amount when formatting with std::fmt?

I'm looking to 0-pad a string before I display it to the user, e.g.
let x = 1234;
println!("{:06}", x); // "001234"
However, I'd like the length of the output string to be variable, e.g. it could be set by a command line parameter from the user:
let x = 1234;
let width = 6;
println!("{:0*}", width, x); //fails to compile
// error: invalid format string: expected `'}'`, found `'*'`
Unlike precision, it doesn't seem that 0-padding supports the * for specifying a width.
I'm not looking for solutions that involve manually padding, because it seems awkward to re-implement part of std::fmt.
You can use the :0_ format specifier with a variable:
println!("{:0width$}", x, width = width); // prints 001234
Here it is running in the playground
Likewise, if width <= 4, it just prints 1234. If width = 60, it prints:
000000000000000000000000000000000000000000000000000000001234
The format arguments also support ordinals, thus this also works:
println!("{:01$}", x, width);
The documentation for std::fmt has a rundown of the various parameters and modifiers the print_! macros support.

NDepend rule to warn if objects of a given type are compared using ==

as the title says: I need a NDepend rule (CQLinq) for C#/.net code, that fires whenever instances of a given type are compared using == (reference comparison). In other words, I want to force the programmer to use .Equals.
Note that the type in question has no overloaded equality operator.
Is this possible? If so, how? :)
Thanks, cheers,
Tim
With the following code with see that for value type, == translate to the IL instruction: ceq. This kind of usage cannot be detected with NDepend.
int i = 2;
int j = 3;
Debug.Assert(i == j);
var s1 = "2";
var s2 = "3";
Debug.Assert(s1 == s2);
However for reference types we can see that a operator method named op_Equality is called.
L_001d: call bool [mscorlib]System.String::op_Equality(string, string)
Hence we just need a CQLinq query that first match all method named op_Equality, and then list all callers of these methods. This can look like:
let equalityOps = Methods.WithSimpleName("op_Equality")
from m in Application.Methods.UsingAny(equalityOps)
select new { m,
typesWhereEqualityOpCalled = m.MethodsCalled.Intersect(equalityOps).Select(m1 => m1.ParentType) }
This seems to work pretty well :)

ReSharper Search and Replace with Pattern

I would like to re-factor various blocks of code throughout a project using ReSharper 7.1 Search / Replace with pattern.
The code blocks are similar to the following simplified example:
someControl.StatusProgressBar.IsIndeterminate = false;
someControl.StatusProgressBar.Visibility = Visibility.Visible;
someControl.StatusProgressBar.Minimum = 0;
someControl.StatusProgressBar.Maximum = 100;
someControl.StatusProgressBar.Value = percentage;
And I would like to change them to:
someControl.StatusProgressBar.Use(p =>
{
p.IsIndeterminate = false;
p.Visibility = Visibility.Visible;
p.Minimum = 0;
p.Maximum = 100;
p.Value = percentage;
});
'Use' is an extension method
This is easy enough if all the blocks of code are setting the same number of properties. The following search and replace patterns will do the job:
SEARCH
$someControl$.$SomeProperty$.$SubProperty1$ = $val1$;
$someControl$.$SomeProperty$.$SubProperty2$ = $val2$;
$someControl$.$SomeProperty$.$SubProperty3$ = $val3$;
$someControl$.$SomeProperty$.$SubProperty4$ = $val4$;
$someControl$.$SomeProperty$.$SubProperty5$ = $val5$;
REPLACE
$someControl$.$SomeProperty$.Use(p=>
{
p.$SubProperty1$ = $val1$;
p.$SubProperty2$ = $val2$;
p.$SubProperty3$ = $val3$;
p.$SubProperty4$ = $val4$;
p.$SubProperty5$ = $val5$;
});
However, if I also have a code block such as:
someControl.StatusProgressBar.IsIndeterminate = false;
someControl.StatusProgressBar.Visibility = Visibility.Visible;
someControl.StatusProgressBar.Minimum = 0;
someControl.StatusProgressBar.Maximum = 100;
someControl.StatusProgressBar.Value = percentage;
someControl.StatusProgressBar.Orientation = Vertical;
Is it possible, with ReSharper, to capture and replace both code blocks with one pattern? The later having one extra property setting but could easily be more than one extra or less.
I am thinking this is not possible. It would require the ability to create some kind of variable pattern and I can't see a way to do that, be it with regular expressions or otherwise.
Any ideas?

A query about lexical scoping

I try to understand lexical-scoping. In lexical-scoping, I have this code, C like syntax:
main{
f1(){
int y = 8;
}
int y = 1; //*
f1();
}
After the execution of f1() line, will the value of y variable in main (I put * next of it) remain 1 or change to 8?
It will remain 1. You have two completely distinct variables. Changes to one do not affect the other.

Resources