Linear Optimization: Set of inequalitiy constraints where only one has to be True - optimization

I am currently implementing a Optimizationproblem, where I have a set of inequalitiy constraints. My Problem is that these constraints are connected with an AND if I use fmincon. So the Problem is solved if all constrained are satisfied. For my problem it is enough of only one of these constraints are satisfied. But all constraints can be satisfied. So instead of a And my constraints should be connected with OR. Do you have any idea how to do that?
As I have to have a linear Problem, I can't use non-linear constraints.
I also could formulate my constraints so that they are all True when the constraints are not satisfied. But I don't know how to implement that the constraints aren't allowed to turn true.

Related

OptaPlanner - ConstraintStreams - Disabling constraints dynamically

I am using Java ConstraintStreams for constraint implementation and need to enable/disable the constraints dynamically.
Following the suggested approach in this answer Optaplanner : Add / remove constraints dynamically using zero constraint weight seems to work as expected (the score is different compared to when the constraint is enabled) but when I put a log in the constraint implementation the log was printed which suggests that the constraint is actually being evaluated even if the score is set to zero.
private Constraint minimizeCost(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(CloudProcess.class)
.filter(process -> {
System.out.println("Minimize computer cost");
return process.getComputer() != null;
})
.penalizeConfigurable("computerCost",
node -> (Integer) process.getComputer.getCost());
}
For Disabling the constraint I am using
new CloudBalance().setConstraintConfiguration(new CloudBalanceConstraintConfiguration()
.computerCost(HardMediumSoftScore.ZERO))
I have modified the CloudBalancing example to make it similar to what I am trying to implement.
So is there something that I am missing in terms of understanding/implementation ?
Will the disabled constraint still execute regular stream operations like filter and skip OptaPlanner specific operations like ifExists, joins etc.?
Is there any way to prevent this behavior ? (currently I am working with version 8.16.0.Final)
You are correct that part of the constraint may still be executed. But constraint weight of zero makes sure that no constraint will ever match - they may have a cost in terms of performance, but they will not affect the score nor justifications.
There is nothing else you could do, other than generating a different ConstraintProvider every time. In the future, we may improve constraint disabling so that the constraints are disabled entirely.

Get number of attached constraints on a variable in MiniZinc

I have two sets of variables in my Minizinc program. Each variable from the first set necessarily has several constraints placed on it, but the variables in the second set are only implicitly constrained via their interactions with variables in the first set. This means that each of the variables in the second set may have anywhere from 0 to ~8 constraints placed on it, depending on the values taken by the variables in the first set.
I see that there is a way to reference the number of constraints placed on a variable at search time via the dom_w_deg search annotation, but I was wondering if there was anyway to access this information at runtime? I want to do this because I would like to specify additional constraints related to the number of constraints already placed on the variables.
I realize this is a weird question, and I may be approaching this whole thing the wrong way, but I've been banging my head against this problem for a while now, so figured I'd ask.
As a general rule, I think that you are approaching your problem erroneously. There are several mis-conceptions in the approach that I can identify leading to this:
Different solver back-ends might do very different things with the model and how it is solved
"A constraint" is not a meaningful concept for the solver. A single constraint might be multiple propagators in the back-end solver, a single propagator, or even just part of a propagator covering several constraints (assuming that it is a propagator based back-end).
Constraint models have monotonic behavior, so you can not in a well-defined and meaningful way change the model based on the number of constraints connected to a variable.
Given that a constraint maps to a single propagator, it may still have very different propagation strength, meaning that it might be done early or very late in the solving process.
Without knowing what you are actually trying to achieve, as a general technique you might be interested in using reification, where the truth of a constraint is reflected onto a binary Boolean variable. In general, it is good practice to have as little reification as possible, since it does not propagate much, but sometimes it is needed.
As a very simple example of using reification, this is a (probably not very good) model that tries to maximize the number of constraints satisfied.
set of int: Domain = 1..10;
var Domain: x;
var Domain: y;
var Domain: z;
array[1..3] of var bool: holds;
constraint holds[1] <-> x < y;
constraint holds[2] <-> y < z;
constraint holds[3] <-> z < x;
var int: goal;
constraint goal = sum(holds);
solve maximize goal;

