Skip to content

Commit

Permalink
Remove slack
Browse files Browse the repository at this point in the history
  • Loading branch information
apete committed Aug 26, 2023
1 parent 3ec278e commit 9fa88be
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 2,505 deletions.
2,474 changes: 1 addition & 2,473 deletions jdeps.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static interface EntityMap extends ProblemStructure {
int countModelVariables();

/**
* The number of slack variables - relates to {@link #getSlack(int)}
* The number of slack variables
*/
int countSlackVariables();

Expand All @@ -207,7 +207,8 @@ public static interface EntityMap extends ProblemStructure {
* Returns which model entity, and constraint type, that corresponds to the slack variable at the
* supplied index.
*
* @param idx Index of solver slack variable
* @param idx Index of solver slack variable (If there are 3 slack variables this input argument
* should be in the range [0.2].)
*/
EntryPair<ModelEntity<?>, ConstraintType> getSlack(int idx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ final class LinearStructure implements ExpressionsBasedModel.EntityMap {
final int nbVars;
final int[] negativePartVariables;
final int[] positivePartVariables;
final EntryPair<ModelEntity<?>, ConstraintType>[] slack;

LinearStructure(final boolean inclMap, final int constrIn, final int constrEq, final int varsPos, final int varsNeg, final int varsSlk, final int varsEye) {

positivePartVariables = new int[varsPos];
negativePartVariables = new int[varsNeg];
slack = (EntryPair<ModelEntity<?>, ConstraintType>[]) new EntryPair<?, ?>[varsSlk + varsEye];
constraints = ConstraintsMap.newInstance(constrIn + constrEq, inclMap);

nbInes = constrIn;
Expand Down Expand Up @@ -128,8 +126,13 @@ public EntryPair<ModelEntity<?>, ConstraintType> getConstraintMap(final int i) {
}

@Override
public EntryPair<ModelEntity<?>, ConstraintType> getSlack(final int js) {
return slack[js];
public EntryPair<ModelEntity<?>, ConstraintType> getSlack(final int idx) {

if (idx < nbSlck) {
return this.getConstraintMap(nbIdty + idx);
} else {
return this.getConstraintMap(idx - nbSlck);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.ojalgo.structure.Structure1D.IntIndex;
import org.ojalgo.type.EnumPartition;
import org.ojalgo.type.context.NumberContext;
import org.ojalgo.type.keyvalue.EntryPair;

abstract class SimplexStore {

Expand Down Expand Up @@ -143,7 +142,6 @@ static <S extends SimplexStore> S build(final ExpressionsBasedModel model, final
mtrxB.set(i, expression.getUpperLimit(true, POSITIVE_INFINITY));
lowerBounds[nbProbVars + i] = ZERO;
upperBounds[nbProbVars + i] = POSITIVE_INFINITY;
structure.slack[i] = EntryPair.of(expression, ConstraintType.UPPER);
structure.setConstraintMap(i, expression, ConstraintType.UPPER, false);
}

Expand All @@ -158,7 +156,6 @@ static <S extends SimplexStore> S build(final ExpressionsBasedModel model, final
mtrxB.set(nbUpConstr + i, expression.getLowerLimit(true, NEGATIVE_INFINITY));
lowerBounds[nbProbVars + nbUpConstr + i] = NEGATIVE_INFINITY;
upperBounds[nbProbVars + nbUpConstr + i] = ZERO;
structure.slack[nbUpConstr + i] = EntryPair.of(expression, ConstraintType.LOWER);
structure.setConstraintMap(nbUpConstr + i, expression, ConstraintType.LOWER, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ final Collection<Equation> generateCutCandidates(final boolean[] integer, final
solRHS[j] = constraintsRHS.doubleValue(i);
}
}

if (ProblemStructure.DEBUG) {
BasicLogger.debug("RHS: {}", Arrays.toString(solRHS));
BasicLogger.debug("Bas: {}", Arrays.toString(included));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.ojalgo.structure.ElementView1D;
import org.ojalgo.structure.Structure1D.IntIndex;
import org.ojalgo.type.context.NumberContext;
import org.ojalgo.type.keyvalue.EntryPair;

/**
* Classic simplex solver:
Expand Down Expand Up @@ -375,7 +374,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = expression.getAdjustedUpperLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(expression, ConstraintType.UPPER);
structure.setConstraintMap(indCnstr, expression, ConstraintType.UPPER, false);
indCnstr++;
indSlack++;
Expand All @@ -395,7 +393,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = -expression.getAdjustedLowerLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(expression, ConstraintType.LOWER);
structure.setConstraintMap(indCnstr, expression, ConstraintType.LOWER, false);
indCnstr++;
indSlack++;
Expand All @@ -413,7 +410,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = variable.getAdjustedUpperLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(variable, ConstraintType.UPPER);
structure.setConstraintMap(indCnstr, variable, ConstraintType.UPPER, false);
indCnstr++;
indSlack++;
Expand All @@ -431,7 +427,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = -variable.getAdjustedLowerLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(variable, ConstraintType.LOWER);
structure.setConstraintMap(indCnstr, variable, ConstraintType.LOWER, false);
indCnstr++;
indSlack++;
Expand All @@ -453,7 +448,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = expression.getAdjustedLowerLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(expression, ConstraintType.LOWER);
structure.setConstraintMap(indCnstr, expression, ConstraintType.LOWER, true);
indCnstr++;
indSlack++;
Expand All @@ -473,7 +467,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = -expression.getAdjustedUpperLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(expression, ConstraintType.UPPER);
structure.setConstraintMap(indCnstr, expression, ConstraintType.UPPER, true);
indCnstr++;
indSlack++;
Expand All @@ -491,7 +484,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = variable.getAdjustedLowerLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(variable, ConstraintType.LOWER);
structure.setConstraintMap(indCnstr, variable, ConstraintType.LOWER, true);
indCnstr++;
indSlack++;
Expand All @@ -509,7 +501,6 @@ static SimplexTableau build(final ExpressionsBasedModel model) {
double rhs = -variable.getAdjustedUpperLimit();
retConstraintsRHS.set(indCnstr, rhs);

structure.slack[indSlack - baseSlackVars] = EntryPair.of(variable, ConstraintType.UPPER);
structure.setConstraintMap(indCnstr, variable, ConstraintType.UPPER, true);
indCnstr++;
indSlack++;
Expand Down
35 changes: 23 additions & 12 deletions src/main/java/org/ojalgo/type/CalendarDateUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* <p>
* Designed to complement {@linkplain CalendarDate}. It is essentially equivalent to {@linkplain ChronoUnit},
* but with a sligthly smaller set of members (and the addtional {@linkplain #QUARTER}). It has been
* but with a slightly smaller set of members (and the additional {@linkplain #QUARTER}). It has been
* retrofitted to implement the {@linkplain TemporalUnit} interface.
* </p>
*
Expand Down Expand Up @@ -83,19 +83,19 @@ public enum CalendarDateUnit implements TemporalUnit, CalendarDate.Resolution {
/**
*
*/
MONTH(ChronoUnit.MONTHS, (TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR) / 1200L, "months"),
MONTH(ChronoUnit.MONTHS, TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR / 1200L, "months"),
/**
*
*/
QUARTER(null, (TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR) / 400L, "quarters"),
QUARTER(null, TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR / 400L, "quarters"),
/**
*
*/
YEAR(ChronoUnit.YEARS, (TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR) / 100L, "years"),
YEAR(ChronoUnit.YEARS, TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR / 100L, "years"),
/**
*
*/
DECADE(ChronoUnit.DECADES, (TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR) / 10L, "decades"),
DECADE(ChronoUnit.DECADES, TypeUtils.HOURS_PER_CENTURY * TypeUtils.MILLIS_PER_HOUR / 10L, "decades"),
/**
*
*/
Expand Down Expand Up @@ -130,6 +130,7 @@ public enum CalendarDateUnit implements TemporalUnit, CalendarDate.Resolution {
myLabel = label;
}

@Override
@SuppressWarnings({ "unchecked" })
public <R extends Temporal> R addTo(final R temporal, final long amount) {
if (temporal instanceof CalendarDate) {
Expand All @@ -141,14 +142,17 @@ public <R extends Temporal> R addTo(final R temporal, final long amount) {
}
}

@Override
public CalendarDate adjustInto(final CalendarDate temporal) {
return new CalendarDate(this.adjustInto(temporal.millis));
}

@Override
public long adjustInto(final long epochMilli) {
return ((epochMilli / myDurationInMillis) * myDurationInMillis) + myHalf;
return epochMilli / myDurationInMillis * myDurationInMillis + myHalf;
}

@Override
public Temporal adjustInto(final Temporal temporal) {

if (temporal instanceof CalendarDate) {
Expand Down Expand Up @@ -187,7 +191,7 @@ public Temporal adjustInto(final Temporal temporal) {

} else if (CalendarDateUnit.QUARTER.toDurationInMillis() == myDurationInMillis) {

int nextMonth = 3 + (3 * (retVal.get(ChronoField.MONTH_OF_YEAR) / 3));
int nextMonth = 3 + 3 * (retVal.get(ChronoField.MONTH_OF_YEAR) / 3);
retVal = retVal.with(ChronoField.MONTH_OF_YEAR, nextMonth).with(ChronoField.DAY_OF_MONTH, 1L).minus(1L, ChronoUnit.DAYS);

} else if (CalendarDateUnit.YEAR.toDurationInMillis() == myDurationInMillis) {
Expand All @@ -196,17 +200,17 @@ public Temporal adjustInto(final Temporal temporal) {

} else if (CalendarDateUnit.DECADE.toDurationInMillis() == myDurationInMillis) {

int nextYear = 10 + (10 * (retVal.get(ChronoField.YEAR) / 10));
int nextYear = 10 + 10 * (retVal.get(ChronoField.YEAR) / 10);
retVal = retVal.with(ChronoField.YEAR, nextYear).with(TemporalAdjusters.firstDayOfYear()).minus(1L, ChronoUnit.DAYS);

} else if (CalendarDateUnit.CENTURY.toDurationInMillis() == myDurationInMillis) {

int nextYear = 100 + (100 * (retVal.get(ChronoField.YEAR) / 100));
int nextYear = 100 + 100 * (retVal.get(ChronoField.YEAR) / 100);
retVal = retVal.with(ChronoField.YEAR, nextYear).with(TemporalAdjusters.firstDayOfYear()).minus(1L, ChronoUnit.DAYS);

} else if (CalendarDateUnit.MILLENIUM.toDurationInMillis() == myDurationInMillis) {

int nextYear = 1000 + (1000 * (retVal.get(ChronoField.YEAR) / 1000));
int nextYear = 1000 + 1000 * (retVal.get(ChronoField.YEAR) / 1000);
retVal = retVal.with(ChronoField.YEAR, nextYear).with(TemporalAdjusters.firstDayOfYear()).minus(1L, ChronoUnit.DAYS);
}
}
Expand All @@ -219,6 +223,7 @@ public Temporal adjustInto(final Temporal temporal) {
}
}

@Override
public long between(final Temporal temporal1Inclusive, final Temporal temporal2Exclusive) {
if (myChronoUnit != null) {
return myChronoUnit.between(temporal1Inclusive, temporal2Exclusive);
Expand Down Expand Up @@ -260,15 +265,15 @@ public double convert(final double sourceDurationMeasure, final CalendarDateUnit

public long convert(final long sourceMeassure, final CalendarDateUnit sourceUnit) {
Optional<TimeUnit> tmpTimeUnit = sourceUnit.getTimeUnit();
if ((myTimeUnit != null) && (tmpTimeUnit.isPresent())) {
if (myTimeUnit != null && tmpTimeUnit.isPresent()) {
return myTimeUnit.convert(sourceMeassure, tmpTimeUnit.get());
} else {
return Math.round(this.convert((double) sourceMeassure, sourceUnit));
}
}

public long count(final long aFromValue, final long aToValue) {
return ((myHalf + this.adjustInto(aToValue)) - this.adjustInto(aFromValue)) / myDurationInMillis;
return (myHalf + this.adjustInto(aToValue) - this.adjustInto(aFromValue)) / myDurationInMillis;
}

public long get(final TemporalUnit unit) {
Expand All @@ -283,6 +288,7 @@ public Optional<ChronoUnit> getChronoUnit() {
return Optional.ofNullable(myChronoUnit);
}

@Override
public Duration getDuration() {
if (myChronoUnit != null) {
return myChronoUnit.getDuration();
Expand All @@ -307,6 +313,7 @@ public boolean isCalendarUnit() {
return DAY.toDurationInMillis() <= this.toDurationInMillis();
}

@Override
public boolean isDateBased() {
if (myChronoUnit != null) {
return myChronoUnit.isDateBased();
Expand All @@ -315,6 +322,7 @@ public boolean isDateBased() {
}
}

@Override
public boolean isDurationEstimated() {
if (myChronoUnit != null) {
return myChronoUnit.isDurationEstimated();
Expand All @@ -323,6 +331,7 @@ public boolean isDurationEstimated() {
}
}

@Override
public boolean isTimeBased() {
if (myChronoUnit != null) {
return myChronoUnit.isTimeBased();
Expand All @@ -335,10 +344,12 @@ public CalendarDateDuration newDuration(final double meassure) {
return new CalendarDateDuration(meassure, this);
}

@Override
public long toDurationInMillis() {
return myDurationInMillis;
}

@Override
public long toDurationInNanos() {
return myDurationInNanos;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ojalgo/type/FloatingPointReceptacle.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.ojalgo.structure.Access1D;

/**
* An array (double[] or float[]) builder/converter. The arrays are built by prepending/appending segements of
* An array (double[] or float[]) builder/converter. The arrays are built by prepending/appending segments of
* anything that can be converted to either double[] or float[] arrays. The total/aggregated arrays are
* extracted by calling {@link #supplyTo(double[])} or {@link #supplyTo(float[])}. This also serves as a type
* converter from any number type to double or float.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ojalgo/type/math/MathType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* The naming scheme is the {@link NumberSet} symbol combined with the total number of bits used to represent
* the components.
* <p>
* For instance: C128 referns to C = {@link ComplexNumber} which is implemeted as two double:s each of 64
* For instance: C128 refers to C = {@link ComplexNumber} which is implemented as two double:s each of 64
* bits. If there would be a float based complex number implementation it would be named C064.
* <p>
* R032 is a Real number implemented/approximated using a single 32 bit float.
Expand Down

0 comments on commit 9fa88be

Please sign in to comment.