-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from alphaville/feature/codegen/halfspace
Support for halfspaces in python
- Loading branch information
Showing
19 changed files
with
321 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.6.0a1 | ||
0.6.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
from .cartesian import * | ||
from .zero import * | ||
from .finite_set import * | ||
from .halfspace import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from .constraint import Constraint | ||
import sys | ||
|
||
|
||
class Halfspace(Constraint): | ||
"""Halfspace | ||
A halfspace is a set of the form H = {c'x <= b}, where c is a given | ||
vector and b is a constant scalar. | ||
""" | ||
|
||
def __init__(self, normal_vector, offset): | ||
"""Construct a new halfspace H = {c'x <= b} | ||
:param normal_vector: vector c | ||
"param offset: parameter b | ||
""" | ||
self.__normal_vector = [float(x) for x in normal_vector] | ||
self.__offset = offset | ||
|
||
@property | ||
def normal_vector(self): | ||
""" | ||
Returns vector c | ||
:return: | ||
""" | ||
return self.__normal_vector | ||
|
||
@property | ||
def offset(self): | ||
""" | ||
Returns parameter b | ||
:return: | ||
""" | ||
return self.__offset | ||
|
||
def dimension(self): | ||
""" | ||
Dimension of the halfspace | ||
:return: length of normal vector | ||
""" | ||
return len(self.__normal_vector) | ||
|
||
def project(self, u): | ||
raise NotImplementedError() | ||
|
||
def distance_squared(self, u): | ||
raise NotImplementedError() | ||
|
||
def is_convex(self): | ||
""" | ||
A halfspace is a convex set | ||
:return: | ||
""" | ||
return True | ||
|
||
def is_compact(self): | ||
"""Whether the set is compact | ||
H is compact iff b < 0 and c = 0, in which case H is empty | ||
""" | ||
eps = sys.float_info.epsilon | ||
# if b < 0 and c = 0, then H is empty, hence compact | ||
if self.offset < 0 and sum([self.normal_vector[idx] | ||
for idx in range(self.dimension())]) < eps: | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,28 @@ | ||
import casadi.casadi as cs | ||
import opengen as og | ||
|
||
u = cs.SX.sym("u", 5) # decision variable (nu = 5) | ||
p = cs.SX.sym("p", 2) # parameter (np = 2) | ||
phi = og.functions.rosenbrock(u, p) # cost function | ||
u = cs.SX.sym("u", 5) # decision variable (nu = 5) | ||
p = cs.SX.sym("p", 2) # parameter (np = 2) | ||
phi = cs.dot(u, u) # cost function | ||
|
||
segment_ids = [0, 1, 2, 3, 4] | ||
cstrs = [og.constraints.NoConstraints, | ||
og.constraints.NoConstraints, | ||
og.constraints.Ball2(None, 1.5), | ||
og.constraints.NoConstraints, | ||
og.constraints.NoConstraints] | ||
dim = 5 | ||
bounds = og.constraints.CartesianProduct(segment_ids, cstrs) | ||
bounds = og.constraints.Halfspace([1., 2., 1., 5., 2.], -10.39) | ||
|
||
problem = og.builder.Problem(u, p, phi) \ | ||
.with_constraints(bounds) | ||
problem = og.builder.Problem(u, p, phi) \ | ||
.with_constraints(bounds) | ||
|
||
meta = og.config.OptimizerMeta() \ | ||
.with_version("0.0.0") \ | ||
.with_authors(["P. Sopasakis", "E. Fresk"]) \ | ||
.with_licence("CC4.0-By") \ | ||
.with_optimizer_name("potato") | ||
meta = og.config.OptimizerMeta() \ | ||
.with_optimizer_name("halfspace_optimizer") | ||
|
||
build_config = og.config.BuildConfiguration() \ | ||
.with_build_directory("my_optimizers") \ | ||
.with_build_mode(og.config.BuildConfiguration.RELEASE_MODE) \ | ||
.with_open_version('0.7.0-alpha.1') \ | ||
.with_tcp_interface_config() | ||
solver_config = og.config.SolverConfiguration() \ | ||
.with_tolerance(1e-5) \ | ||
.with_delta_tolerance(1e-4) \ | ||
.with_initial_penalty(890) \ | ||
.with_penalty_weight_update_factor(5) | ||
tcp_config = og.config.TcpServerConfiguration(bind_port=3305) | ||
build_config = og.config.BuildConfiguration() \ | ||
.with_build_directory('my_optimizers') \ | ||
.with_build_mode(og.config.BuildConfiguration.DEBUG_MODE) \ | ||
.with_open_version('0.7.1-alpha') \ | ||
.with_tcp_interface_config(tcp_interface_config=tcp_config) | ||
|
||
builder = og.builder.OpEnOptimizerBuilder(problem, | ||
meta, | ||
build_config, | ||
solver_config).with_verbosity_level(3).with_generate_not_build_flag(True) | ||
|
||
og.config.SolverConfiguration()) | ||
builder.build() | ||
|
||
o = og.tcp.OptimizerTcpManager('my_optimizers/potato') | ||
# o.start() | ||
# r = o.call([1.0, 50.0]) | ||
# if r.is_ok(): | ||
# status = r.get() | ||
# print(status.solution) | ||
# o.kill() | ||
|
Oops, something went wrong.