Hive foreign keys?

I am new to Hive. I have tried searching various websites but none were able to give me a clear picture of the following:
A> Foreign keys: The general Hive concept never mentions anything about foreign keys. Then, how do we enforce referential constraints? (I am aware of JOIN ON syntax, so does that mean the two tables have a primary key:foreign key relationship?) Is there a higher purpose for not supporting foreign keys?
B> Float equality comparison: There seems to be a problem with this. For instance, to check if A=3.5 => "A>3.49 and A<3.51". Is this the right way?
Are there any references/materials out there which could help in HQL implementation?
Appreciate any help,
Thanks
-Shiree
Hive is implemented as Schema-on-Read, so there is no inherent referential integrity performed by Hive on datasets. Instead, integrity needs to be performed by the source system, and more importantly, by any queries that are executed in Hive.
Primary/foreign keys constraint support is available in Hive 2.1.0.
See 2.1.0 release notes.
Hive does not currently support FK/PK constraints.
But it may be the case in the future. It gives Hive CBO more information to make better cardinality estimates, better query rewrites:
https://issues.apache.org/jira/browse/HIVE-13019
https://issues.apache.org/jira/browse/HIVE-6905
In response to Mo K's answer, constraints not necessarily mean overhead. Oracle for example has "RELY NOVALIDATE" constraints - so CBO (or Hive CBO in this case) relies on that constraint for its query optimizations, but does not have to actually check if that constraint is true.
Edit 02/18/2016: I've created https://issues.apache.org/jira/browse/HIVE-13076 please vote up if you're interested in that feature.
Edit 07/25/2016: https://issues.apache.org/jira/browse/HIVE-13076 is resolved as of 06/2016, should be landing in Hive 2.1. I don't see yet updates in official documentation.
Generally a best practice in Data Warehousing, is to avoid enforcing referential integrity, to avoid the overhead. Therefore if the need arises, you can enforce it explicitly in your queries.

Getting ORA-00905 error when trying to add a foreign key

I'm trying to perform the following statement into oracle:
alter table COMENTARIO
add constraint FK_COMENTARIO_DI foreign key (DI_ID)
references DATO_DE_INTERES (DI_ID) ON UPDATE CASCADE ON DELETE SET NULL;
However, I get ORA-00905 missing keyword.
When I remove the ON UPDATE statement though, the command works without any problem. Why is this? Is there any options in case I can't use ON UPDATE? Thank you beforehand!
There is no "ON UPDATE" clause for cascading constraints. I don't know of any alternatives aside from some sort of application enforcement or triggers.
There is no such option as ON UPDATE CASCADE in Oracle. Maybe instead of looking for a way to implement this (I think it is possible with ON UPDATE trigger) you could tell why do you need this.
I mean - why would you want to update the primary key of DATO_DE_INTERES?
In Oracle, there is no ON UPDATE clause in a constraint definition. In the vast majority of cases, you wouldn't want to implement this "just in case" because primary keys should be immutable. If your primary keys are not immutable, that would generally be indicative of a data model problem that should be addressed rather than coded around.
That said, if you really want to implement something like that, Tom Kyte does have an update cascade package. But you're far better off designing the system to avoid the problem in the first place rather than designing in this level of complexity.

XOR Constraint in Mysql

I want to implement a kind of xor constraint on foreign keys in mysql 5.1
There is this table, let's say Entity which can refer to two different kinds of valuesets represented by Tables ValsA and ValsB. Now I would like to implement a constraint wich makes sure that exactly one of those two is mapped, and the other one isn't.
In Oracle you could use something like
CHECK (NVL2(FK_A,1,0)+NVL2(FK_B,1,0)=1));
but as far as I understand it MySQL does not really support CHECK Constraints (yet).
Any ideas?
Correct. MySQL does not support check contraints. The CHECK clause is parsed but ignored by all storage engines.
You'd have to enforce the XOR condition on the client side.