package edu.neu.ccs.satsolver; import java.util.*; import java.io.*; public class SATSolverUtil { public static OutputI calculateBias(InputInitialI ii) { // Return something // getInitialCalc( ii.getPairs() ); return getInitialCalc( ii.getPairs() ); } public static OutputI updateBias(InputUpdateI ui) { // Return Something return getUpdateCalc(ui); } /* getInitialCalc : takes in a Set of pairs from the Input Object * created by the Undergraduates and returns * Output object with the MaxBias and Polynomial */ private static OutputI getInitialCalc(java.util.Set pairs) { // make the set into an array so we can iterate over it Object[] ps = pairs.toArray(); // create a polynomial object with zero values for // all of its coefficients double[] baseValues = {0,0,0,0}; Polynomial appmean_x = new Polynomial(baseValues); // iterate through all of the pairs given // For each pair : 1. get its polynomial (tempP) // 2. multiply tempP by the pair's fractional weight // 3. add the tempP to appmean_x for(int i = 0; i < ps.length; i++) { PairI p = (PairI)ps[i]; Polynomial tempP = appSat.getPolynomial(p.getRelationNumber()); appmean_x = appmean_x.addPolynomials(tempP.multiplyBy(p.getFraction())); } // Take the derivative of the appmean_x(S) polynomial double max = appmean_x.findMax(); return new Output(max, appmean_x); } /* getUpdateCalc : takes in an inputUpdate object and returns the new max from * the added and subtracted polynomials. */ private static OutputI getUpdateCalc( InputUpdateI iu ) { // Pull out all the data structures hidden in the // InputUpdate Object PolynomialI oldPoly = (PolynomialI)iu.getPolynomialBefore(); double[] oldCoeffs = { oldPoly.getCoefficient(0), oldPoly.getCoefficient(1), oldPoly.getCoefficient(2), oldPoly.getCoefficient(3) }; Object[] aps = iu.getAddedPairs().toArray(); Object[] sps = iu.getSubtractedPairs().toArray(); // create a polynomial object with zero values for // all of its coefficients Polynomial appmean_x = new Polynomial(oldCoeffs); // iterate through all of the pairs given // For each pair : 1. get its polynomial (tempP) // 2. multiply tempP by the pair's fractional weight // 3. add the tempP to appmean_x for(int i = 0; i < aps.length; i++) { PairI p = (PairI)aps[i]; Polynomial tempP = appSat.getPolynomial(p.getRelationNumber()); appmean_x = appmean_x.addPolynomials(tempP.multiplyBy(p.getFraction())); } // iterate through all of the pairs given // For each pair : 1. get its polynomial (tempP) // 2. multiply tempP by the pair's fractional weight // 3. subtract the tempP to appmean_x for(int i = 0; i < sps.length; i++) { PairI p = (PairI)sps[i]; Polynomial tempP = appSat.getPolynomial(p.getRelationNumber()); appmean_x = appmean_x.subtractPolynomials(tempP.multiplyBy(p.getFraction())); } // Take the derivative of the appmean_x(S) polynomial double max = appmean_x.findMax(); return new Output(max, appmean_x); } }