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 java
.util
.logging
.Level
;
31 import java
.util
.logging
.Logger
;
33 import net
.adaptivebox
.deps
.DEPSAgent
;
34 import net
.adaptivebox
.deps
.behavior
.DEGTBehavior
;
35 import net
.adaptivebox
.deps
.behavior
.PSGTBehavior
;
36 import net
.adaptivebox
.global
.IUpdateCycleEngine
;
37 import net
.adaptivebox
.knowledge
.Library
;
38 import net
.adaptivebox
.knowledge
.SearchPoint
;
39 import net
.adaptivebox
.space
.BasicPoint
;
41 import com
.sun
.star
.comp
.Calc
.NLPSolver
.dialogs
.IEvolutionarySolverStatusDialog
;
42 import com
.sun
.star
.lang
.IllegalArgumentException
;
43 import com
.sun
.star
.lang
.XSingleComponentFactory
;
44 import com
.sun
.star
.lib
.uno
.helper
.Factory
;
45 import com
.sun
.star
.registry
.XRegistryKey
;
46 import com
.sun
.star
.uno
.XComponentContext
;
49 public final class DEPSSolverImpl
extends BaseEvolutionarySolver
50 implements com
.sun
.star
.lang
.XServiceInfo
52 private static final String m_implementationName
= DEPSSolverImpl
.class.getName();
53 private static final String
[] m_serviceNames
= {
54 "com.sun.star.sheet.Solver",
55 "com.sun.star.beans.PropertySet"
58 public DEPSSolverImpl( XComponentContext context
)
60 super(context
, "DEPS Evolutionary Algorithm");
62 registerProperty(m_agentSwitchRate
);
63 registerProperty(m_minFactor
);
64 registerProperty(m_maxFactor
);
65 registerProperty(m_CR
);
66 registerProperty(m_c1
);
67 registerProperty(m_c2
);
68 registerProperty(m_weight
);
69 registerProperty(m_CL
);
72 public static XSingleComponentFactory
__getComponentFactory( String sImplementationName
) {
73 XSingleComponentFactory xFactory
= null;
75 if ( sImplementationName
.equals( m_implementationName
) )
76 xFactory
= Factory
.createComponentFactory(DEPSSolverImpl
.class, m_serviceNames
);
80 public static boolean __writeRegistryServiceInfo( XRegistryKey xRegistryKey
) {
81 return Factory
.writeRegistryServiceInfo(m_implementationName
,
86 // com.sun.star.lang.XServiceInfo:
87 public String
getImplementationName() {
88 return m_implementationName
;
91 public boolean supportsService( String sService
) {
92 int len
= m_serviceNames
.length
;
94 for( int i
=0; i
< len
; i
++) {
95 if (sService
.equals(m_serviceNames
[i
]))
101 public String
[] getSupportedServiceNames() {
102 return m_serviceNames
;
105 private final PropertyInfo
<Double
> m_agentSwitchRate
= new PropertyInfo
<Double
>("AgentSwitchRate", 0.5, "Agent Switch Rate (DE Probability)");
107 private final PropertyInfo
<Double
> m_minFactor
= new PropertyInfo
<Double
>("DEFactorMin", 0.5, "DE: Min Scaling Factor (0-1.2)");
108 private final PropertyInfo
<Double
> m_maxFactor
= new PropertyInfo
<Double
>("DEFactorMax", 0.5, "DE: Max Scaling Factor (0-1.2)");
109 private final PropertyInfo
<Double
> m_CR
= new PropertyInfo
<Double
>("DECR", 0.9, "DE: Crossover Probability (0-1)");
111 private final PropertyInfo
<Double
> m_c1
= new PropertyInfo
<Double
>("PSC1", 1.494, "PS: Cognitive Constant");
112 private final PropertyInfo
<Double
> m_c2
= new PropertyInfo
<Double
>("PSC2", 1.494, "PS: Social Constant");
113 private final PropertyInfo
<Double
> m_weight
= new PropertyInfo
<Double
>("PSWeight", 0.729, "PS: Constriction Coefficient");
114 private final PropertyInfo
<Double
> m_CL
= new PropertyInfo
<Double
>("PSCL", 0.0, "PS: Mutation Probability (0-0.005)");
116 public void solve() {
118 m_librarySize
.setValue(m_swarmSize
.getValue()); //DEPS' library is as large as the swarm
119 } catch (IllegalArgumentException ex
) {
120 Logger
.getLogger(DEPSSolverImpl
.class.getName()).log(Level
.SEVERE
, null, ex
);
123 if (m_problemEncoder
== null)
129 DEPSAgent
[] agents
= new DEPSAgent
[m_swarmSize
.getValue()];
130 for (int i
= 0; i
< m_swarmSize
.getValue(); i
++) {
131 DEGTBehavior deGTBehavior
= new DEGTBehavior();
132 deGTBehavior
.MIN_FACTOR
= Math
.min(m_minFactor
.getValue(), m_maxFactor
.getValue());
133 deGTBehavior
.MAX_FACTOR
= Math
.max(m_minFactor
.getValue(), m_maxFactor
.getValue());
134 deGTBehavior
.CR
= m_CR
.getValue();
135 deGTBehavior
.setLibrary(m_library
);
137 PSGTBehavior psGTBehavior
= new PSGTBehavior();
138 psGTBehavior
.c1
= m_c1
.getValue();
139 psGTBehavior
.c2
= m_c2
.getValue();
140 psGTBehavior
.CL
= m_CL
.getValue();
141 psGTBehavior
.weight
= m_weight
.getValue();
142 psGTBehavior
.setLibrary(m_library
);
144 agents
[i
] = new DEPSAgent(m_problemEncoder
, deGTBehavior
, psGTBehavior
,
145 m_agentSwitchRate
.getValue(), m_specCompareEngine
,
146 m_library
.getSelectedPoint(i
));
150 m_solverStatusDialog
.setVisible(true);
151 m_solverStatusDialog
.setMaxIterations(m_learningCycles
.getValue());
152 m_solverStatusDialog
.setMaxStagnation(m_required
.getValue());
153 int learningCycle
= 1;
156 long startTime
= System
.nanoTime();
158 if (learningCycle
>= m_learningCycles
.getValue())
161 if (m_solverStatusDialog
.getUserState() == IEvolutionarySolverStatusDialog
.CONTINUE
)
164 m_toleratedCount
= 0;
165 m_toleratedMin
= -1.0 * m_tolerance
.getValue();
166 m_toleratedMax
= m_tolerance
.getValue();
167 for (; learningCycle
<= m_learningCycles
.getValue() &&
168 m_toleratedCount
< m_required
.getValue() &&
169 m_solverStatusDialog
.getUserState() != IEvolutionarySolverStatusDialog
.CANCEL
; learningCycle
++) {
170 m_library
.refreshGbest(m_specCompareEngine
);
172 for (int i
= 0; i
< m_swarmSize
.getValue(); i
++)
173 agents
[i
].generatePoint();
175 for (int i
= 0; i
< m_swarmSize
.getValue(); i
++)
178 for (int i
= 0; i
< m_swarmSize
.getValue(); i
++) {
179 SearchPoint agentPoint
= agents
[i
].getMGState();
180 boolean inRange
= (agentPoint
.getObjectiveValue() >= m_toleratedMin
&& agentPoint
.getObjectiveValue() <= m_toleratedMax
);
181 if (Library
.replace(m_envCompareEngine
, agentPoint
, m_totalBestPoint
)) {
182 m_solverStatusDialog
.setBestSolution(m_totalBestPoint
.getObjectiveValue(), m_totalBestPoint
.isFeasible());
184 m_toleratedMin
= agentPoint
.getObjectiveValue() - m_tolerance
.getValue();
185 m_toleratedMax
= agentPoint
.getObjectiveValue() + m_tolerance
.getValue();
186 m_toleratedCount
= 0;
191 if (m_specCompareEngine
instanceof IUpdateCycleEngine
)
192 ((IUpdateCycleEngine
)m_specCompareEngine
).updateCycle(learningCycle
);
194 m_solverStatusDialog
.setIteration(learningCycle
);
195 m_solverStatusDialog
.setStagnation(m_toleratedCount
);
196 m_solverStatusDialog
.setRuntime(runtime
+ (System
.nanoTime() - startTime
));
197 m_xReschedule
.reschedule();
200 applySolution(); //show the current solution
201 unlockDocument(); //allow the solution to be displayed
203 runtime
+= (System
.nanoTime() - startTime
);
204 m_solverStatusDialog
.setRuntime(runtime
);
205 } while (m_solverStatusDialog
.waitForUser() == IEvolutionarySolverStatusDialog
.CONTINUE
);