tdf#154285 Check correct number of arguments to SbRtl_Now function
[LibreOffice.git] / nlpsolver / src / com / sun / star / comp / Calc / NLPSolver / SCOSolverImpl.java
blobc1798606d4e6f6853703156d9d15b9a1cfa3eaa1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2009 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 package com.sun.star.comp.Calc.NLPSolver;
30 import com.sun.star.comp.Calc.NLPSolver.dialogs.IEvolutionarySolverStatusDialog;
31 import com.sun.star.uno.XComponentContext;
32 import com.sun.star.lib.uno.helper.Factory;
33 import com.sun.star.lang.XSingleComponentFactory;
34 import com.sun.star.registry.XRegistryKey;
35 import net.adaptivebox.sco.SCAgent;
36 import net.adaptivebox.global.IUpdateCycleEngine;
37 import net.adaptivebox.knowledge.Library;
38 import net.adaptivebox.knowledge.SearchPoint;
41 public final class SCOSolverImpl extends BaseEvolutionarySolver
42 implements com.sun.star.lang.XServiceInfo
44 private static final String m_implementationName = SCOSolverImpl.class.getName();
45 private static final String[] m_serviceNames = {
46 "com.sun.star.sheet.Solver",
47 "com.sun.star.beans.PropertySet"
50 public SCOSolverImpl( XComponentContext context )
52 super(context, "SCO Evolutionary Algorithm");
54 registerProperty(m_librarySize); //SCO allows the user to specify the size of the library
57 public static XSingleComponentFactory __getComponentFactory( String sImplementationName ) {
58 XSingleComponentFactory xFactory = null;
60 if ( sImplementationName.equals( m_implementationName ) )
61 xFactory = Factory.createComponentFactory(SCOSolverImpl.class, m_serviceNames);
62 return xFactory;
65 public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey ) {
66 return Factory.writeRegistryServiceInfo(m_implementationName,
67 m_serviceNames,
68 xRegistryKey);
71 // com.sun.star.lang.XServiceInfo:
72 public String getImplementationName() {
73 return m_implementationName;
76 public boolean supportsService( String sService ) {
77 int len = m_serviceNames.length;
79 for( int i=0; i < len; i++) {
80 if (sService.equals(m_serviceNames[i]))
81 return true;
83 return false;
86 public String[] getSupportedServiceNames() {
87 return m_serviceNames;
90 public void solve() {
91 initializeSolve();
93 if (m_problemEncoder == null)
95 return;
98 //Init:
99 int swarmSize = m_swarmSize.getValue();
100 SCAgent[] agents = new SCAgent[swarmSize];
101 for (int i = 0; i < swarmSize; i++) {
102 agents[i] = new SCAgent();
103 agents[i].setProblemEncoder(m_problemEncoder);
104 agents[i].setSpecComparator(m_specCompareEngine);
105 agents[i].setExternalLib(m_library);
108 //Learn:
109 m_solverStatusDialog.setVisible(true);
110 int learningCycles = m_learningCycles.getValue();
111 m_solverStatusDialog.setMaxIterations(learningCycles);
112 m_solverStatusDialog.setMaxStagnation(m_required.getValue());
113 int learningCycle = 1;
114 long runtime = 0;
115 do {
116 long startTime = System.nanoTime();
118 if (learningCycle >= m_learningCycles.getValue())
119 learningCycle = 1;
121 if (m_solverStatusDialog.getUserState() == IEvolutionarySolverStatusDialog.CONTINUE)
122 lockDocument();
124 m_toleratedCount = 0;
125 m_toleratedMin = -1.0 * m_tolerance.getValue();
126 m_toleratedMax = m_tolerance.getValue();
127 for (; learningCycle <= learningCycles &&
128 m_toleratedCount < m_required.getValue() &&
129 m_solverStatusDialog.getUserState() != IEvolutionarySolverStatusDialog.CANCEL; learningCycle++) {
130 for (int i = 0; i < swarmSize; i++) {
131 SearchPoint point = agents[i].generatePoint();
132 boolean inRange = (point.getObjectiveValue() >= m_toleratedMin && point.getObjectiveValue() <= m_toleratedMax);
133 if (Library.replace(m_envCompareEngine, point, m_totalBestPoint)) {
134 m_solverStatusDialog.setBestSolution(m_totalBestPoint.getObjectiveValue(), m_totalBestPoint.isFeasible());
135 if (!inRange) {
136 m_toleratedMin = point.getObjectiveValue() - m_tolerance.getValue();
137 m_toleratedMax = point.getObjectiveValue() + m_tolerance.getValue();
138 m_toleratedCount = 0;
143 for (int i = 0; i < swarmSize; i++)
144 agents[i].updateInfo();
146 if (m_specCompareEngine instanceof IUpdateCycleEngine)
147 ((IUpdateCycleEngine)m_specCompareEngine).updateCycle(learningCycle);
149 m_solverStatusDialog.setIteration(learningCycle);
150 m_solverStatusDialog.setStagnation(m_toleratedCount);
151 m_solverStatusDialog.setRuntime(runtime + (System.nanoTime() - startTime));
152 m_xReschedule.reschedule();
155 applySolution(); //show the current solution
156 unlockDocument(); //allow the solution to be displayed
158 runtime += (System.nanoTime() - startTime);
159 m_solverStatusDialog.setRuntime(runtime);
160 } while (m_solverStatusDialog.waitForUser() == IEvolutionarySolverStatusDialog.CONTINUE);
162 lockDocument();
164 finalizeSolve();