com.enterprisemath.math.lp.StandardLPProblemResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of em-math Show documentation
Show all versions of em-math Show documentation
Advanced mathematical algorithms.
The newest version!
package com.enterprisemath.math.lp;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import com.enterprisemath.math.algebra.Vector;
import com.enterprisemath.utils.DomainUtils;
import com.enterprisemath.utils.ValidationUtils;
/**
* Defines result of the LP (linear programming) problem.
* Result can be one of the following types:
*
* - There is no solution at all
* - There is one optimal solution
* - There are multiple optimal solutions
* - There is no optimal solution as objective function is unbounded and can grow to infinity
*
*
* @author radek.hecl
*
*/
public class StandardLPProblemResult {
/**
* Builder object.
*/
public static class Builder {
/**
* Value of the objective function.
*/
private Double value;
/**
* Optimal solutions
*/
private Set solutions = new HashSet();
/**
* Sets value of the objective function.
*
* @param value objective function value
* @return this instance
*/
public Builder setValue(Double value) {
this.value = value;
return this;
}
/**
* Sets the optimal solutions.
*
* @param solutions optimal solutions
* @return this instance
*/
public Builder setSolutions(Set solutions) {
this.solutions = DomainUtils.softCopySet(solutions);
return this;
}
/**
* Adds optimal solution into the set.
*
* @param solution optimal solution
* @return this instance
*/
public Builder addSolution(Vector solution) {
solutions.add(solution);
return this;
}
/**
* Builds the result object.
*
* @return created object
*/
public StandardLPProblemResult build() {
return new StandardLPProblemResult(this);
}
}
/**
* Value of the objective function.
*/
private Double value;
/**
* Optimal solutions
*/
private Set solutions = new HashSet();
/**
* Creates new instance.
*
* @param builder builder object
*/
public StandardLPProblemResult(Builder builder) {
value = builder.value;
solutions = Collections.unmodifiableSet(DomainUtils.softCopySet(builder.solutions));
guardInvariants();
}
/**
* Guards this object to be consistent. Throws exception if this is not the case.
*/
private void guardInvariants() {
ValidationUtils.guardNotNullCollection(solutions, "solutions cannot have null element");
}
/**
* Returns value of the objective function.
*
* @return value of the objective function
*/
public Double getValue() {
return value;
}
/**
* Returns set of optimal solutions.
*
* @return optimal solutions
*/
public Set getSolutions() {
return solutions;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
/**
* Creates the result.
*
* @param value value of objective function
* @param solutions solutions
* @return created instance
*/
public static StandardLPProblemResult create(Double value, Vector... solutions) {
return new StandardLPProblemResult.Builder().
setValue(value).
setSolutions(DomainUtils.createSet(solutions)).
build();
}
}