2. Standard - How Do I ...
2.1 - Generate random numbers from
a probability distribution?
2.2 - Find the mean and standard
deviation of a vector?
2.3 - Integrate a function numerically?
2.4 - Solve an equation numerically?
2.5 - Find the inverse of a matrix?
2.6 - Solve simultaneous linear equations?
2.7 - Find the shortest path in a
graph?
2.8 - Optimize using linear programming?
2.9 - Use probability distributions
to model arrivals?
2.10 - Color the vertices and edges
of a graph?
2.11 - Solve a Traveling Salesman
Problem?
2.12 - Solve a Vehicle Routing Problem?
2.13 - Estimate the population mean,
median and proportion from a sample?
2.14 - Perform correlation analysis?
2.15 - Solve multiple regression
problems?
// Get
a random scaler.
double scaler = dist.getRandomScaler();
// Get a random vector.
VectorI
vector = dist.getRandomVector(10);
// Get a random matrix
MatrixI
matrix = dist.getRandomMatrix(10,
10);
}
}
// A matrix multiplied by its inverse should
equal the identity matrix
MatrixI test = algebra.multiply(inverse,
matrix);
MatrixI identity = new SparseMatrix(new
DenseVector(10,
1.0));
if(test.equals(identity))
System.out.println("OK");
else
System.out.println("ERROR");
}
catch(LinearException
e){}
}
}
class Simultaneous
{
public static void
main(String[] args)
{
try{
AlgebraI algebra = new Algebra();
DistributionI dist = new UniformDistribution(1.0, 2.0);
DecompositionI
decomp = new QRIteration();
//
Build and decompose the equations.
MatrixI equations = dist.getRandomMatrix(10, 10);
decomp.decompose(equations);
//
Solve first set
VectorI rightHandSide1 = dist.getRandomVector(10);
VectorI solution1 = decomp.solveEquations(rightHandSide1);
//
The equations multiplied by the solution should equal the rhs.
if(algebra.multiply(equations, solution1).equals(rightHandSide1))
System.out.println("OK-1");
else
System.out.println("ERROR-1");
//
Solve second set
VectorI rightHandSide2 = dist.getRandomVector(10);
VectorI solution2 = decomp.solveEquations(rightHandSide2);
//
The equations multiplied by the solution should equal the rhs.
if(algebra.multiply(equations, solution2).equals(rightHandSide2))
System.out.println("OK-2");
else
System.out.println("ERROR-2");
}
catch(LinearException
e){}
}
}
|
(max) Z = 20x1 + 15x2 c1: x1
<= 100
|
import drasys.or.mp.*;
import drasys.or.mp.lp.*;
public class MpExample
{
public static void
main(String[] args)
throws Exception
{
SizableProblemI
prob = new Problem(4,2);
prob.getMetadata().put("lp.isMaximize",
"true");
prob.newVariable("x1").setObjectiveCoefficient(20.0);
prob.newVariable("x2").setObjectiveCoefficient(15.0);
final byte LTE = Constraint.LESS;
final byte GTE = Constraint.GREATER;
prob.newConstraint("c1").setType(LTE).setRightHandSide(
100.0);
prob.newConstraint("c2").setType(LTE).setRightHandSide(
100.0);
prob.newConstraint("c3").setType(LTE).setRightHandSide(
6000.0);
prob.newConstraint("c4").setType(GTE).setRightHandSide(
2000.0);
//
Columnwise insertion is more efficient for the default matrix.
prob.setCoefficientAt("c1",
"x1", 1.0);
prob.setCoefficientAt("c3",
"x1", 50.0);
prob.setCoefficientAt("c4",
"x1", 20.0);
prob.setCoefficientAt("c2",
"x2", 1.0);
prob.setCoefficientAt("c3",
"x2", 35.0);
prob.setCoefficientAt("c4",
"x2", 15.0);
LinearProgrammingI
lp = new DenseSimplex(prob);
double ans = lp.solve();
System.out.println("Solution
= "+ans);
}
}
class Coloring
{
public static void
main(String[] args)
{
String[] colors = {"red",
"green", "blue", "grey"};
SparseGraph
graph = new SparseGraph();
try{
//
Add Vertices
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
graph.addVertex("F");
graph.addVertex("G");
//
Add Edges
graph.addEdge("A","B","2");
graph.addEdge("A","C","1");
graph.addEdge("D","B","3");
graph.addEdge("D","C","7");
graph.addEdge("D","E","5");
graph.addEdge("D","F","9");
graph.addEdge("G","E","6");
graph.addEdge("G","F","10");
graph.addEdge("E","B","4",true);
graph.addEdge("C","F","8",true);
//
Create algorithm and color elements
ColoringI
alg = new WelshPowell(graph);
alg.colorEdges();
alg.colorVertices();
//
Print Vertex Colors
System.out.println("\n---
Vertex Colors ---");
for(Enumeration enum
= graph.vertices();
enum.hasMoreElements();){
VertexI vert = (VertexI)enum.nextElement();
String label = (String)vert.getKey();
String color = colors[alg.getVertexColor(vert)];
System.out.println(label + " = " + color);
}
//
Print Edge Colors
System.out.println("\n---
Edge Colors ---");
for(Enumeration enum
= graph.edges(); enum.hasMoreElements();){
EdgeI edge = (EdgeI)enum.nextElement();
String label = (String)edge.getValue();
String color = colors[alg.getEdgeColor(edge)];
System.out.println(label + " = " + color);
}
}catch(Exception e){}
}
}
PopulationMean
pMean = new PopulationMean(sample);
double mean = pMean.getMean();
double meanLowerBound
= pMean.getLowerBound(0.95);
double meanUpperBound
= pMean.getUpperBound(0.95);
PopulationMedian
pMedian = new PopulationMedian(sample);
double median = pMean.getMedian();
double medianLowerBound
= pMedian.getLowerBound(0.95);
double medianUpperBound
= pMedian.getUpperBound(0.95);
PopulationProportion
pProp = new PopulationProportion(sample, 4);
double prop = pMean.getProportion();
double propLowerBound
= pProp.getLowerBound(0.95);
double propUpperBound
= pProp.getUpperBound(0.95);
}
}
double[][]data =
{
{47,4.2}, {71,8.1}, {64,6.8}, {35,4.3}, {43,5.0},
{60,7.5}, {38,4.7}, {59,5.9}, {67,6.9}, {56,5.7},
{67,5.7}, {57,5.4}, {69,7.5}, {38,3.8}, {54,5.9},
{76,6.3}, {53,5.7}, {40,4.0}, {47,5.2}, {23,2.2},
};
LinearCorrelation
lc = new LinearCorrelation(new
ColumnMajorMatrix(data));
MatrixI sumSquares =
lc.getSumOfSquares();
System.out.println("Sum
of squares for col-0 = " + sumSquares.elementAt(0);
System.out.println("Sum
of squares for col-1 = " + sumSquares.elementAt(1);
MatrixI sumProd = lc.getSumOfProducts();
System.out.println("Sum
of squares for col-0 = " + sumProd.elementAt(0,0);
System.out.println("Sum
of squares for col-1 = " + sumProd.elementAt(1,1);
System.out.println("Sum
of products for col-0 and col-1 = " + sumProd.elementAt(0,1);
MatrixI correlation =
lc.getCorrelation();
System.out.println("Correlation
for col-0 and col-1 = " + correlation.elementAt(0,1);
MatrixI upperBound =
lc.getUpperBound(0.95);
System.out.println("Correlation
upper bound for col-0 and col-1 = " + upperBound.elementAt(0,1);
MatrixI lowerBound =
lc.getLowerBound(0.95);
System.out.println("Correlation
lower bound for col-0 and col-1 = " + lowerBound.elementAt(0,1);
double[][] indep = {
{10.5, 2200}, { 2.5, 1000}, {13.1, 3250}, { 4.0, 1475}, {14.7, 3800},
{ 3.6, 1200}, { 7.1, 1900}, {22.5, 5575}, {17.0, 4200}, { 6.4, 1850},
};
VectorI dependent = new
DenseVector(da);
MatrixI independent
= new RowArrayMatrix(ia, true);
LinearRegressionI
reg = new ReverseLinear(dependent,
independent);
//
Print coefficients
VectorI coef = reg.getCoefficients();
System.out.println("Intercept
Coefficient = "+coef.elementAt(2));
System.out.println("Column-0
Coefficient = "+coef.elementAt(0));
System.out.println("Column-1
Coefficient = "+coef.elementAt(1));
//Print
regression report
System.out.println(reg);