LightJason - AgentSpeak(L++)
linearprogram/CSolve.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin.math.linearprogram;
25 
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.apache.commons.math3.optim.MaxIter;
28 import org.apache.commons.math3.optim.OptimizationData;
29 import org.apache.commons.math3.optim.PointValuePair;
30 import org.apache.commons.math3.optim.linear.LinearConstraint;
31 import org.apache.commons.math3.optim.linear.LinearConstraintSet;
32 import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
33 import org.apache.commons.math3.optim.linear.NonNegativeConstraint;
34 import org.apache.commons.math3.optim.linear.SimplexSolver;
35 import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
43 
44 import javax.annotation.Nonnegative;
45 import javax.annotation.Nonnull;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.LinkedList;
49 import java.util.List;
50 import java.util.Objects;
51 
52 
72 public final class CSolve extends IBuiltinAction
73 {
77  private static final long serialVersionUID = -9105794980077188037L;
78 
82  public CSolve()
83  {
84  super( 3 );
85  }
86 
87  @Nonnegative
88  @Override
89  public final int minimalArgumentNumber()
90  {
91  return 1;
92  }
93 
94  @Nonnull
95  @Override
96  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
97  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
98  {
99  // first argument is the LP pair object, second argument is the goal-type (maximize / minimize),
100  // third & fourth argument can be the number of iterations or string with "non-negative" variables
101  final List<OptimizationData> l_settings = new LinkedList<>();
102 
103  final Pair<LinearObjectiveFunction, Collection<LinearConstraint>> l_default = p_argument.get( 0 ).raw();
104  l_settings.add( l_default.getLeft() );
105  l_settings.add( new LinearConstraintSet( l_default.getRight() ) );
106 
107  p_argument.subList( 1, p_argument.size() ).stream()
108  .map( i ->
109  {
110  if ( CCommon.rawvalueAssignableTo( i, Number.class ) )
111  return new MaxIter( i.raw() );
112 
113  if ( CCommon.rawvalueAssignableTo( i, String.class ) )
114  switch ( i.<String>raw().trim().toLowerCase() )
115  {
116  case "non-negative":
117  return new NonNegativeConstraint( true );
118  case "maximize":
119  return GoalType.MAXIMIZE;
120  case "minimize":
121  return GoalType.MINIMIZE;
122 
123  default:
124  return null;
125  }
126 
127  return null;
128  } )
129  .filter( Objects::nonNull )
130  .forEach( l_settings::add );
131 
132  // optimze and return
133  final SimplexSolver l_lp = new SimplexSolver();
134  final PointValuePair l_result = l_lp.optimize( l_settings.toArray( new OptimizationData[l_settings.size()] ) );
135 
136  p_return.add( CRawTerm.from( l_result.getValue() ) );
137  p_return.add( CRawTerm.from( l_result.getPoint().length ) );
138  Arrays.stream( l_result.getPoint() ).boxed().map( CRawTerm::from ).forEach( p_return::add );
139 
140  return CFuzzyValue.from( true );
141  }
142 
143 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
solves the linear program and returns the solution.
execution context with local data
Definition: IContext.java:42
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45