@Override public double[][] infer(double[][] features, KObject origin, KInternalDataManager manager) { int maxOutput = ((MetaEnum) origin.metaClass().outputs()[0].type()).literals().length; KMemoryChunk ks = manager.closestChunk( origin.universe(), origin.now(), origin.uuid(), origin.metaClass(), ((AbstractKObject) origin).previousResolved()); int dependenciesIndex = origin.metaClass().dependencies().index(); // check if chunk is empty int size = (maxOutput + 1) * (origin.metaClass().inputs().length * NUMOFFIELDS + 1); if (ks.getDoubleArraySize(dependenciesIndex, origin.metaClass()) == 0) { return null; } Array1D state = new Array1D(size, 0, origin.metaClass().dependencies().index(), ks, origin.metaClass()); double[][] result = new double[features.length][1]; for (int j = 0; j < features.length; j++) { result[j] = new double[1]; double maxprob = 0; double prob = 0; for (int output = 0; output < maxOutput; output++) { prob = getProba(features[j], output, state, origin.metaClass().dependencies()); if (prob > maxprob) { maxprob = prob; result[j][0] = output; } } } return result; }
public static void main(String[] args) { KMetaModel metaModel = new MetaModel("SmartCityMetaModel"); KMetaClass metaClassCity = metaModel.addMetaClass("City"); KMetaClass metaClassDistrict = metaModel.addMetaClass("District"); metaClassCity.addAttribute("name", KPrimitiveTypes.STRING); metaClassDistrict.addAttribute("name", KPrimitiveTypes.STRING); metaClassDistrict.addAttribute("nbcitizen", KPrimitiveTypes.LONG); // create the reference districts from City to district with multiplicity 0..* metaClassCity.addRelation("districts", metaClassDistrict, null); KModel model = metaModel.createModel( DataManagerBuilder.create().withScheduler(new DirectScheduler()).build()); model.connect( o -> { // Create reflexively a model object using the metaClass name KObject city = model.createByName("City", BASE_UNIVERSE, BASE_TIME); city.setByName("name", "MySmartCity"); // Create reflexively a model object using the metaClass KObject district_1 = model.create(metaClassDistrict, BASE_UNIVERSE, BASE_TIME); district_1.setByName("name", "District_1"); district_1.setByName("nbcitizen", 10000); // Create reflexively a model object using the metaClass KObject district_2 = model.createByName("District", BASE_UNIVERSE, BASE_TIME); district_2.setByName("name", "District_2"); district_2.setByName("nbcitizen", 50000); // Add the two district to the City city.addByName("districts", district_1); city.addByName("districts", district_2); // Save the full model as JSON in the console model.universe(BASE_UNIVERSE).time(BASE_TIME).json().save(city, System.out::println); // Visiting all reachable objects from the city city.visit( elem -> { System.out.println("Visiting..." + elem.toJSON()); return KVisitResult.CONTINUE; }, o1 -> System.out.println("End of the visit")); // Visiting all attributes of an object city.visitAttributes( (metaAttribute, value) -> { System.out.println( "City attribute " + metaAttribute.metaName() + ", type=" + metaAttribute.attributeTypeId() + "=" + value); }); // Finally any object have a UUID and can be retrieve from it long cityUUID = city.uuid(); System.out.println("City uuid=" + cityUUID); model.lookup( BASE_UNIVERSE, BASE_TIME, cityUUID, new KCallback<KObject>() { @Override public void on(KObject resolvedObject) { System.out.println("Resolved=" + resolvedObject.toJSON()); } }); }); }
@Override public void train( double[][] trainingSet, double[][] expectedResultSet, KObject origin, KInternalDataManager manager) { int maxOutput = ((MetaEnum) origin.metaClass().outputs()[0].type()).literals().length; KMemoryChunk ks = manager.preciseChunk( origin.universe(), origin.now(), origin.uuid(), origin.metaClass(), ((AbstractKObject) origin).previousResolved()); int dependenciesIndex = origin.metaClass().dependencies().index(); // Create initial chunk if empty int size = (maxOutput + 1) * (origin.metaClass().inputs().length * NUMOFFIELDS + 1); if (ks.getDoubleArraySize(dependenciesIndex, origin.metaClass()) == 0) { ks.extendDoubleArray(origin.metaClass().dependencies().index(), size, origin.metaClass()); for (int i = 0; i < size; i++) { ks.setDoubleArrayElem(dependenciesIndex, i, 0, origin.metaClass()); } } Array1D state = new Array1D(size, 0, origin.metaClass().dependencies().index(), ks, origin.metaClass()); // update the state for (int i = 0; i < trainingSet.length; i++) { int output = (int) expectedResultSet[i][0]; for (int j = 0; j < origin.metaClass().inputs().length; j++) { // If this is the first datapoint if (state.get(getCounter(output, origin.metaClass().dependencies())) == 0) { state.set(getIndex(j, output, MIN, origin.metaClass().dependencies()), trainingSet[i][j]); state.set(getIndex(j, output, MAX, origin.metaClass().dependencies()), trainingSet[i][j]); state.set(getIndex(j, output, SUM, origin.metaClass().dependencies()), trainingSet[i][j]); state.set( getIndex(j, output, SUMSQUARE, origin.metaClass().dependencies()), trainingSet[i][j] * trainingSet[i][j]); } else { if (trainingSet[i][j] < state.get(getIndex(j, output, MIN, origin.metaClass().dependencies()))) { state.set( getIndex(j, output, MIN, origin.metaClass().dependencies()), trainingSet[i][j]); } if (trainingSet[i][j] > state.get(getIndex(j, output, MAX, origin.metaClass().dependencies()))) { state.set( getIndex(j, output, MAX, origin.metaClass().dependencies()), trainingSet[i][j]); } state.add(getIndex(j, output, SUM, origin.metaClass().dependencies()), trainingSet[i][j]); state.add( getIndex(j, output, SUMSQUARE, origin.metaClass().dependencies()), trainingSet[i][j] * trainingSet[i][j]); } // update global stat if (state.get(getCounter(maxOutput, origin.metaClass().dependencies())) == 0) { state.set( getIndex(j, maxOutput, MIN, origin.metaClass().dependencies()), trainingSet[i][j]); state.set( getIndex(j, maxOutput, MAX, origin.metaClass().dependencies()), trainingSet[i][j]); state.set( getIndex(j, maxOutput, SUM, origin.metaClass().dependencies()), trainingSet[i][j]); state.set( getIndex(j, maxOutput, SUMSQUARE, origin.metaClass().dependencies()), trainingSet[i][j] * trainingSet[i][j]); } else { if (trainingSet[i][j] < state.get(getIndex(j, maxOutput, MIN, origin.metaClass().dependencies()))) { state.set( getIndex(j, maxOutput, MIN, origin.metaClass().dependencies()), trainingSet[i][j]); } if (trainingSet[i][j] > state.get(getIndex(j, maxOutput, MAX, origin.metaClass().dependencies()))) { state.set( getIndex(j, maxOutput, MAX, origin.metaClass().dependencies()), trainingSet[i][j]); } state.add( getIndex(j, maxOutput, SUM, origin.metaClass().dependencies()), trainingSet[i][j]); state.add( getIndex(j, maxOutput, SUMSQUARE, origin.metaClass().dependencies()), trainingSet[i][j] * trainingSet[i][j]); } } // Update Global counters state.add(getCounter(output, origin.metaClass().dependencies()), 1); state.add(getCounter(maxOutput, origin.metaClass().dependencies()), 1); } }