-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
80 changed files
with
4,217 additions
and
645 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 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 @@ | ||
1.6.1 | ||
1.7.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::OptimizationsController < Api::ApiController | ||
before_action :set_optimization, only: [:show, :update, :destroy] | ||
|
||
# GET /api/v1/optimizations/1 | ||
def show | ||
json_response @optim | ||
end | ||
|
||
# POST /api/v1/optimizations | ||
def create | ||
@optim = Optimization.new(optim_params) | ||
authorize @optim | ||
@optim.save! | ||
@optim.create_optimizer | ||
@optim.set_owner(current_user) | ||
json_response @optim, :created | ||
end | ||
|
||
# PATCH /api/v1/optimizations/1 | ||
def update | ||
@optim.check_optimization_inputs(optim_params) | ||
inputs = {x: optim_params['x'], y: optim_params['y']} | ||
@optim.update!(inputs: inputs, outputs: {status: Optimization::RUNNING, x_suggested: nil}) | ||
OptimizationJob.perform_later(@optim) | ||
head :no_content | ||
end | ||
|
||
# DELETE /api/v1/optimizations/1 | ||
def destroy | ||
@proxy.destroy_optimizer | ||
@optim.destroy! | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def set_optimization | ||
@optim = Optimization.find(params[:id]) | ||
@proxy = @optim.proxy | ||
authorize @optim | ||
end | ||
|
||
def optim_params | ||
params.require(:optimization).permit! | ||
end | ||
|
||
end |
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class OptimizationJob < ActiveJob::Base | ||
def perform(optim) | ||
optim.perform | ||
end | ||
end |
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "thrift" | ||
require "optimizer_store" | ||
|
||
module WhatsOpt | ||
class OptimizerProxy < ServiceProxy | ||
|
||
CSTRS_TYPES = { | ||
'<' => WhatsOpt::Services::ConstraintType::LESS, | ||
'=' => WhatsOpt::Services::ConstraintType::EQUAL, | ||
'>' => WhatsOpt::Services::ConstraintType::GREATER, | ||
} | ||
|
||
def _initialize | ||
@protocol = Thrift::MultiplexedProtocol.new( | ||
@protocol, "OptimizerStoreService" | ||
) | ||
@client = Services::OptimizerStore::Client.new(@protocol) | ||
end | ||
|
||
def create_optimizer(optimizer_kind, xlimits, cstr_specs=[], options={}) | ||
opts = {} | ||
options.each do |ks, v| | ||
k = ks.to_s | ||
opts[k] = Services::OptionValue.new(matrix: v) if v.to_s =~ /^\[\[.*\]\]$/ | ||
opts[k] = Services::OptionValue.new(vector: v) if (!opts[k] && v.to_s =~ /^\[.*\]$/) | ||
opts[k] = Services::OptionValue.new(integer: v.to_i) if (!opts[k] && v.to_s == v.to_i.to_s) | ||
opts[k] = Services::OptionValue.new(number: v.to_f) if (!opts[k] && v.to_s =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/) | ||
opts[k] = Services::OptionValue.new(str: v.to_s) unless opts[k] | ||
end | ||
cspecs = cstr_specs.map do |cspec| | ||
Services::ConstraintSpec.new(type: CSTRS_TYPES[cspec[:type]] || '?', bound: cspec[:bound]) | ||
end | ||
_send { @client.create_optimizer(@id, optimizer_kind, xlimits, cspecs.compact, opts) } | ||
end | ||
|
||
def ask() | ||
res = nil | ||
_send { res = @client.ask(@id) } | ||
res | ||
end | ||
|
||
def tell(x, y) | ||
_send { | ||
@client.tell(@id, x, y) | ||
} | ||
end | ||
|
||
def destroy_optimizer | ||
_send { @client.destroy_optimizer(@id) } | ||
end | ||
|
||
end | ||
end |
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,95 @@ | ||
# frozen_string_literal: true | ||
|
||
require "thrift" | ||
require "securerandom" | ||
require "administration" | ||
|
||
module WhatsOpt | ||
class ServiceProxy | ||
attr_reader :id, :pid, :host, :port | ||
|
||
PYTHON = APP_CONFIG["python_cmd"] || "python" | ||
OUTDIR = File.join(Rails.root, "upload", "store") | ||
|
||
DEFAULT_HOST = "localhost" | ||
DEFAULT_PORT = 41400 | ||
|
||
def initialize(id: nil, host: DEFAULT_HOST, port: DEFAULT_PORT, server_start: true) | ||
@host = host | ||
@port = port | ||
socket = Thrift::Socket.new(@host, @port) | ||
@transport = Thrift::BufferedTransport.new(socket) | ||
@protocol = Thrift::BinaryProtocol.new(@transport) | ||
|
||
@admin_protocol = Thrift::MultiplexedProtocol.new( | ||
@protocol, "AdministrationService" | ||
) | ||
@admin_client = Services::Administration::Client.new(@admin_protocol) | ||
|
||
@id = id || SecureRandom.uuid | ||
|
||
self._initialize | ||
|
||
if server_start && !server_available? | ||
cmd = "#{PYTHON} #{File.join(Rails.root, 'services', 'run_server.py')} --outdir #{OUTDIR}" | ||
Rails.logger.info cmd | ||
@pid = spawn(cmd, [:out, :err] => File.join(Rails.root, "log", "whatsopt_server.log")) | ||
retries = 0 | ||
while retries < 10 && !server_available? # wait for server start | ||
retries += 1 | ||
sleep(1) | ||
end | ||
end | ||
|
||
end | ||
|
||
def _initialize | ||
raise "not yet implemented" | ||
end | ||
|
||
def self.shutdown_server(host: DEFAULT_HOST, port: DEFAULT_PORT) | ||
socket = Thrift::Socket.new(host, port) | ||
transport = Thrift::BufferedTransport.new(socket) | ||
protocol = Thrift::BinaryProtocol.new(transport) | ||
admin_protocol = Thrift::MultiplexedProtocol.new( | ||
protocol, "AdministrationService" | ||
) | ||
client = Services::Administration::Client.new(admin_protocol) | ||
transport.open() | ||
client.shutdown | ||
rescue => e | ||
Rails.logger.warn e | ||
else | ||
transport.close() | ||
end | ||
|
||
def self.kill_server(pid) | ||
if pid | ||
p "KILL #{pid}" | ||
Process.kill("TERM", pid) | ||
Process.waitpid pid | ||
end | ||
end | ||
|
||
def server_available? | ||
_send { @admin_client.ping } | ||
end | ||
|
||
def _send | ||
@transport.open() | ||
yield | ||
rescue Services::SurrogateException => e | ||
# puts "#{e}: #{e.msg}" | ||
Rails.logger.warn "#{e}: #{e.msg}" | ||
raise | ||
rescue Thrift::TransportException => e | ||
# puts "#{e}" | ||
Rails.logger.warn e | ||
false | ||
else | ||
true | ||
ensure | ||
@transport.close() | ||
end | ||
end | ||
end |
Oops, something went wrong.