I'm trying to impose a simple constrain that however doesn't work in any way I try. I'd to impose:
subject to myConstrain:
sum { a1 in A, a2 in A } myVar[a1,a2] = *<<<number of elements of the set A>>>*
How can I do that? Is there a function to use? I try in this way but it doesn't work.
subject to myConstrain:
sum { a1 in A, a2 in A } myVar[a1,a2] = sum {a in A} 1;
Thanks
You can write such constraint as follows:
subject to myConstrain:
sum{a1 in A, a2 in A} myVar[a1, a2] = card(A);
Related
How can I write an algorithm that determines if a function f from the finite set A to the finite set B is an onto function.
This is what I have so far:
A: array ( members of set A )
B: array ( members of set B )
Mapped: associative array of Boolean variables.
for each b in B:
Mapped[b] = false
for each a in A:
Mapped[f(a)] = true
Onto = true;
for each b in B:
Onto = Onto AND Mapped[b]
return Onto
Is this correct?
Yeah, that'll work. A potentially easier approach would be
for each a in A:
remove f(a) from B
return (is B empty?)
And then of course you should sort B first, so you can remove more quickly.
I have a function that needs to return the last property of an object that satisfies the condition:
types = {
a: 1,
b: 2,
c: 3
}
g = (s) -> v for k, v of types when k is s
console.log g 'b'
this code prints [ 2 ]
I expected just 2, and not an array. And indeed, this code does print what I expect:
console.log v for k, v of types when k is 'b'
What is wrong?
P.S. I know that instead of this function I can just access the object's property using [], but this is a contrived example.
If we rearrange the code then things should be clearer.
Your second piece of code:
console.log v for k, v of types when k is 'b'
is just another way of writing this:
for k, v of types when k is 'b'
console.log(v)
or even:
for k, v of types
if k is 'b'
console.log(v)
Since there is only one 'b' key, only one console.log call is made.
Your first piece of code:
g = (s) -> v for k, v of types when k is s
is the same as this:
g = (s) ->
a = (v for k, v of types when k is s)
a
The loop, v for k, v of types when k is s yields an array by definition so a will be an array (with only one element) and g will return an array.
console.log v for k, v of types when k is 'b' will call console.log(v) for every v when k satisfies the condition whereas your first code snipped will call console.log(g(b)). If there were two elements in types that satisfied the condition, the outputs would be:
[1, 2]
and
1
2
To make g output the first element that satisfies the condition, you could use return with early out or just take the first element of the results array.
g = (s) -> return v for k, v of types when k is s
Let be: L={an bm co | n < m < o, n natural}
Using Pumping Lemma I have choosen: z = uvwxy = an bn+1 cn+2
|uv|<=n and |v|>0
=> uv2wx2y
If vwx is of a's and / or b's it is okay and we would have more a's and/or b's than c's - but if vwx contains only c's it would be element of L.
As far as I understood all new words have to be not element of L to show that it is not a CFL. How would I do this?
If we have a mix of a's & b's use uv2wx2y
If we have a mix of b's & c's use uv0wx0y
Now all words that are created by pumping z are not element of L.
I have the following code that does not work:
fun { [h; d]
h(anotherFun; d)
}
h: hopen(`hparam)
d: (2013.06.01, 2013.06.02)
h,/:fun d
What do I need to do to call fun with h and each element of d? The diagnostic from q is cryptic to say the least.
it seems like h and elements of d are your two arguments so you can try
h fun/: d
Easiest would be create projection with constant argument and just call it for each argument from the list
fun[h;] each d
Is there an efficient way to get the n upper entries from a sorted Multiset (TreeMultiset)?
To specify what I mean I post my inefficient solution:
public SortedMultiset<DataTuple> headMultiset(int upperBound, BoundType boundType){
int i=0;
DataTuple act= this.coreData.firstEntry().getElement();
Iterator<DataTuple> itr = this.coreData.iterator();
while(i<=upperBound){
act = itr.next();
i+=this.coreData.count(act);
}
return headMultiset(act, boundType);
}
In this example DataSet can be seen as Object and this.coreData is the underling TreeMultiset.
I'm really new to that topic, so all kinds of comments would be appreciated.
I'm not 100% sure what result you're looking for. Let's take an example: say the multiset has contents [5 x a, 3 x b, 7 x c, 2 x d, 5 x e]. (As in Multiset.toString(), I am writing "count x object" to represent count occurrences of object.) If I understand the problem correctly, if n is 5, then the result you want is [5 x a], correct?
(It's also not clear whether you want the size of the result multiset to "round." For example: if n was 6 in the above multiset, would you want [5 x a, 1 x b], [5 x a], or [5 x a, 3 x b] ?)
For the moment, I'll assume that you want to round up, that is, you would expect [5 x a, 3 x b]. Then your answer isn't that far off, although I think it's subtly wrong as written. Here's how I would write it:
public <E> SortedMultiset<E> takeElements(SortedMultiset<E> multiset, int n) {
if (n == 0) { return ImmutableSortedMultiset.of(); }
Iterator<Multiset.Entry<E>> iterator = multiset.entrySet().iterator();
E cutoff = null;
for (int count = 0; count < n && iterator.hasNext(); ) {
Multiset.Entry<E> entry = iterator.next();
count += entry.getCount();
cutoff = entry.getElement();
}
if (count < n) { return multiset; }
// cutoff is not null, since the loop must iterate at least once
return multiset.headMultiset(cutoff, BoundType.CLOSED);
}
Actually the solution with the HashMap seems to have a acceptable performance. I built the hash map via:
public NavigableMap<Integer, E> BuildHashMap (SortedMultiset<E> multiset){
NavigableMap<Integer, E> ret = new TreeMap<Integer, E>();
int n = 0;
for (Entry<E> e : multiset.entrySet()) {
ret.put(n, e.getElement());
n += e.getCount();
}
return ret;
}
and access it with .floorEntry(n).getValue().
However elementSet().asList() is the function I'm actually looking for.