Skip to content

Commit de9add4

Browse files
committed
Precedence and BinPacking revisited
1 parent 55883de commit de9add4

File tree

7 files changed

+91
-10
lines changed

7 files changed

+91
-10
lines changed

src/main/java/org/xcsp/common/Types.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public static enum TypeChild {
205205
machines,
206206
conditions,
207207
sizes,
208+
capacities,
208209
weights,
209210
profits,
210211
balance,

src/main/java/org/xcsp/parser/XParser.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.xcsp.common.Types.TypeChild.FINAL;
3737
import static org.xcsp.common.Types.TypeChild.arcs;
3838
import static org.xcsp.common.Types.TypeChild.balance;
39+
import static org.xcsp.common.Types.TypeChild.capacities;
3940
import static org.xcsp.common.Types.TypeChild.coeffs;
4041
import static org.xcsp.common.Types.TypeChild.colOccurs;
4142
import static org.xcsp.common.Types.TypeChild.condition;
@@ -1097,10 +1098,15 @@ private void parsePermutation(Element elt, Element[] sons, int lastSon) {
10971098
}
10981099

10991100
private void parsePrecedence(Element elt, Element[] sons, int lastSon) {
1100-
addLeaf(list, parseSequence(sons[0]));
1101-
addLeaf(values, parseSequence(sons[1]));
1102-
if (lastSon == 2)
1103-
addLeaf(operator, TypeOperator.valOf(sons[lastSon].getTextContent()));
1101+
if (sons.length == 0)
1102+
addLeaf(list, parseSequence(elt));
1103+
else {
1104+
addLeaf(list, parseSequence(sons[0]));
1105+
if (lastSon == 1)
1106+
addLeaf(values, parseSequence(sons[1]));
1107+
// if (lastSon == 2)
1108+
// addLeaf(operator, TypeOperator.valOf(sons[lastSon].getTextContent()));
1109+
}
11041110
}
11051111

11061112
/**********************************************************************************************
@@ -1138,7 +1144,9 @@ private void parseCumulative(Element elt, Element[] sons) {
11381144
private void parseBinPacking(Element elt, Element[] sons) {
11391145
addLeaf(list, parseSequence(sons[0]));
11401146
addLeaf(sizes, parseSequence(sons[1]));
1141-
if (isTag(sons[2], condition))
1147+
if (isTag(sons[2], capacities))
1148+
addLeaf(capacities, parseSequence(sons[2]));
1149+
else if (isTag(sons[2], condition))
11421150
addLeaf(condition, parseCondition(sons[2]));
11431151
else
11441152
addLeaf(conditions, parseConditions(sons[2]));

src/main/java/org/xcsp/parser/callbacks/CompetitionValidator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ public void buildCtrLexMatrix(String id, XVarInteger[][] matrix, TypeOperatorRel
407407
unimplementedCaseIf(currTestIsMiniTrack, id);
408408
}
409409

410+
@Override
411+
public void buildCtrPrecedence(String id, XVarInteger[] list) {
412+
unimplementedCase(id); // should we allow it in the future competition (2023)?
413+
}
414+
410415
@Override
411416
public void buildCtrPrecedence(String id, XVarInteger[] list, int[] values, boolean covered) {
412417
unimplementedCase(id); // should we allow it in the future competition (2023)?
@@ -717,6 +722,16 @@ public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condi
717722
unimplementedCase(id); // for the moment, not accepted for the competition
718723
}
719724

725+
@Override
726+
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, int[] limits) {
727+
unimplementedCase(id); // for the moment, not accepted for the competition
728+
}
729+
730+
@Override
731+
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, XVarInteger[] loads) {
732+
unimplementedCase(id); // for the moment, not accepted for the competition
733+
}
734+
720735
@Override
721736
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condition[] conditions, int startIndex) {
722737
unimplementedCase(id); // for the moment, not accepted for the competition

src/main/java/org/xcsp/parser/callbacks/SolutionChecker.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
import org.xcsp.common.Condition.ConditionVar;
5252
import org.xcsp.common.Constants;
5353
import org.xcsp.common.IVar.Var;
54+
import org.xcsp.common.Range;
5455
import org.xcsp.common.Types.TypeAtt;
5556
import org.xcsp.common.Types.TypeChild;
57+
import org.xcsp.common.Types.TypeConditionOperator;
5658
import org.xcsp.common.Types.TypeFlag;
5759
import org.xcsp.common.Types.TypeObjective;
5860
import org.xcsp.common.Types.TypeOperatorRel;
@@ -603,6 +605,19 @@ public void buildCtrLexMatrix(String id, XVarInteger[][] matrix, TypeOperatorRel
603605
i -> IntStream.range(i + 1, transposedTuples.length).allMatch(j -> orderedVectors(transposedTuples[i], transposedTuples[j], operator))));
604606
}
605607

608+
@Override
609+
public void buildCtrPrecedence(String id, XVarInteger[] list) {
610+
Set<Integer> allValues = new HashSet<>();
611+
for (XVarInteger x : list) {
612+
Object d = ((Dom) x.dom).allValues();
613+
if (d instanceof Range)
614+
d = ((Range) d).toArray();
615+
for (int v : (int[]) d)
616+
allValues.add(v);
617+
}
618+
buildCtrPrecedence(id, list, allValues.stream().mapToInt(v -> v).sorted().toArray(), false);
619+
}
620+
606621
@Override
607622
public void buildCtrPrecedence(String id, XVarInteger[] list, int[] values, boolean covered) {
608623
int[] tuple = solution.intValuesOf(list);
@@ -990,6 +1005,18 @@ public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condi
9901005
checkCondition(w, condition);
9911006
}
9921007

1008+
@Override
1009+
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, int[] limits) {
1010+
Condition[] conditions = IntStream.of(limits).mapToObj(v -> Condition.buildFrom(TypeConditionOperator.LE, v)).toArray(Condition[]::new);
1011+
buildCtrBinPacking(id, list, sizes, conditions, 0);
1012+
}
1013+
1014+
@Override
1015+
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, XVarInteger[] loads) {
1016+
Condition[] conditions = Stream.of(loads).map(v -> Condition.buildFrom(TypeConditionOperator.EQ, v)).toArray(Condition[]::new);
1017+
buildCtrBinPacking(id, list, sizes, conditions, 0);
1018+
}
1019+
9931020
@Override
9941021
public void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condition[] conditions, int startIndex) {
9951022
Map<Integer, Integer> map = filling(solution.intValuesOf(list), sizes);

src/main/java/org/xcsp/parser/callbacks/XCallbacks.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,8 @@ default void buildCtrFalse(String id, XVar[] list) {
13611361
*/
13621362
void buildCtrLexMatrix(String id, XVarInteger[][] matrix, TypeOperatorRel operator);
13631363

1364+
void buildCtrPrecedence(String id, XVarInteger[] list);
1365+
13641366
void buildCtrPrecedence(String id, XVarInteger[] list, int[] values, boolean covered);
13651367

13661368
/**
@@ -1815,6 +1817,11 @@ void buildCtrElement(String id, XVarInteger[][] matrix, int startRowIndex, XVarI
18151817

18161818
void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condition condition);
18171819

1820+
void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, int[] limits); // capacities given as limits
1821+
1822+
void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, XVarInteger[] loads); // capacities given as
1823+
// loads
1824+
18181825
void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condition[] conditions, int startIndex);
18191826

18201827
void buildCtrKnapsack(String id, XVarInteger[] list, int[] weights, Condition wcondition, int[] profits, Condition pcondition);

src/main/java/org/xcsp/parser/callbacks/XCallbacks2.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ default void buildCtrLexMatrix(String id, XVarInteger[][] matrix, TypeOperatorRe
341341
unimplementedCase(id);
342342
}
343343

344+
@Override
345+
default void buildCtrPrecedence(String id, XVarInteger[] list) {
346+
unimplementedCase(id);
347+
}
348+
344349
@Override
345350
default void buildCtrPrecedence(String id, XVarInteger[] list, int[] values, boolean covered) {
346351
unimplementedCase(id);
@@ -643,6 +648,16 @@ default void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Cond
643648
unimplementedCase(id);
644649
}
645650

651+
@Override
652+
default void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, int[] limits) {
653+
unimplementedCase(id);
654+
}
655+
656+
@Override
657+
default void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, XVarInteger[] loads) {
658+
unimplementedCase(id);
659+
}
660+
646661
@Override
647662
default void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Condition[] conditions, int startIndex) {
648663
unimplementedCase(id);
@@ -651,7 +666,6 @@ default void buildCtrBinPacking(String id, XVarInteger[] list, int[] sizes, Cond
651666
@Override
652667
default void buildCtrKnapsack(String id, XVarInteger[] list, int[] weights, Condition wcondition, int[] profits, Condition pcondition) {
653668
unimplementedCase(id);
654-
655669
}
656670

657671
@Override

src/main/java/org/xcsp/parser/loaders/CtrLoaderInteger.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,13 @@ private void lex(XCtr c) {
431431

432432
private void precedence(XCtr c) {
433433
if (c.childs[0].type == TypeChild.list) {
434-
Utilities.control(c.childs.length == 2, "bad form");
435-
boolean covered = c.childs[1].getAttributeValue(TypeAtt.covered, false);
436-
xc.buildCtrPrecedence(c.id, (XVarInteger[]) c.childs[0].value, trIntegers(c.childs[1].value), covered);
434+
if (c.childs.length == 1)
435+
xc.buildCtrPrecedence(c.id, (XVarInteger[]) c.childs[0].value);
436+
else {
437+
Utilities.control(c.childs.length == 2, "bad form");
438+
boolean covered = c.childs[1].getAttributeValue(TypeAtt.covered, false);
439+
xc.buildCtrPrecedence(c.id, (XVarInteger[]) c.childs[0].value, trIntegers(c.childs[1].value), covered);
440+
}
437441
} else
438442
xc.unimplementedCase(c);
439443
}
@@ -744,7 +748,12 @@ private void binPacking(XCtr c) {
744748
CChild[] childs = c.childs;
745749
XVarInteger[] list = (XVarInteger[]) childs[0].value;
746750
int[] sizes = trIntegers(c.childs[1].value);
747-
if (childs[2].type == TypeChild.condition)
751+
if (childs[2].type == TypeChild.capacities) {
752+
if (childs[2].value instanceof XVarInteger[])
753+
xc.buildCtrBinPacking(c.id, list, sizes, (XVarInteger[]) childs[2].value);
754+
else
755+
xc.buildCtrBinPacking(c.id, list, sizes, trIntegers(childs[2].value));
756+
} else if (childs[2].type == TypeChild.condition)
748757
xc.buildCtrBinPacking(c.id, list, sizes, (Condition) childs[2].value);
749758
else
750759
xc.buildCtrBinPacking(c.id, list, sizes, (Condition[]) childs[2].value, childs[2].getAttributeValue(TypeAtt.startIndex, 0));

0 commit comments

Comments
 (0)