Skip to content

Add sgnsqr external function f(X) = sgn(x)*x^2 #3622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pyomo/contrib/ampl_function_demo/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

def build_ampl_function_demo(user_args=[], parallel=None):
return build_cmake_project(
targets=['src'],
package_name='asl_external_demo',
description='AMPL External function demo library',
user_args=['-DBUILD_AMPLASL_IF_NEEDED=ON'] + user_args,
targets=["src"],
package_name="asl_external_demo",
description="AMPL External function demo library",
user_args=["-DBUILD_AMPLASL_IF_NEEDED=ON"] + user_args,
parallel=parallel,
)

Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/ampl_function_demo/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@


def load():
ExtensionBuilderFactory.register('ampl_function_demo')(AMPLFunctionDemoBuilder)
ExtensionBuilderFactory.register("ampl_function_demo")(AMPLFunctionDemoBuilder)
23 changes: 23 additions & 0 deletions pyomo/contrib/ampl_function_demo/src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ extern real scbrt(arglist *al){
}


/* A function where f(x) = sgn(x)x^2
*/
extern real sgnsqr(arglist *al){
real x = al->ra[al->at[0]];

real y = copysign(x * x, x);

// Compute the first derivative, if requested.
if (al->derivs!=NULL) {
al->derivs[0] = copysign(1, x) * 2 * x;
}

// Compute the second derivative, if requested.
if (al->hes!=NULL) {
al->hes[0] = copysign(2, x);
}

return y;
}


// Register external functions defined in this library with the ASL
void funcadd(AmplExports *ae){
/* Arguments for addfunc (this is not fully detailed; see funcadd.h)
Expand All @@ -173,6 +194,8 @@ void funcadd(AmplExports *ae){
*/
addfunc("safe_cbrt", (rfunc)scbrt,
FUNCADD_REAL_VALUED, 1, NULL);
addfunc("sgnsqr", (rfunc)sgnsqr,
FUNCADD_REAL_VALUED, 1, NULL);
addfunc("demo_function", (rfunc)demo_function,
FUNCADD_REAL_VALUED|FUNCADD_STRING_ARGS, -2, NULL);
}
23 changes: 19 additions & 4 deletions pyomo/contrib/ampl_function_demo/tests/test_ampl_function_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
from pyomo.core.base.external import nan

flib = find_library("asl_external_demo")
is_pypy = platform.python_implementation().lower().startswith('pypy')
is_pypy = platform.python_implementation().lower().startswith("pypy")


@unittest.skipUnless(flib, 'Could not find the "asl_external_demo.so" library')
class TestAMPLExternalFunction(unittest.TestCase):
@unittest.skipIf(is_pypy, 'Cannot evaluate external functions under pypy')
@unittest.skipIf(is_pypy, "Cannot evaluate external functions under pypy")
def test_eval_function(self):
m = pyo.ConcreteModel()
m.tf = pyo.ExternalFunction(library=flib, function="demo_function")
Expand All @@ -34,7 +34,7 @@ def test_eval_function(self):
m.cbrt.evaluate_fgh([0]), (0, [100951], [-1.121679e13]), reltol=1e-5
)

@unittest.skipIf(is_pypy, 'Cannot evaluate external functions under pypy')
@unittest.skipIf(is_pypy, "Cannot evaluate external functions under pypy")
def test_eval_function_fgh(self):
m = pyo.ConcreteModel()
m.tf = pyo.ExternalFunction(library=flib, function="demo_function")
Expand All @@ -52,7 +52,7 @@ def test_eval_function_fgh(self):
)

@unittest.skipUnless(
check_available_solvers('ipopt'), "The 'ipopt' solver is not available"
check_available_solvers("ipopt"), "The 'ipopt' solver is not available"
)
def test_solve_function(self):
m = pyo.ConcreteModel()
Expand All @@ -68,3 +68,18 @@ def test_solve_function(self):
solver = pyo.SolverFactory("ipopt")
solver.solve(m, tee=True)
self.assertAlmostEqual(m.x(), 6, 4)

@unittest.skipIf(is_pypy, "Cannot evaluate external functions under pypy")
def test_eval_sqnsqr_function_fgh(self):
m = pyo.ConcreteModel()
m.tf = pyo.ExternalFunction(library=flib, function="sgnsqr")

f, g, h = m.tf.evaluate_fgh((2,))
self.assertEqual(f, 4)
self.assertEqual(g, [4])
self.assertEqual(h, [2])

f, g, h = m.tf.evaluate_fgh((-2,))
self.assertAlmostEqual(f, -4)
self.assertStructuredAlmostEqual(g, [4])
self.assertStructuredAlmostEqual(h, [-2])
Loading